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}
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