-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingly linked list with instertion first searching deletion first.cpp
147 lines (125 loc) · 2.65 KB
/
singly linked list with instertion first searching deletion first.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
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
146
147
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct mylist
{
int data;
struct mylist *next;
} node;
void insert(node *s, int data)
{
while(s->next != NULL)
{
s = s->next;
}
s->next = (node*) malloc(sizeof(node));
s->next->data = data;
s->next->next = NULL;
}
void insertFirst(node *s, int data)
{
node* temp = s->next;
s->next = (node*) malloc(sizeof(node));
s->next->data = data;
s->next->next = temp;
}
void display(node *s)
{
while(s->next != NULL)
{
cout<<s->next->data<<endl;
s = s->next;
}
}
void search(node *s, int data)
{
int count = 0;
while(s->next != NULL)
{
if(s->next->data == data)
{
count++;
}
s = s->next;
}
cout<<"Total"<<count<<"results found"<<endl;
}
void deleteNode(node *s, int data)
{
while(s->next != NULL)
{
if(s->next->data == data)
{
s->next = s->next->next;
return;
}
s = s->next;
}
}
void deleteFirst(node *s)
{
s->next = s->next->next;
}
void deleteLast(node *s)
{
while(s->next != NULL)
{
if(s->next->next == NULL)
{
s->next = NULL;
return;
}
s = s->next;
}
}
void insertAfter(node *s, int search, int data)
{
while(s->next != NULL)
{
if(s->next->data == search)
{
node *store = s->next->next;
s->next->next = (node*) malloc(sizeof(node));
s->next->next->data = data;
s->next->next->next = store;
return ;
}
s = s->next;
}
}
void countNode(node *s)
{
int count = 0;
while(s->next != NULL)
{
count++;
s = s->next;
}
cout<<"Total"<<count<<"nodes found"<<endl;
}
int main()
{
node *first = (node*) malloc(sizeof(node));
first->next = NULL;
/*Inserting Data*/
cout<<"======Inserting data========="<<endl;
insert(first, 5);
insert(first, 9);
insert(first, 11);
insert(first, 9);
/*Displaying Data*/
display(first);
/*Inserting Data at the top*/
cout<<"======Inserting data at the first========="<<endl;
insertFirst(first, 2);
/*Displaying Data*/
display(first);
cout<<"======Deleting Last Data========="<<endl;
deleteLast(first);
/*Displaying Data*/
display(first);
cout<<"======Deleting First Data========="<<endl;
deleteFirst(first);
/*Displaying Data*/
display(first);
}