-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoublelinkedlist1.txt
82 lines (79 loc) · 2.19 KB
/
doublelinkedlist1.txt
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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *prevlink;
struct node *nextlink;
};
struct node *head =0;
struct node *newnode , *tail;
int Insertatbeg(int num);
int Insertatend(int num);
int Display();
int main() {
int ch, num;
while(1){
printf("-------Operations of Double Linked List-------\n");
printf("1. Insert at the beginning\n");
printf("2. Insert at the end\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter Your Choice: \n");
scanf("%d" , &ch);
switch(ch){
case 1: printf("Enter the number you want to Insert: ");
scanf("%d" , &num);
Insertatbeg(num);
break;
case 2: printf("Enter the number you want to Insert: ");
scanf("%d" , &num);
Insertatend(num);
break;
case 3: Display();
break;
case 4: exit(0);
break;
default: printf("Invalid Choice!\n");
return 0;
}
}
}
int Insertatbeg(int num){
struct node *newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode -> data = num;
newnode -> nextlink = 0;
newnode -> prevlink = 0;
if(head == 0){
head = newnode;
} else {
head -> prevlink = newnode;
newnode -> nextlink = head;
head = newnode;
}
}
int Insertatend(int num){
struct node *newnode, *temp;
newnode = (struct node*)malloc(sizeof(struct node));
newnode -> data = num;
newnode -> nextlink = 0;
newnode -> prevlink = 0;
temp = head;
while(temp -> nextlink != 0){
temp = temp -> nextlink;
}
temp -> nextlink = newnode;
newnode -> prevlink = temp;
}
int Display(){
if(head == 0){
printf("List is Empty!");
} else {
struct node *current;
current = head;
while(current != 0){
printf("%d\t" , current -> data);
current = current -> nextlink;
}
} printf("\n");
}