circular linked list insertion

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

showing results for - "circular linked list insertion"
Leonie
15 Apr 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}
Lucia
29 Jan 2019
1#include <iostream>
2
3using namespace std;
4class node
5{
6public:
7    int data;
8    node*next;
9};
10node*head=NULL;
11void insert_begin(int a)
12{
13    node*temp=new node;
14    temp->data=a;
15    if(head==NULL)
16    {
17        temp->next=temp;
18        head=temp;
19    }
20    else
21    {
22        node*ptr=new node;
23        ptr=head;
24        while(ptr->next!=head)
25        {
26           ptr=ptr->next;
27        }
28        ptr->next=temp;
29        temp->next=head;
30        head=temp;
31
32    }
33}
34void insert_end(int a)
35{
36    node*temp=new node;
37    temp->data=a;
38    if(head==NULL)
39    {
40        temp->next=head;
41        head=temp;
42    }
43    else
44    {
45        node*ptr=new node;
46        ptr=head;
47        while(ptr->next!=head)
48        {
49            ptr=ptr->next;
50        }
51        ptr->next=temp;
52        temp->next=head;
53    }
54}
55void printf()
56{
57    node*temp=new node;
58    temp=head;
59    do
60    {
61        cout<<temp->data<<" ";
62        temp=temp->next;
63    }while(temp!=head);
64
65    cout<<endl;
66}
67
68int main()
69{
70    while(1)
71    {
72        cout<<"1-insert at begin"<<endl<<"2-insert at end"<<endl<<"3-exit"<<endl;
73        int n;
74        cout<<"enter your choice:"<<endl;
75        cin>>n;
76        cout<<"enter thee value you want to insert:"<<endl;
77        int val;
78        cin>>val;
79        switch(n)
80        {
81        case 1:
82            {
83                cout<<"enter thee value you want to insert:"<<endl;
84                  int val;
85                  cin>>val;
86                insert_begin(val);
87                printf();
88                break;
89            }
90        case 2:
91            {
92                cout<<"enter thee value you want to insert:"<<endl;
93                int val;
94                cin>>val;
95                insert_end(val);
96                printf();
97                break;
98            }
99        case 3:
100
101            {
102                exit(0);
103            }
104        default:
105            {
106                cout<<"invalid choice :"<<endl;
107            }
108        }
109    }
110    return 0;
111}
112
queries leading to this page
insertion in a circular linked listcircular list in cinsertion in circular linked list in javacircular linked list insertion at endcircular linked list program in c short and simpleinsert at ened in circular linked listcircular linked list insert at beginning and display in ccircular linked list examplescircular linked list operations program in cinsertion in circular linked list in cinsertion in circular linked list in pythoncircular linked list programcircular linked list insert after a node in ccircular linked list code c 2b 2bto study and implement the basic addition 2c deletion and traversal operations on singly circular linked list circular linked list examplein a circular singly linked listlinked list and circular linked listcircular linked list stl c 2b 2bsingly circular linked list in ccircular linked list in c librarycircular doubly linked list in c implementationtime complexity of circular linked listwhere is circular linked list used circular linked list in cinsert into a sorted circular linked listcircular linked list using stlcircular linked list c 2b 2b implementationexplain circular linked listcircular linked list in c insertionwrite a c program to implement insertion and deletion on circular linked list circular linked list representationcircular sinlgy linked list ccircular linked list insertion in cin a circular linked list 2acircular linked list insertion and deletioncircular linked list in ccode to create circular linked list singly circular linked list in data structureinsert at end in circular linked listcircular queue in linked listcircular linked list in c examplecircular linked list in c example real lifeinsert into circular linked listwhat is circular linked listcircular linked list using stl in c 2b 2bcircular singly linked list operationsinsertion at any position in circular doubly linked listinsert at position in circular linked listcircular header linked list stores the address of the header node in the next field of the last nodeexplain circular linked list insert at beginning algorithmcircular doubly linked list code in cimplementing circular linked listwrite a c program to create circular singly linked list and display itcircularly linked list in c 2b 3d stlcircular linked list java codecircular linked list appendcircular linked list insertionsingle circular linked listinsertion 2c deletion operations with circular linked listscircular doubly linked list program in c with all operationscircular linked list insertion at position in javahow to create circular linked list in ccircular linked list in c with before and aftercircular linked list c 2b 2bcircular linked list insertion a node time complexityinsertion and deletion in circular linked listcircular linked list c programc circular listinsert node in circular linked listinsert function in a circular linked list c programmingcircular linked list in c jennycircular linked list in c 2b 2b insertion timewrite a c program for implementing circular linked list circular linked list using arraycircular linked listin a circular linked listmycodeschool circularly linked list c programcircular linked list insert at beginning in chow to create circular linked list in c 2b 2binsert into sorted circular linked listimplement circular linked list program circular linked list cppa 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 3fcircular linked list insertion at end pythoninsert in circular linked listcircular linked list using array in csingly linked list vs circular linked list representationhow to create a circular linked listcircular linked list c 2b 2b guidecircular linked list c 2b 2b using functioncode to create circular linked list c 2b 2bcircular linked list implementation in ccircular linked list code in ccircular linked list program in c 2b 2bhow to make a linked list circularcircular linked list program in cc program to implement circular linked listcircular linked list insertion and deletion in ccircular linked list function c 2b 2bcircular singly linked listcircular linked list linked list adt in ccircular singly linked list in c 2b 2bcircular linked list in c 2b 2bcircular singly linked list code in c 2b 2binsert at end the circular doubly linked listtime complexity of singly circular linked listtime complexity of adding a node at front end in circular listcircular linked list with stlcircular linked list can be used forinsert in a sorted circular linked listdefine circular linked listhow to make a circular linked list in ccircular linked list structurecircular linked list adt in cdisplay circular linked list in chow to make a circular linked listcircular linked list code clinked list circular c 2b 2bcircular singly linked list in ccircular linked list example in c 2b 2bcircular linked list advantagesinsertion in circular liked listcircular linked list usesinsert in circular linked list in ccircular linked list stlcircular linked list gfgcreate circular linked list in cinsertion in circular linked list algorithmdoubly circular linked list program in cinsert in sorted circular linked listcircular linked list in c 2b 2b code using classcircular linked list in c 2b 2b stlcircular linked list insert at beginningcircular linked list implementationinsertion at the end in circular doubly linked listcircular linked list insert a node in a circular linked list 2acircular linked list insertion in pythoncircular linked list insertion and deletion program in ccircular linked list with examplecircular doubly linked list program in chow to create a circular linked list in ccircular linked list c 2b 2b codecircular linked list ccircular list cwhat is circular linked list in cinsert into singly circular linked listdoubly circular linked listcircular linked list implimentationinsertion in circular linked listcircular linked list display program in ccircular singly linked list cinsertion before a given element in circular linked list cinsertion of node in circular linked listcode for circular linked listin circular linked listinsert in the circular linked listcircular linked list insert program in ccircular linked list codeinsert into sorted circular linked list gfginsert at end the circular linked listcircular linked list operationspseudocode for singly circular linked list operations in chow to make a circularly linked list c 2b 2bcircular linked list algorithmcircular linked list code in c 2b 2bcircular linked list all operations in csorted insert for circular linked listc program for circular linked listcircular linked list in c definitioncircular doubly linked list in ccircular linked list insertion at positioncircular linked list program in c languageinsert at end of circular linked listcircular linked list simple program in cimplementation of the circular linked listsingly circular linked listinsertion of an element in a circular linked listcircular double linked list in cc circular linked listlinked list circular program c 2b 2bwrite a c code to create a circular linked list circular linked list insert at end in javacircular linked list in cppinsertion of circular doubly linked listcircular linked list insertion