SWE & DMT students are playing the following game: N SWE students and N DMT stud
ID: 3844108 • Letter: S
Question
SWE & DMT students are playing the following game: N SWE students and N DMT students, both are numbered from 1 to N, are sitting in a circle.
SWE students are sitting in ascending order.
DMT students are sitting between two SWE students in descending order.
Starting at student SWE1, a hot potato is passed. After M passes, the student holding the hot potato is eliminated, the circle closes ranks, and the game continues with the student who was sitting after the eliminated student picking up the hot potato. The last remaining person wins.
Use the normal pointer.
The doubly linked data structure must be used in this assignment.
The ADT list in STL is not allowed in this assignment. (list in STL is prohibited)
Sample Input:
7 3
Sample Output:
Source:
C++
Explanation / Answer
I have created and developed the C++ Program for implementing the doubly linked list. I have added the comments for each part of code and attached the final output of it.
Let me explain you in simpler and in step-by-step manner:-
Step-1:
The initial part is to implement the ADT classes which hides the logical operations and perform main operations at a initial stage like inserting, searching and removing.
Example:-
#define ADT_LIST_H
Step-2:
The next step is to bind the list of items in a pre-defined functions like checking the size of the lists, displaying the results in a view items by pushing the items using the virtual attribute.
Example:-
class LinkedListItem {
public:
virtual bool contains(int value) const = 0;
void erase(int indexVal) = 0;
int size() const = 0;
void print() const = 0;
};
Step-3:
The final step is to create a Node class which points to the initial set of items to their respective data field
Example:-
class LinkedListNode {
public:
int data;
Node *next;
};
Program:-
// The include statement is used to inherit the properties and methods of different classes
#include<stdio.h>
#include<conio.h>
// Created a structure for the Double Linked list nodes
struct DoublyLinkedNodes
{
int data;
struct DoublyLinkedNodes *next;
}*linkedTopVal = NULL;
void DoublyLinkedPushMethod(int);
void DoublyLinkedPopMethod();
void DoublyLinkedDisplayMethod();
// The main() method is the starting point of the program
void main()
{
int ListChoiceItem, LinkedValue;
printf(" :: The Double Linked List items are: ");
while(1){
printf(" Please select any one of the following items: n");
printf("1. Push 2. Pop 3. Display 4. Quit ");
printf(" Please enter your List item: ");
scanf("%d",&ListChoiceItem);
switch(ListChoiceItem){
case 1: printf("Enter the values in the Linked List ");
scanf("%d", &LinkedValue);
DoublyLinkedPushMethod(LinkedValue);
break;
case 2: DoublyLinkedPopMethod(); break;
case 3: DoublyLinkedDisplayMethod(); break;
case 4: exit(0);
default: printf(" You have selected wrong input. Please try again!! ");
}
}
}
void DoublyLinkedPushMethod(int LinkedValue)
{
struct DoublyLinkedNodes *newNode;
newNode = (struct DoublyLinkedNodes*)malloc(sizeof(struct DoublyLinkedNodes));
newNode->data = LinkedValue;
if(linkedTopVal == NULL)
newNode->next = NULL;
else
newNode->next = linkedTopVal;
linkedTopVal = newNode;
printf(" Congrats for inserting the values to the list!! ");
}
void DoublyLinkedDisplayMethod()
{
if(linkedTopVal == NULL)
printf(" Your stack value is empty ");
else{
struct DoublyLinkedNodes *LinkedTempVar = linkedTopVal;
while(LinkedTempVar->next != NULL){
printf("%d-->",LinkedTempVar->data);
LinkedTempVar = LinkedTempVar -> next;
}
printf("%d--->Its Null",LinkedTempVar->data);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.