-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3)LINKED LIST : Inserting and deleting first element of linked list in c++
86 lines (83 loc) · 1.73 KB
/
3)LINKED LIST : Inserting and deleting first element of linked list in c++
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
#include<iostream>
using namespace std;
struct node
{
int data;
node* address;
};
struct node* head;
void insert(int element)
{
node* block = new node();
block ->data = element;
block ->address = head;
head = block;
}
void remove()
{
node* block = head;
head = head ->address;
free(block);
}
void print()
{
node* block = head;
cout<<"List is : ";
while (block != NULL)
{
cout<<block ->data;
block = block ->address;
cout<<" ";
}
cout<<"\n";
}
int main()
{
head = NULL;
cout<<"How many elements : ";
int size , element , cut;
char id;
cin>>size;
for(int i=0 ; i<size ; i++)
{
cout<<"Enter the element : ";
cin>>element;
insert(element);
print();
if(i==size-1)
{
cout<<"Do you want to insert or delete the element (Lowercase : i/d) :";
cin>>id;
if(id==105)
{
size=size+1;
cout<<"Enter the element : ";
cin>>element;
insert(element);
print();
}
else if (id==100)
{
cout<<"How many elements do you want to remove : ";
cin>>cut;
if(cut>=size)
{
cout<<"Deletion not possible .";
}
else
{
for(int i=0 ; i<cut ; i++)
{
remove();
}
print();
}
}
else
{
cout<<"Wrong input ";
exit;
}
}
}
}