-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSingly_LL.cpp
More file actions
124 lines (117 loc) · 2.82 KB
/
Copy pathSingly_LL.cpp
File metadata and controls
124 lines (117 loc) · 2.82 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
//Author : Mr Singh
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
}*start=NULL,*last=NULL;
void Create()
{ int val,n;
cout<<"Enter How Many Node You Wanna Create : ";
cin>>n;
node *newnode=new node();
cout<<"Enter Value You Wanna Insert : ";
cin>>val;
newnode->data=val;
newnode->next=NULL;
start=newnode; last=newnode;
for(int i=1;i<n;i++)
{
node *newnode=new node();
cout<<"Enter Value You Wanna Insert : ";
cin>>val;
newnode->data=val;
newnode->next=NULL;
last->next=newnode;
last=newnode;
}
}
void Display()
{ node *temp=start;
while(temp!=NULL)
{
cout<<temp->data<<"|";
temp=temp->next;
}
}
void Insert()
{ node *temp;
node *newnode=new node();
int val,pos;
cout<<"\nEnter Data In Node That You Wanna Insert : "; cin>>val;
newnode->data=val; newnode->next=NULL;
cout<<"Enter The Position Where You Wanna Insert Node : "; cin>>pos;
if(pos==1)
{
newnode->next=start;
start=newnode;
}
else
{ temp=start;
for(int i=1;i<pos-1;i++)
temp=temp->next;
newnode->next=temp->next;
temp->next=newnode;
}
}
void Search()
{ int ele;
cout<<"Enter Element You Wanna Search : ";
cin>>ele;
node *temp=start;
for(int i=1;temp!=NULL;temp=temp->next,i++)
{
if(temp->data==ele)
{ cout<<"Element ["<<ele<<"] Fount at : "<<i<<" position\n";
}
}
}
void Delete()
{ int pos;
node *temp,*prev;
cout<<"Enter The Position Of Node That You Wanna Delete : "; cin>>pos;
if(pos==1)
{ temp=start; start=start->next;
temp->next=NULL; delete temp;}
else
{ prev=start;
for(int i=1;i<pos-1;i++)
prev=prev->next;
temp=prev->next;
prev->next=temp->next;
temp->next=NULL; delete temp;
}
}
void ReverseSLL()
{ node *prev=NULL,*next=NULL,*current=start;
while(current!=NULL)
{
next=current->next;
current->next=prev;
prev=current; current=next;
}
start=prev;
}
int main()
{ int ch;
while(1)
{
cout<<"\n**** SINGLY LINKED LIST ****\n";
cout<<"1>Create\n2>Display\n3>Insert\n4>Search\n5>Delete\n6>ReverseLL\n7>Exit\n";
cout<<"Enter Your Choice From Above :";
cin>>ch;
switch(ch)
{
case 1: Create(); break;
case 2: Display(); break;
case 3: Insert(); break;
case 4: Search(); break;
case 5: Delete(); break;
case 6: ReverseSLL(); break;
case 7: exit(0);
}
}
return 0;
}
//Author : Mr Singh @+@