Question

In: Computer Science

how to do circular linked list in c++ (inset and delet and display)

how to do circular linked list in c++
(inset and delet and display)

Solutions

Expert Solution

Please find the answer below, all the details are mentioned in the comments.

circular.cpp

#include <iostream>
#include <fstream>
#include <string>


using namespace std;

//structure of the node
struct node {
int data;//to store data
node *next;//to store next
};

//function to delete node form list from the begin
void deleteNode(struct node *&head){
//if list is empty
if(head==NULL){
cout<<"No elements to delete.\n";
}
//if head is the only element in the list
else if(head->next==head){
cout<<head->data<<" deleted.\n";
delete head;//delete head
head = NULL; //set head to NULL
}
//if there is more then one node in the list
else{
//store the head in temp
node *temp;
temp = head;
//last node of the list
node *last;
last = head;
while(last->next!=head)
last = last->next;
//update last to point to head next
last->next = head->next;
cout<<head->data<<" deleted.\n";
//head would be head->next node
head = last->next;
//delete temp node
delete temp;
}
}

//insertNode at the end.
void insertNode (int data, struct node *&head){
node *newNode;
newNode= new node;//creates new node.
newNode->data=data;//stores value.

//If the list is empty, create a single node
if(head==NULL){
newNode->next = newNode;
head = newNode;
}
//if head is the only element in the list
else if(head == head->next){
newNode->next = head->next;
head->next = newNode;
}
// If list is not empty
else{
node *temp;
//last node of the list
temp = head;
while(temp->next!=head)
temp = temp->next;
//next of new node is head
newNode->next = temp->next;
//next of last node is newNode
temp->next = newNode;
}
}
//display th list in doubly link manner
void display(struct node* head) {
node *temp;
temp = head;

printf("\nThe List is:\n");
if(head == NULL){
cout<<"Empty"<<endl;
return;
}
//traverse the list from begin
while (temp->next != head)
{
cout<<temp->data<<"-->";
temp = temp->next;
}
cout<<temp->data<<endl;
}

int main()
{
//head of the list
struct node *head=NULL;
insertNode(1,head);//insert node into the list
insertNode(2,head);//insert node into the list
insertNode(3,head);//insert node into the list
insertNode(4,head);//insert node into the list
insertNode(5,head);//insert node into the list
display(head);
deleteNode(head);
deleteNode(head);
deleteNode(head);
display(head);
deleteNode(head);
deleteNode(head);
display(head);
return 0;
}

Output:

Please let us know in the comments if you face any problems.


Related Solutions

Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use...
C++ Remove every other node in a circular linked list USING RECURSION. You can only use the preprocessor directives <iostream> and using namespace std; You must use this prototype: int remove_every_other(node *&rear)
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked)....
The file supplied.o contains code that can build, display, and destroy a linear linked list (singly-linked). For this lab, you will need to write the following two functions in list.cpp, and add function prototypes for them to list.h. The provided main.cpp has calls to each of these functions commented out. As you write the functions, uncomment them from main.cpp. void reverse(node * head, node *& newHead) Recursively make a revserse copy of the source list with head where newhead is...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next = p;     } };...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 ->...
Please use C++ and linked list to solve this problem Linked list 1 -> 2 -> 3 -> 4 -> 5-> 6 ->7 replaceNode( 5 , 6) // Replace 5 with 6     result 1 -> 2 -> 3 -> 4 -> 6 -> 6 ->7 Base code #include <iostream> using namespace std; class Node { public:     int data;     Node *next;     Node(int da = 0, Node *p = NULL) {         this->data = da;         this->next =...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy...
Description The purpose of this challenge is to implement a circular doubly-linked list using a dummy node. This challenge simulates an operating system’s window manager. Requirements Write the following struct struct Window { string appname; Window *next; Window *prev; }; Create a class called WindowManager. In this class, create a private variable Window * head. This will keep track of the location of the head node. Create private variables Window * current, * dummy. current will keep track of the...
How do you write an append function for a linked list that is a that has...
How do you write an append function for a linked list that is a that has a O(1) Big O notation in Python? The one given in class is O(n). He recommended using a variable to track the end the list. This is the code I have written so far. I don't think I'm properly defining the tail in the add possibly class Node: def __init__(self, init_data): self.data = init_data self.next = None def get_data(self): return self.data def get_next(self): return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT