-
Notifications
You must be signed in to change notification settings - Fork 1
/
doublylinkedlist_operation.c
131 lines (128 loc) · 2.42 KB
/
doublylinkedlist_operation.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
#include<stdio.h>
#include<stdlib.h>
typedef struct node1
{
int data;
struct node1 *next;
struct node1 *prev;
}node;
node *header;
void create()
{
node *ptr,*temp;
int num;
header=(node*)malloc(sizeof(node));
header->data=0;
header->next=NULL;
header->prev=NULL;
ptr=header;
printf("Enter number");
scanf("%d",&num);
while(num!=-999)
{
temp=(node*)malloc(sizeof(node));
temp->data=num;
temp->next=NULL;
ptr->next=temp;
temp->prev=ptr;
printf("Enter -999 to exit");
scanf("%d",&num);
ptr=temp;
}
}
void insertatbegin()
{
node *temp;
int num;
node *ptr;
ptr=header;
printf("Enterthe number\n");
scanf("%d",&num);
temp=(node*)malloc(sizeof(node));
temp->data=num;
temp->next=NULL;
temp->next=ptr->next;
ptr->next=temp;
ptr=temp;
}
void insertatend()
{
node *ptr;
int num;
node *temp;
ptr=header;
temp=(node*)malloc(sizeof(node));
printf("Enter data");
scanf("%d",&num);
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
temp->data=num;
temp->next=NULL;
ptr->next=temp;
temp->prev=ptr;
}
void display()
{
node *ptr;
ptr=header->next;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void deletefrombegin()
{
node *temp,*ptr;
ptr=header;
temp=ptr->next;
ptr->next=temp->next;
temp->next->prev=ptr;
free(temp);
}
void deletefromend()
{
node *ptr,*temp;
ptr=header;
while(ptr->next->next!=NULL)
{
ptr=ptr->next;
}
temp=ptr->next;
ptr->next=NULL;
free(temp);
}
int main()
{
int choice;
do
{
printf("Enter your choice\n");
printf("1-create\n");
printf("2-Insert at beginning\n");
printf("3-insert at end\n");
printf("4-Deletefrom begin\n");
printf("5-DElete from end\n");
printf("6-Display\n");
scanf("%d",&choice);
switch(choice)
{
case 1:create();
break;
case 2:insertatbegin();
break;
case 3:insertatend();
break;
case 4:deletefrombegin();
break;
case 5:deletefromend();
break;
case 6:display();
break;
default:
break;
}
}while(choice<=6);
}