forked from xanxit/CPP-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inserting in a circular linked list.cpp
89 lines (88 loc) · 1.31 KB
/
inserting in a circular linked list.cpp
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
#include<iostream>
#include<string.h>
using namespace std;
class node{
public:
int data;
node*next;
}*head=NULL;
void node1(int n)
{
node *t=new node;
t->data=n;
t->next=t;
head=t;
}
void othernodes(int n)
{
node*temp=head;
node*t=new node;
while(temp->next!=head)
{
temp=temp->next;
}
t->data=n;
t->next=temp->next;
temp->next=t;
}
void insert(int n)
{
node*temp=head;
node*t=new node;
t->data=n;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=t;
t->next=head;
head=t;
}
void insertaf(int pos,int val)
{
node*temp=head;
node*t=new node;
int i=1;
while(i<pos)
{
temp=temp->next;
i++;
}
t->data=val;
t->next=temp->next;
temp->next=t;
}
void display(node*p)
{
do
{
cout<<head->data<<" ";
head=head->next;
}while(p!=head);
}
int main()
{
int p,l,i=0;
cout<<"Enter the value in the first node of 1st linked list: ";
cin>>p;
node1(p);
char n[3];
while(i>=0)
{
cout<<"Do you want to add a new node \nEnter Yes or No :";
cin>>n;
if(strcmp(n,"yes")==0||strcmp(n,"Yes")==0){
cout<<"Enter value for the next node:";
cin>>l;
othernodes(l);
}
else {
cout<<"Dhanyawaad aapka\n";
break;
}
}
insert(1);//adding 1 to the head position
insertaf(3,9);//inserting after 3rd position
display(head);
return 0;
}