circular linked list in c 2b 2b

Solutions on MaxInterview for circular linked list in c 2b 2b by the best coders in the world

showing results for - "circular linked list in c 2b 2b"
Luana
10 Oct 2018
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <stdbool.h>
5
6struct node {
7   int data;
8   int key;
9	
10   struct node *next;
11};
12
13struct node *head = NULL;
14struct node *current = NULL;
15
16bool isEmpty() {
17   return head == NULL;
18}
19
20int length() {
21   int length = 0;
22
23   //if list is empty
24   if(head == NULL) {
25      return 0;
26   }
27
28   current = head->next;
29
30   while(current != head) {
31      length++;
32      current = current->next;   
33   }
34	
35   return length;
36}
37
38//insert link at the first location
39void insertFirst(int key, int data) {
40
41   //create a link
42   struct node *link = (struct node*) malloc(sizeof(struct node));
43   link->key = key;
44   link->data = data;
45	
46   if (isEmpty()) {
47      head = link;
48      head->next = head;
49   } else {
50      //point it to old first node
51      link->next = head;
52		
53      //point first to new first node
54      head = link;
55   }    
56}
57
58//delete first item
59struct node * deleteFirst() {
60
61   //save reference to first link
62   struct node *tempLink = head;
63	
64   if(head->next == head) {  
65      head = NULL;
66      return tempLink;
67   }     
68
69   //mark next to first link as first 
70   head = head->next;
71	
72   //return the deleted link
73   return tempLink;
74}
75
76//display the list
77void printList() {
78
79   struct node *ptr = head;
80   printf("\n[ ");
81	
82   //start from the beginning
83   if(head != NULL) {
84	
85      while(ptr->next != ptr) {     
86         printf("(%d,%d) ",ptr->key,ptr->data);
87         ptr = ptr->next;
88      }
89   }
90	
91   printf(" ]");
92}
93
94void main() {
95   insertFirst(1,10);
96   insertFirst(2,20);
97   insertFirst(3,30);
98   insertFirst(4,1);
99   insertFirst(5,40);
100   insertFirst(6,56); 
101
102   printf("Original List: "); 
103	
104   //print list
105   printList();
106
107   while(!isEmpty()) {            
108      struct node *temp = deleteFirst();
109      printf("\nDeleted value:");  
110      printf("(%d,%d) ",temp->key,temp->data);
111   }   
112	
113   printf("\nList after deleting all items: ");
114   printList();   
115}
Elvis
23 Mar 2018
1#include <iostream>
2using namespace std;
3
4#define  NULL  0
5
6
7struct  node
8{
9  int  data ;
10  struct  node  *next ;
11} ;
12
13struct  node  *first=NULL ;
14struct  node  *last=NULL ;
15
16void  create()
17{
18  int  i , n ;
19  struct  node  *pnode , *p ;
20
21  printf("Enter the number of nodes required:\n") ;
22  scanf("%d",&n) ;
23
24  printf("Enter the data value of each node:\n") ;
25  for(i=1 ; i<=n ; i++)
26  {
27    pnode=(struct node*)malloc(sizeof(struct node)) ;
28    if(pnode==NULL)
29    {
30      printf("Memory overflow. Unable to create.\n") ;
31      return ;
32    }
33
34    scanf("%d",&pnode->data) ;
35
36    if(first==NULL)
37	 first=last=pnode ;
38    else
39    {
40	  last->next=pnode ;
41	  last=pnode ;    /* last keeps track of last node */
42    }
43
44    last->next=first ;
45  }
46}
47
48/* This function will delete a node with value k from the Linked List if such a node exists */
49void  deletenode(int  k)
50{
51  struct  node  *p , *follow ;
52
53  /* searching the required node */
54  p=first ;
55  follow=NULL ;
56  while(follow!=last)
57  {
58    if(p->data==k)
59	  break ;
60    follow=p ;
61    p=p->next ;
62  }
63
64  if(follow==last)
65    printf("Required node not found.\n") ;
66  else
67  {
68    if(p==first&&p==last)  /* deleting the one and the only node */
69	  first=last=NULL ;
70    else if(p==first)       /* deleting the first node */
71    {
72      first=first->next ;
73      last->next=first ;
74    }
75    else if(p==last)      /* deleting the last node */
76    {
77      last=follow ;
78      last->next=first ;
79    }
80    else		/* deleting any other node */
81      follow->next=p->next ;
82
83    free(p) ;
84  }
85}
86
87/* This function will go through all the nodes of Linked List exactly once and will display data value of each node */
88void  traverse()
89{
90  struct  node  *p , *follow ;
91  if(first==NULL)
92    printf("Circularly Linked List Empty") ;
93  else
94  {
95    printf("Circularly Linked List is as shown: \n") ;
96
97    p=first ;
98    follow = NULL ;
99    while(follow!=last)
100    {
101      printf("%d " , p->data) ;
102      follow=p ;
103      p=p->next ;
104    }
105
106    printf("\n") ;
107  }
108}
109
110int main()
111{
112  int  x , k , ch ;
113 
114  do
115  {
116    printf("\n Menu: \n") ;
117    printf("1:Create Linked List \n") ;
118    printf("2:Delete Node \n") ;
119    printf("3:Traverse \n") ;
120    printf("4:Exit \n") ;
121
122    printf("\nEnter your choice: ") ;
123    scanf("%d",&ch) ;
124
125    switch(ch)
126    {
127      case 1:
128      create() ;
129      break ;
130
131      case 2:
132      printf("Enter the data value of the node to be deleted: ") ;
133      scanf("%d",&k) ;
134      deletenode(k) ;
135      break ;
136
137      case 3:
138      traverse() ;
139      break ;
140
141      case 4:
142      break ;
143    }
144 }
145 while(ch!=4) ;
146
147 return 0;
148}
149
Bobby
28 Jan 2018
1#include<iostream>
2
3#define SIZE 100
4
5using namespace std;
6
7class node
8{
9public:
10    node()
11    {
12        next = NULL;
13    }
14  int data;
15  node *next;
16}*front=NULL,*rear=NULL,*n,*temp,*temp1;
17
18class cqueue
19{
20public:
21    void insertion();
22    void deletion();
23    void display();
24};
25
26int main()
27{
28    cqueue cqobj;
29  int ch;
30  do
31  {
32     cout<<"\n\n\tMain Menu";
33     cout<<"\n##########################";
34     cout<<"\n1. Insert\n2. Delete\n3. Display\n4. Exit\n\nEnter Your Choice: ";
35     cin>>ch;
36     switch(ch)
37     {
38        case 1:
39          cqobj.insertion();
40          cqobj.display();
41          break;
42        case 2:
43          cqobj.deletion();
44          break;
45        case 3:
46          cqobj.display();
47          break;
48        case 4:
49          break;
50        default:
51          cout<<"\n\nWrong Choice!!! Try Again.";
52     }
53  }while(ch!=4);
54  return 0;
55}
56
57void cqueue::insertion()
58{
59  n=new node[sizeof(node)];
60  cout<<"\nEnter the Element: ";
61  cin>>n->data;
62  if(front==NULL)
63  {
64      front=n;
65  }
66  else
67  {
68      rear->next=n;
69  }
70  rear=n;
71  rear->next=front;
72}
73
74void cqueue::deletion()
75{
76  int x;
77  temp=front;
78  if(front==NULL)
79  {
80      cout<<"\nCircular Queue Empty!!!";
81  }
82  else
83  {
84     if(front==rear)
85     {
86       x=front->data;
87       delete(temp);
88       front=NULL;
89       rear=NULL;
90     }
91     else
92     {
93        x=temp->data;
94        front=front->next;
95        rear->next=front;
96        delete(temp);
97     }
98     cout<<"\nElement "<<x<<" is Deleted";
99     display();
100  }
101}
102
103void cqueue::display()
104{
105  temp=front;
106  temp1=NULL;
107  if(front==NULL)
108  {
109    cout<<"\n\nCircular Queue Empty!!!";
110  }
111  else
112  {
113    cout<<"\n\nCircular Queue Elements are:\n\n";
114    while(temp!=temp1)
115    {
116       cout<<temp->data<<"  ";
117       temp=temp->next;
118       temp1=front;
119    }
120  }
121}
Jessica
08 Sep 2016
1/* Function to traverse a given Circular linked list and print nodes */
2void printList(struct Node *first) 
3{ 
4    struct Node *temp = first;  
5  
6    // If linked list is not empty 
7    if (first != NULL)  
8    { 
9        // Keep printing nodes till we reach the first node again 
10        do
11        { 
12            printf("%d ", temp->data); 
13            temp = temp->next; 
14        } 
15        while (temp != first); 
16    } 
17} 
queries leading to this page
circular linked list in c 2b 2b using classcircular singly linked list c 2b 2bcircular linked list insert at beginning and display in cdoubly circular linked listimplement doubly circular linked list c 2b 2bcircular linked list in c jennyin a circular linked list 2acircular linked list code ccircular linked list in c 2b 2b insertion timecircular linked list program in cdisplay function in circular linked listcircular queue program in c using linked listhow to make a circular linked list c 2b 2bcircular queue using linked list sanfoundrya circular queue is implement using a single linked listin circular linked lists 2c the tail node is also the head node queue using circular linked listcircular linked list stl c 2b 2bsingly circular linked list in data structurecircular linked list ccircular queue using double linked listimplementation of circular linked list c 2b 2bcircular linked list cpp using classhow to print circular linked listprint the cicular linked listdoubly circular linked list c 2b 2bprint circular linked list in cppcircular linked list using stlimplement circular queue using linked list in cimplementation of the circular linked listdoubly circular linked list program in ccircular linked list simple program in ccode to create circular linked list c 2b 2binsertion before a given element in circular linked list c in a circular linked list 2ac circular linked listis queue a circular linked listcircular queue using linked listcircular linked list using stl in c 2b 2ba circular linked list contains four nodes 7b a 2c b 2c c 2c d 7d which node pointers should be updated if a new node e is to be inserted at end of the list 3fimplementation of circular queue using linked list in cpseudocode for singly circular linked list operations in ccode for circular linked listhow to resolve circular linked list in c 2b 2b hlinkes list circalcircular linked list can be used forcircular linked list algorithmcircular queue linked list implementation in ccircular singly linked list in c 2b 2bc 2b 2b doubly circular linked listcircular queue using linked list cprint element in circular linked list in cpp code to search element in a circular linked list c 2b 2bsudo code for circular linked listcircular queue implementation in c using linked listcircular linked queuecircular linked list insertion in ccircular linked list usesstack using circular linked list in c 2b 2bhow to make a linked list circularhow to make a circularly linked list c 2b 2bfor implementing circular linked list in c 2b 2bimplementation of circular stack using linked list in c 2b 2bprint circular linked listhow to implement circular linked listcircular linked list all operations in ccircular double linked list in ccircular linked list in c with before and aftercircular linked list cppcircular linked list insertioncircular linked list pythonc program to implement circular linked listcircular linked list queue in ccircular linked list in c definitioncircular linked list in c insertioncircular doubly linked list in c implementationwrite a c program for implementing circular linked list circular linked list in c 2b 2b code using classdefine circular linked listcircular linked list code c 2b 2bimplementing circular linked listhow to make a circular linked listin circular linked listcircular queue using linked list in ccircular queue linked listcircular linked list is used to represent a queuecircular queue and its implementation in linked listlinked list and circular linked listwrite a c program to create circular singly linked list and display itcircular linked list in c 2b 2b pdfsingly linked list vs circular linked list representationinsertion 2c deletion operations with circular linked liststraversing a circular linked listcircular doubly linked list program in ccircular linked list in cppimplement doubly circular linked list c 2b 2bin a circular linked listcircular linked list insertion and deletion in ccircular linked list structurecircular queue with linked listcircular linked list c programprint a circular linked listcircular queue in c 2b 2bhow to create circular linked list in chow to create a circular linked list in ccircular queue and circular linked list same 3fqueue with circular linked list in c 2b 2bcircular linked list vs circular queue circular linked list in chow to make a circularly doubly linked list c 2b 2bto study and implement the basic addition 2c deletion and traversal operations on singly circular linked list how to traverse through a circular linked listcircular linked list class c 2b 2bc 2b 2b circular linked listcircular doubly linked list in cprint element in circular linked listcircular linked list in c examplehow to add elements to a sorted circular linked list c 2b 2btime complexity of adding a node at front end in circular listcircular linked list function c 2b 2bimplement a queue using circular linked listcircular linked list insertion and deletiontime complexity of circular linked listcircular doubly linked list program in c with all operationscircular queue linked list implementationcode to displaycircular linked list circular linked list display program in clinked list implementation of circular queue in ccircular linked list programcircular linked list queuecopy constructor circular linked list c 2b 2bcircular linked list codedisplay circular doubly linked list c 2b 2b codecircular linked list c 2b 2b implementationcircular linked list in c example real lifecircular linked list insert after a node in cmycodeschool circularly linked list c programa single pointer p is used to access the circular linkeed list to which nodecircular linked list in ccpp circular linked listdoubly circular linked list c 2b 2b guideis traversal two way in circular listlinked list circular c 2b 2bcircular header linked list stores the address of the header node in the next field of the last nodecircular queue using circular linked list c 2b 2bcircular queue using linked list in c 2b 2bprinting a circular linked listcircular doubly linked list c 2b 2bhow to detect a circular linked list in c 2b 2bimplement circular linked list and display all the list elements in c 2b 2bcircular queue using doubly linked listexplain circular linked listdisplay of circular linked list c 2b 2bcircular linked list in c 2b 2b stlcircularly linked list in c 2b 3d stlcreate circular linked list in c program to implement circular queue using linked list code to create circular linked list code to update circular linked list implementation of circular linked list in c 2b 2bcircular linked list program in c 2b 2bcircular linked list c 2b 2b guidecircular linked list c 2b 2b pinchjava circular linked list geeksforgeekscircular queue using linked list in chow to create a circular linked listin a circular singly linked listimplement circular linked list program what is circular linked list in ccircular doubly linked list code in ccombining circular linked list c 2b 2bwhere is circular linked list usedcircular linked list implementationcircular linked list code in ccircular linked list implementation in pythoncircular linked list examplecircular queue using linked list search functioncircular linked list c 2b 2b using functioncircular singly linked list code in c 2b 2bqueue linked list implementation consider implementing a circular queue using a linked list circular linked list representationcircular linke list c 2b 2bdisplay circular linked list in chow to create circular linked list in c 2b 2bwhy can we implement circular queue using linked listcircular linked list insertion and deletion program in chow will you represent circular linked list as queue write an algorithm to traverse the nodes of a circular linked list circular queue using linked list c 2b 2bdoubly circular linked list in c 2b 2bcircular linked list examplesqueue using circular linked list c 2b 2bcreation of circular linked list in c 2b 2bcircular linked list in c 2b 2bcircular linked list in c libraryc circular listc program for circular queue using linked listprint circular linked list c 2b 2bc program to implement circular queue using linked listcircular linked list c 2b 2b traversalcode to delete element from a circular linked list c 2b 2bcircular linked list insert at beginning in ccircular linked list operations program in cdoubly circular linked list all operations c 2b 2bcircular linked list in data structure syntaxcircular singly linked list ccircular linked list using arraya circular linked list is used to represent a queuecircular queue implementation using linked listinsert function in a circular linked list c programmingcircular linked list insert program in ctime complexity of singly circular linked listcircular linked list c 2b 2b using classcircular singly linked listcircular linked list with examplewrite a c program to implement insertion and deletion on circular linked list circular queue in linked listcircular queue using singly linked listcircular list ccircular linked list gfgcircular linked list code in cppinitializing circular linked listc 2b 2b circular linked list stlcircular queue linked list c 2b 2bimplementation of queue using circular linked listcircular linked listcircular linked list code in c 2b 2bcircular linked list vector implementationsingly circular linked list in calgorithm for implementation of circular queue using linked listcircular linked list stl cpp referencecircular linked list c 2b 2b codecircular linked list with stlqueue using circular linked list c 2b 2bcircular linked list implementation in cc program for circular linked listcircular sinlgy linked list crepresent linked list as circular queuecircular linked list in pythoncircular linked list implement queueinsert in circular linked list in cwhat is circular linked listsingle circular linked listcircular queue in c 2b 2b using linked listcircular linked list stllinked list circular program c 2b 2bcircular linked list c 2b 2bcircular queue using doubly linked list in cwrite a c code to create a circular linked list circular linked list program in c short and simplecircular list in ccircular linked list example in c 2b 2bcircular linked list program in c languagec 2b 2b program for circular queue using linked listhow to detecting a circular linked list in c 2b 2bcircular linked list operationsefficient code for circular linked list in cimplement the queue with circular linked list in c 2b 2bcircular linked list implimentationinsertion in circular linked listsimple queue as a circular linked listcreate circle linked list cppcircular linked list java codecircular linked list using array in csingly circular linked listcircular linked list advantagesprint doubly circular linked list in c 2b 2bcircular linked queue implementationcircular singly linked list in ccircular singly linked list operationscircular linked list in c 2b 2b