forked from piyush-kash/Hacktober2021-cpp-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked list.cpp
111 lines (102 loc) · 1.76 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
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int key;
node* next;
};
node* top=NULL;
int q=0;
int insert(int pos,int data)
{
node* temp=new node();//temp will bw used to make new node, malloc not needed in c++
temp->key=data;
temp->next=NULL;
if(pos==1)
{
node* temp1=new node();
temp1=top;
temp->next=temp1;
top=temp;
return 0;
}
if(pos>q+1)//no of elements<position to be inserted
{
cout<<"Insertion not possible\n";
return 0;
}
else if(top==NULL)//list empty
{
temp->next=NULL;
top=temp;
return 0;
}
node* temp1=new node();//for traversing
temp1=top;//temp1 is 1st node and not top
for(int i=0;i<pos-2;i++)//n-1 as we need to stop before the pos
temp1=temp1->next;
temp->next=temp1->next;//temp1 is currently the node before pos
temp1->next=temp;
}
int del(int pos)
{
node* temp=new node();
temp=top;
if(pos==1)
{
temp=temp->next;
top=temp;
return 0;
}
for(int i=0;i<pos-2;i++)
temp=temp->next;
node* temp1=new node();
temp1=temp->next;
temp->next=temp1->next;
}
int traverse()
{
if(q==0)
{
cout<<"List Empty\n";
return 0;
}
node* temp=new node();
temp=top;
while(temp!=NULL)
{
cout<<temp->key<<" ";
temp=temp->next;
}
cout<<endl;
}
int main()
{
int a=0,n=0,b=0;
while(1)
{
cout<<"1.Insert 2.Delete 3.Traverse 4.Exit\n";
cin>>n;
switch(n)
{
case 1:
cout<<"Enter no to be inserted and position(1 based index)";
cin>>a>>b;
insert(b,a);
q++;
break;
case 2:
cout<<"Enter position to be deleted(1 based index)";
cin>>a;
del(a);
q--;
break;
case 3:
traverse();
break;
case 4:
return 0;
}
}
}