-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.cpp
179 lines (141 loc) · 3.42 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
using namespace std;
struct node{
int data;
struct node *next;
}*head;
void create(int no)
{
int val;
struct node *newnode, *temp;
int i;
head = (struct node*)malloc(sizeof(struct node));
cout<<"Enter the data of 1st node\n";
cin>>val;
head->data=val;
head->next=NULL;
temp = head;
for(i=2;i<=no;i++)
{
newnode = (struct node*)malloc(sizeof(struct node));
if(newnode==NULL)
{
cout<<"\nunbale to allocate memory";
break;
}
cout<<"Enter the valuue of node no "<<i;
cout<<"\n";
cin>>val;
newnode->data=val;
newnode->next=NULL;
temp->next = newnode;
temp=temp->next;
}
}
void addFirst(int val)
{
struct node *newnode;
newnode =(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next = head;
head = newnode;
cout<<"\nElement successfully Added in the start of the linked list-----";
}
void addLast(int val)
{
struct node* newnode;
struct node *temp;
temp=head;
newnode= (struct node*)malloc(sizeof(struct node));
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next= newnode;
newnode->data = val;
newnode->next = NULL;
cout<<"This is addlast\n";
}
void search(struct node *start,int key)
{
int count=1;
struct node *temp= start;
while(temp!=NULL)
{
if(temp->data==key)
{
cout<<"\nelement Present in the linked llist at postion no:-"<<count;
break;
}
count=count+1;
temp = temp->next;
}
cout<<"'\n Element not present in the list---";
}
void display()
{
struct node* temp;
temp = head;
while(temp->next!=NULL)
{
cout<<temp->data;
cout<<"\n";
temp = temp->next;
}
cout<<temp->data;
cout<<"\n";
}
int main()
{
int ch;
do{
cout<<"linked list\n";
cout<<"1.create Linkedlist\n";
cout<<"2.Add first";
cout<<"\n3.Add last\n";
cout<<"4.display\n";
cout<<"\n 5.Searching";
cout<<"\n6.exit\n";
cout<<"Enter your choice\n";
cin>>ch;
switch (ch)
{
case 1:
int n;
cout<<"Enter the no of nodes you want in you linkes list\n";
cin>>n;
create(n);
cout<<"\nlinked list Create succesfully----";
break;
case 2:
int v;
cout<<"\nEnter the element you want to add at the start of the linked list";
cin>>v;
addFirst(v);
break;
case 3:
int x;
cout<<"\n Enter the element you want to add at the End of the linked list---";
cin>>x;
addLast(x);
break;
case 4:
display();
break;
case 5:
int k;
cout<<"\n Enter the element you want to search in the linked list";
cin>>k;
search(head,k);
break;
case 6:
break;
default:
break;
}
}while (ch!=6);
return 0;
}