-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cpp
More file actions
136 lines (115 loc) · 1.88 KB
/
LinkedList.cpp
File metadata and controls
136 lines (115 loc) · 1.88 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
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int info;
node* link;
};
node *start;
node *ptr, *ptr1;
void create1(){
ptr=(struct node *)malloc(sizeof(struct node));
cout<<endl<<"Enter Element: ";
cin>>ptr->info;
start=ptr;
}
void createn(){
ptr1=(struct node *)malloc(sizeof(struct node));
cout<<endl<<"Enter Element: ";
cin>>ptr1->info;
ptr->link=ptr1;
ptr=ptr1;
}
int searchEle(){
cout<<endl<<"Enter element to be searched: ";
int ele;
cin>>ele;
ptr=start;
int loc=1;
while(ptr!=NULL){
if(ptr->info==ele){
cout<<endl<<"Element Found at "<<loc<<".";
return loc;
break;
}
else{
ptr=ptr->link;
loc++;
if(ptr==NULL)cout<<endl<<"Element not found.";
}
}
}
void deleteEle(){
cout<<endl<<"Enter Element to be deleted: ";
int ele;
cin>>ele;
int pass=1;
ptr=start;
node *next;
while(ptr!=NULL){
if(pass==1){
if (ptr->info==ele){
start=ptr->link;
break;
}
pass++;
}
next=ptr->link;
if (next->info==ele){
ptr->link=next->link;
free(next);
break;
}
else{
ptr=ptr->link;
if(ptr==NULL)cout<<endl<<"Element not found.";
}
}
}
void display(){
ptr=start;
while(ptr!=NULL){
cout<<ptr->info<<" ";
ptr=ptr->link;
}
}
void addEle(){
cout<<endl<<"Enter Element after which to add new Element: ";
int pre;
cin>>pre;
cout<<endl<<"Enter Element to be added: ";
int newEle;
cin>>newEle;
node *newptr;
newptr=(struct node *)malloc(sizeof(struct node));
newptr->info=newEle;
ptr=start;
while(ptr!=NULL){
if(ptr->info==pre){
node *saver;
saver=ptr->link;
ptr->link=newptr;
newptr->link=saver;
break;
}
else ptr=ptr->link;
}
}
int main(){
char ch;
create1();
ch='y';
while(ch=='y'){
createn();
cout<<endl<<"Add more? (y/n): ";
cin>>ch;
}
ptr1->link=NULL;
display();
int a=searchEle();
deleteEle();
display();
addEle();
display();
return 0;
}