-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinklist.cpp
91 lines (76 loc) · 1.29 KB
/
linklist.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
90
91
#include<iostream>
using namespace std;
struct node {
int data;
struct node *link;
};
void insert(struct node** p,int d) //INSERT ELEMENTS IN LINKLIST
{ struct node *t,*r;
if(*p==NULL)
{ t=new struct node;
t->data=d;
t->link=NULL;
*p=t;
}
else{
t=*p;
while(t->link!=NULL)
t=t->link;
r=new struct node;
r->data=d;
r->link=NULL;
t->link=r;
}
}
void display(struct node *p) //DISPLAY LINKLIST
{
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->link;
}
}
int del(struct node **p,int x) //DELETE AN ELEMENT
{ int f=0;
struct node *t,*y;
t=*p;
y=*p;
if(t->data==x)
{*p=t->link;
return 0;
}
while(1)
{ t=t->link;
if(t->data==x)
{y->link=t->link;
break;
}
if(t->link==NULL)
{cout<<"\nNot found!!!";
break;
}
y=y->link;
}
}
int main()
{ node *ptr;
ptr=NULL;
int n,i,data;
cout<<"\nEnter no of data:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter data :";
cin>>data;
insert(&ptr,data);
}
cout<<"\nLinked List :";
display(ptr);
int x;
cout<<"\nWhich element to delete : ";
cin>>x;
del(&ptr,x);
cout<<"\nUpdated Linked List : ";
display(ptr);
return 0;
}