-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLinked_List.cpp
136 lines (135 loc) · 2.43 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
#include<iostream>
#include<conio.h>
using namespace std;
struct node
{
int data;
struct node *link;
};
struct node* createNode(){
struct node *n;
n = (struct node *)malloc(sizeof(struct node));
return(n);
}
int k=0;
struct node *start=NULL;
int insertNode(){
int p=0;
struct node *temp, *t;
cout<<"Enter number of member you want enter"<<endl;
cin>>p;
cout<<"Enter element"<<endl;
for(int i=0;i<p;i++)
{
k++;
temp = createNode();
cin>>temp->data;
temp->link=NULL;
if(start == NULL)
start = temp;
else
{
t=start;
while(t->link!=NULL)
t=t->link;
t->link = temp;
}
}
}
void deleteNode()
{
k--;
struct node *r;
if(start == NULL)
cout<<"List is empty"<<endl;
else
{
r=start;
start = start->link;
free(r);
}
}
void viewList()
{
struct node *t;
t=start;
cout<<"LIST: "<<endl;
while(t!=NULL)
{
cout<<t->data<<"-->";
t=t->link;
}
cout<<"NULL"<<endl;
}
void length()
{
cout<<"List Length: "<<k;
}
int menu()
{
int ch;
cout<<"--------------LINKED LIST OPERATIONS-------------"<<endl;
cout<<"1.INSERT MEMBER"<<endl;
cout<<"2.DELETE FIRST MEMBER"<<endl;
cout<<"3.GET LENGTH"<<endl;
cout<<"4.VIEW LINKED LIST"<<endl;
cout<<"5.ROTATE LINKED LIST"<<endl;
cout<<"6.EXIT"<<endl;
cin>>ch;
return ch;
}
void Rotate()
{
struct node *p,*q,*new_head;
int k=1,count=1;
cout<<"Enter steps"<<endl;
cin>>k;
p=start;
while(p!=NULL)
{
if(k==count)
break;
p=p->link;
count++;
}
new_head=p->link;
p->link=NULL;
q=new_head;
while(q->link!=NULL)
{
q=q->link;
}
q->link=start;
start=new_head;
}
int main()
{
while(1)
{
system("CLS");
switch(menu())
{
case 1:
insertNode();
break;
case 2:
deleteNode();
break;
case 3:
length();
break;
case 4:
viewList();
break;
case 5:
Rotate();
break;
case 6:
exit(0);
break;
default:
cout<<"wrong options"<<endl;
}
getch();
}
}