-
Notifications
You must be signed in to change notification settings - Fork 106
/
linkedlist.cpp
67 lines (65 loc) · 1 KB
/
linkedlist.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
// http://btechsmartclass.com/DS/U2_T3.html
#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int num;
struct node *ptr;
}node;
node *head,*first,*temp=0;
void insert(int n)
{
first=0;
for (int i=0;i<n;i++)
{
head=new node;
cin >> head->num;
if(first!=0){temp->ptr=head;temp=head;}
else first=temp=head;
}
temp->ptr=0;
}
void print()
{
temp=first;
while(temp!=0){cout << temp->num <<" ";temp=temp->ptr;}
cout << endl;
}
void delete1(int x)
{
temp=first;
head=first;
if(temp->num==x) { first=temp->ptr; return ;}
while(temp!=0 && temp->num!=x)
{
head=temp;
temp=temp->ptr;
}
head->ptr=temp->ptr;
free(temp);
}
void add(int b)
{
temp=first;
while(temp->ptr !=0)
temp=temp->ptr;
head=new node;
head->num=b;
temp->ptr=head;
temp=head;
temp->ptr=0;
}
int main()
{
int n;
cin >> n;
insert(n);
print();
int m;
cin >> m;
delete1(m);
print();
add(8);
print();
return 0;
}