-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linked_list.cpp
154 lines (134 loc) · 2.18 KB
/
Linked_list.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
#include<bits/stdc++.h>
using namespace std;
/*
1. Insert elements at top.
2. Traverse along the nodes.
3. insert at the end of the list.
4. insert in between nodes.
5. Delete a Node.
*/
class node{
public:
int data;
node* next;
};
class linklist{
public:
node* head;
linklist(){
head=NULL;
}
void insert(int data)
{
node* temp= new node();
temp->data=data;
temp->next=head;
head=temp;
}
void display()
{
node* ptr;
ptr=head;
while(ptr!=NULL)
{
cout<<ptr->data<<"->";
ptr=ptr->next;
}cout<<endl;
}
void append(int data)
{
node* temp=new node;
node* last = head;
temp->data=data;
temp->next=NULL;
if(head==NULL)
{
head=temp;
return;
}
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
return;
}
void insertAfter(int position, int data)
{ node* ptr=head;
for (int i = 0; i < position-1; ++i)
{
ptr=ptr->next;
}
node* temp=new node;
temp->data=data;
temp->next=ptr->next;
ptr->next=temp;
}
// 1 2 3 4 5 6
void del(int key)
{ node* temp= head;
node* prev;
// if head holds the key to be deleted.
if(temp!=NULL && temp->data==key)
{
head=temp->next;
free(temp);
return;
}
//else, search for the key to be deleted and keep track of
// the previosu node.
// 1 2 3 4 5 6
while(temp!=NULL && temp->data!=key)
{
prev=temp;
temp=temp->next;
}
if (temp==NULL)
{
cout<<"Key not found in list."; return;
}
prev->next=temp->next;
free(temp);
}
// null- 1->2->3->null
void reverse()
{
node* current=head;
node* prev=NULL;
node* temp=NULL;
while(current!=NULL)
{
temp=current->next;
current->next=prev;
prev=current;
current=temp;
}
head=prev;
}
};
int main()
{
linklist l1,l2;
//1. Insert at last
for (int i = 1; i <=5; ++i)
{
l1.append(i);
}
// use l1.insert(i) to insert at head.
cout<<"Linked list is: ";
l1.display(); // 1- 2 - 3 - 4 - 5
// insert after
int position,data;
cout<<"Enter the position after and the data respectively: "<<endl;
cin>>position>>data;
l1.insertAfter(position, data);
l1.display();
cout<<"Enter the Data of the node to Delete: "<<endl;
cin>>position;
l1.del(position);
l1.display();
cout<<"Reversed linked list is: "<<endl;
l1.reverse();
l1.display();
return 0;
}