circular linked list in c

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

showing results for - "circular linked list in c"
Paola
26 Apr 2020
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}
Henri
19 Feb 2017
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
queries leading to this page
circular linked list in c definitioncode to create circular linked list c 2b 2bimplementation of circular linked list c 2b 2blinked list circular program c 2b 2bcircular linked list in c examplecode to delete element from a circular linked list c 2b 2bhow to make a circularly doubly linked list c 2b 2bc 2b 2b doubly circular linked listc 2b 2b circular linked listcreate circular linked list in cimplement circular linked list program hlinkes list circalsingly circular linked list in cwhat is circular linked listcode for circular linked listfor implementing circular linked list in c 2b 2bc 2b 2b circular linked list stlimplementing circular linked listcircular linked list representationcircular linked list in c 2b 2b pdfcircular linked list insert at beginning and display in csingly circular linked list in data structurecircular singly linked list c in a circular linked list 2acircular linked list in c 2b 2b code using classhow to make a circular linked listcode to create circular linked list circular linked list in data structure syntaxcircular doubly linked list in ccircular linked list in c librarycircular linked list class c 2b 2bwrite a c program to create circular singly linked list and display itinsertion in circular linked listcircular linked list with stlexplain circular linked listwhere is circular linked list useddoubly circular linked list in c 2b 2bcircular linked list insert program in ccircular linked list display program in chow to create a circular linked listcombining circular linked list c 2b 2bcircular linked list code in ccircular doubly linked list in c implementationhow to implement circular linked listcircular linked list operations program in chow to detecting a circular linked list in c 2b 2bcircular linked list in c jennycircular linked list in c 2b 2b insertion timecircular linked list examplewrite a c code to create a circular linked list circular linked list program in c short and simpledoubly circular linked list all operations c 2b 2bdefine circular linked listhow to create circular linked list in ccircular linked list in c 2b 2b using classin circular linked listcircular linked list cpp using classin circular linked lists 2c the tail node is also the head node circular linked list stl cpp referencecircular linked list advantagesdoubly circular linked listcircular linked list insertion and deletion in cdoubly circular linked list c 2b 2b guidecpp circular linked listcircular linked list operationslinked list and circular linked listdisplay circular doubly linked list c 2b 2b codecircular doubly linked list program in ccircular linked list in c example real lifecircular linked list implementationcircular sinlgy linked list cmycodeschool circularly linked list c programcircular linked list cppcircular linked list implimentationcircular singly linked list code in c 2b 2bcircular singly linked listcircular linked list code in c 2b 2bcircular linked list c 2b 2b implementationinsert function in a circular linked list c programminglinked list circular c 2b 2bcircular linked list using stlcircular linked list c programtime complexity of adding a node at front end in circular listcircular linked list with examplehow to make a circularly linked list c 2b 2bdoubly circular linked list c 2b 2bcircular linke list c 2b 2bhow to create circular linked list in c 2b 2bc circular linked listwhat is circular linked list in chow to make a linked list circularcircular doubly linked list program in c with all operationscircular linked list c 2b 2bimplementation of circular stack using linked list in c 2b 2bcircular singly linked list c 2b 2bcircular header linked list stores the address of the header node in the next field of the last nodeimplement doubly circular linked list c 2b 2bimplementation of the circular linked listcircular linked list programc program for circular linked listcircular linked list c 2b 2b using functioncircular linked list program in ccircular linked list can be used forcircular linked list in ccircular doubly linked list c 2b 2bcircularly linked list in c 2b 3d stlcircular singly linked list in ccircular linked list all operations in cinsert in circular linked list in ccircular linked list using stl in c 2b 2bcircular linked list function c 2b 2bcircular linked list simple program in ccircular linked lista 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 3fc program to implement circular linked listcircular linked list using array in ccircular linked list c 2b 2b pinchcircular linked list stl c 2b 2bcircular double linked list in cdisplay of circular linked list c 2b 2bcircular linked list stlcircular singly linked list in c 2b 2binsertion 2c deletion operations with circular linked listscircular linked list gfgdisplay circular linked list in ccircular linked list using arraycircular linked list in c 2b 2b stlcircular linked list implementation in csudo code for circular linked listcircular linked list in cppimplement doubly circular linked list c 2b 2bcircular linked list in c 2b 2bsingly circular linked listtime complexity of circular linked listcircular doubly linked list code in ccircular linked list code cinsertion before a given element in circular linked list ccreation of circular linked list in c 2b 2bcircular linked list java codecircular linked list c circular linked list in chow to make a circular linked list c 2b 2bcircular linked list codein a circular linked listcircular linked list c 2b 2b guidesingly linked list vs circular linked list representationcircular linked list insert at beginning in ccircular list ccircular linked list usescode to update circular linked list how to create a circular linked list in ccircular linked list insertioncircular linked list insert after a node in ca single pointer p is used to access the circular linkeed list to which nodecircular linked list example in c 2b 2bcircular list in ccircular linked list program in c languagecreate circle linked list cpphow to add elements to a sorted circular linked list c 2b 2bjava circular linked list geeksforgeekscircular linked list in c insertioncircular linked list structurein a circular singly linked listdoubly circular linked list program in cpseudocode for singly circular linked list operations in ccircular linked list program in c 2b 2bcopy constructor circular linked list c 2b 2bwrite a c program for implementing circular linked list circular linked list code c 2b 2btime complexity of singly circular linked listc circular listhow to detect a circular linked list in c 2b 2bimplement circular linked list and display all the list elements in c 2b 2bhow to resolve circular linked list in c 2b 2b circular linked list algorithmcircular linked list vector implementationcircular linked list insertion and deletion program in cwrite a c program to implement insertion and deletion on circular linked list implementation of circular linked list in c 2b 2bcircular linked list in c with before and aftercircular linked list examplescircular queue in linked listcircular linked list code in cppcircular linked list c 2b 2b traversalsingle circular linked listto study and implement the basic addition 2c deletion and traversal operations on singly circular linked list circular singly linked list operationsin a circular linked list 2acircular linked list insertion and deletioncircular linked list insertion in ccircular linked list c 2b 2b using classcircular linked list c 2b 2b codecircular linked list in c