-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDlinkedList.c
More file actions
174 lines (164 loc) · 2.98 KB
/
DlinkedList.c
File metadata and controls
174 lines (164 loc) · 2.98 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL;
struct node* createNode(){
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->prev=NULL;
newNode->next=NULL;
return newNode;
}
int countNode(){
int count=0;
struct node *temp=head;
while(temp!=NULL){
temp=temp->next;
count+=1;
}
return count;
}
void push(int data){
struct node *newNode = createNode();
newNode->data=data;
if(head==NULL){
head=newNode;
}
else{
newNode->next=head;
head->prev=newNode;
head=newNode;
}
}
void append(int data){
struct node *newNode = createNode();
newNode->data=data;
if(head==NULL){
head=newNode;
}
else if(head->next==NULL){
newNode->prev=head;
head->next=newNode;
}
else{
struct node *temp=head;
while(temp->next != NULL){
temp=temp->next;
}
temp->next=newNode;
newNode->prev=temp;
}
}
void insert(int data,int n){
struct node *newNode = createNode();
newNode->data=data;
if(head==NULL && n!=0){
printf("\nEntered Position is wrong....TryAgain!!\n");
return;
}
else if(n>countNode()){
printf("Entered Position is greater than count\n");
return;
}
else{
struct node *temp=head;
for(int i = 1; i <n-1 ; i++)
{
temp=temp->next;
}
newNode->next=temp->next;
temp->next->prev=newNode;
temp->next=newNode;
newNode->prev=temp;
}
}
void delete(int n){
if(head==NULL){
printf("\nInvalid!! list is empty\n");
}
else{
struct node *temp=head;
while(temp->data!=n && temp->next!=NULL){
temp=temp->next;
}
if(temp->next==NULL){
printf("\nError!!Value not found\n");
}
else{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
printf("\nNode Deleted!!\n");
}
}
}
void display(){
struct node *temp=head;
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
}
}
int main(){
while(true){
int ch,n;
printf("Enter the choice\n");
printf("1-Push the value\n");
printf("2-Append the value\n");
printf("3-Insert the value at specified position\n");
printf("4-Delete the value\n");
printf("5-Display the LinkedList\n");
printf("6-Exit\n");
scanf("%d",&ch);
switch(ch){
case 1:
{
printf("Enter the value to be pushed\n");
scanf("%d",&n);
push(n);
break;
}
case 2:
{
printf("Enter the value to be Appended\n");
scanf("%d",&n);
append(n);
break;
}
case 3:
{
int n1;
printf("Enter the position in which value to be inserted\n");
scanf("%d",&n1);
printf("Enter the value to be inserted at that position\n");
scanf("%d",&n);
insert(n,n1);
break;
}
case 4:
{
printf("Enter the value to be Deleted\n");
scanf("%d",&n);
delete(n);
break;
}
case 5:
{
printf("Elements in the lists are :");
display();
break;
}
case 6:
printf("\n***Thank You***\n");
exit(0);
break;
default:
printf("\nWrong Choice!!Enter again\n");
break;
}
}
}