-
Notifications
You must be signed in to change notification settings - Fork 0
/
doubley.c
144 lines (139 loc) · 2.86 KB
/
doubley.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
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
#include<stdio.h>
#include<stdlib.h>
//self referential structure.
struct node{
struct node *prev;
int info;
struct node *next;
};
struct node *START=NULL;
//creation of node using malloc in heap memory .malloc returns address of heap memory and store in the pointer variable.
struct node * createNode()
{
struct node *n=(struct node *)malloc(sizeof(struct node));
return (n);
}
//insertion of node at starting
void insertatbegining()
{
struct node *temp,*t;
temp=createNode();
printf("enter a number you want to add in the begining of the list -");
scanf("%d",&temp->info);
temp->next=NULL;
temp->prev=NULL;
t=START;
if(START==NULL)
START=temp;
else{
temp->next=START;
START->prev=temp;
START=temp;
}
}
//insertion of node at last
void insertNode()
{
struct node *newnode,*t;
newnode=createNode();
printf("enter a number");
scanf("%d",&newnode->info);
newnode->next=NULL;
newnode->prev=NULL;
if(START==NULL)
START=newnode;
else
{
t=START;
while(t->next!=NULL)
{
t=t->next;
}
t->next=newnode;
newnode->prev=t;
}
}
//traversing.
void viewList()
{
struct node *t;
if(START==NULL)
printf("list is empty");
else{
t=START;
while(t!=NULL)
{
printf("%d ",t->info);
t=t->next;
}
}
}
// insertion of new node at kth position
void insertionatk()
{
int k,i;
struct node *p,*newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("enter data -");
scanf("%d",&newnode->info);
newnode->next=NULL;
newnode->prev=NULL;
printf("\nenter the position where you want to insert element ");
scanf("%d",&k);
p=START;
for(i=0;i<k-2;i++)
{
p=p->next;
}
newnode->next=p->next;
p->next->prev=newnode;
p->next=newnode;
newnode=p->prev;
}
//deletion of first node.
void deletefirstNode(){
struct node *r;
if(START==NULL)
printf("list is empty");
else
{
r=START;
START=START->next;
START->prev=NULL;
r->next=NULL;
r->prev=NULL;
free(r);
}
}
//deletion of last node.
void deletelastNode(){
struct node *t,*q;
if(START==NULL)
printf("list is empty");
else{
t=START;
while(t->next->next!=NULL)
{
t=t->next;
}
q=t->next;
t->next=NULL;
q->prev=NULL;
free(q);
}
}
int main()
{ while(1)
{
insertNode();
insertNode();
insertNode();
insertNode();
insertNode();
viewList();
deletelastNode();
viewList();
deletelastNode();
viewList();
}
}