-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsinglylinkedlist.cpp
More file actions
145 lines (140 loc) · 3.72 KB
/
singlylinkedlist.cpp
File metadata and controls
145 lines (140 loc) · 3.72 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using namespace std;
#include<iostream>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int main()
{
node *temp,*p,*pp,*start;
node *ptr,*pptr,*nptr;
int i,size,exit,option,val,inopt;
cout<<"Enter the size of linked list : ";
cin>>size;
cout<<"Enter the elements of linked list";
pp=(node*)malloc(sizeof(node));
start=pp;
for (i=0;i<size;i++)
{
p=(node*)malloc(sizeof(node));
cin>>pp->data;
pp->next=p;
pp=pp->next;
}
pp->next=NULL;
exit=1;
while(exit)
{
cout<<"Enter according to following option"; //choices for the operations
cout<<"1. for insertion";
cout<<"2. for deletion";
cout<<"3. for traversal";
cin>>option;
switch(option)
{
case 1 : //for insertion
cout<<"At what position you want to insert?"<<endl<<"11. for insertion at start"<<endl<<"12. for insertion at last node"<<"13. for before the given node"<<endl<<"14. for after the given node";
cin>>inopt;
node *new_node;
new_node=(node*)malloc(sizeof(node));
switch(inopt)
{
case 11 :new_node->next=start;
start=new_node;
cin>>new_node->data;
break;
case 12 :temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new_node;
cin>>new_node->data;
new_node->next=NULL;
break;
case 13 :
cout<<"enter the data of node before which you want to insert a new node";
cin>>val;
ptr=start;
while(ptr->data!=val)
{
pptr=ptr;
ptr=ptr->next;
}
pptr->next=new_node;
new_node->next=ptr;
cin>>new_node->data;
break;
case 14 :
cout<<"enter the data of node after which you want to insert a new node";
cin>>val;
ptr=start;
pptr=start;
while(pptr->data!=val)
{
pptr=ptr;
ptr=ptr->next;
}
pptr->next=new_node;
new_node->next=ptr;
cin>>new_node->data;
break;
default : cout<<"wrong choice!!";
break;
}
case 2 : //for deletion
cout<<"At what position you want to delete?"<<endl<<"21. for deletion at start"<<endl<<"22. for deletion at last node"<<endl<<"23. for before the given node"<<endl<<"24. for after the given node";
cin>>inopt,val;
switch(inopt)
{
case 21: start=start->next;
break;
case 22: ptr=pptr=start;
while(ptr->next!=NULL)
{
pptr=ptr;
ptr=ptr->next;
}
pptr->next=NULL;
break;
case 23: cout<<"enter the value before you want to delete the node";
cin>>val;
ptr=pptr=nptr;
while(nptr->data!=val)
{
pptr=ptr;
ptr=nptr;
nptr=nptr->next;
}
pptr->next=nptr;
break;
case 24: cout<<"enter the value after you want to delete the node";
cin>>val;
ptr=pptr=nptr;
while(pptr->data!=val)
{
pptr=ptr;
ptr=nptr;
nptr=nptr->next;
}
pptr->next=nptr;
break;
default: cout<<"wronng choice!!";
}
case 3: cout<<"Entered linked list is : "; // for traversal
temp=start;
while(temp->next!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
break;
default : cout<<"wrong choice";
exit=0;
break;
}
}
}