-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularlist.cpp
More file actions
97 lines (93 loc) · 1.4 KB
/
circularlist.cpp
File metadata and controls
97 lines (93 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<iostream>
using namespace std;
struct node
{
int data;
struct node* next;
};
struct node* head=NULL;
struct node* getnewnode(int x)
{
struct node* newnode=new node;
newnode->data=x;
newnode->next=NULL;
return newnode;
}
//this is a function that add an element at the begin of the list
void insert_begin()
{
struct node* temp=new node;
temp=head;
int x;
cout<<"enter the element\n";
cin>>x;
struct node* newnode=getnewnode(x);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
head=newnode;
}
}
//this is a function that add an element at the end of the list
void insert_end()
{
int x;
struct node* temp=head;
cout<<"enter the element\n";
cin>>x;
struct node* newnode=getnewnode(x);
if(head==NULL)
{
head=newnode;
newnode->next=head;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=newnode;
newnode->next=head;
temp=newnode;
}
}
void display()
{
struct node* ptr=head;
while(ptr!=head)
{
cout<<ptr->data<<"-->";
ptr=ptr->next;
}
cout<<endl;
}
int main()
{
int ch;
do{
cout<<"\n1.insert_begin()() \n2.insert_end() \n3.display()\n";
cin>>ch;
switch(ch)
{
case 1:insert_begin();
break;
case 2:insert_end();
break;
case 3:display();
break;
default:cout<<"invalid choice\n";
break;
}
}while(ch!=4);
}