-
Notifications
You must be signed in to change notification settings - Fork 87
/
doubly_LList.cpp
133 lines (131 loc) · 2.14 KB
/
doubly_LList.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
#include<stdio.h>
#include<stdlib.h>
struct Node{
struct Node *prev;
int data;
struct Node *next;
}*first;
void create(int a[],int n){
struct Node *t,*last;
first=(struct Node *)malloc(sizeof(struct Node));
first->data=a[0];
first->prev=NULL;
first->next=NULL;
last=first;
for(int i=1;i<n;i++){
t=(struct Node *)malloc(sizeof(struct Node));
t->data=a[i];
t->next=NULL;
last->next=t;
t->prev=last;
last=t;
}
}
void display(struct Node *p){
while(p){
printf("%5d",p->data);
p=p->next;
}
}
void insert(int pos,int x){
struct Node *t,*p=first;
t=(struct Node *)malloc(sizeof(struct Node));
t->data=x;
if(pos==0){
if(first==NULL){
first=t;
t->prev=NULL;
t->next=NULL;
}else{
t->prev=NULL;
t->next=first;
first->prev=t;
first=t;
}
}
else{
for(int i=0;i<pos-1;i++){
p=p->next;
}
t->next=p->next;
t->prev=p;
if(p->next!=NULL){
p->next->prev=t;
}
p->next=t;
}
}
int Delete(int pos){
struct Node *p=first;
int x;
if(pos==1){
first=first->next;
x=p->data;
delete p;
if(first){
first->prev=NULL;
}
}
else{
for(int i=0;i<pos-1;i++){
p=p->next;
}
p->prev->next=p->next;
if(p->next){
p->next->prev=p->prev;
}
x=p->data;
delete p;
}
return x;
}
void reverse(struct Node *p){
struct Node *temp;
while(p){
temp=p->next;
p->next=p->prev;
p->prev=temp;
p=p->prev;
if(p!=NULL && p->next==NULL){
first=p;
}
}
}
int mid(struct Node *p){
struct Node *q=first;
while(q){
q=q->next;
if(q){
q=q->next;
}
if(q){
p=p->next;
}
}
return p->data;
}
int main(){
int n,pos,x;
printf("Enter the number of Nodes: ");
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int));
printf("Enter the data for nodes:\n");
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
create(a,n);
display(first);
printf("\nEnter the data and position to insert: ");
scanf("%d%d",&x,&pos);
insert(pos,x);
display(first);
printf("\nEnter the position to delete: ");
scanf("%d",&pos);
printf("%d\n",Delete(pos));
display(first);
reverse(first);
printf("\nDisplaying the reversed list:\n");
display(first);
printf("\nmiddle of linked list is: %d",mid(first));
return 0;
}