0% found this document useful (0 votes)
10 views3 pages

Doubly Linked List

Code for doubly linked list

Uploaded by

Thanos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Doubly Linked List

Code for doubly linked list

Uploaded by

Thanos
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h> printf("Enter value: ");


#include <stdlib.h> scanf("%d", &val);
struct node { printf("Enter location: ");
int data; scanf("%d", &loc);
struct node *next, *prev; inS(val, loc);
} *head = NULL; break;
void inB(int val); case 4:
void inE(int val); deB();
void inS(int val, int loc); break;
void deB(); case 5:
void deE(); deE();
void deS(int loc); break;
void dis(); case 6:
int main() { printf("Enter location: ");
int ch, val, loc; scanf("%d", &loc);
for (;;) { deS(loc);
printf("\nA.Insert:\t1.At Beginning\t2.At End\t3.At break;
Specific Location");
case 7:
printf("\nB.Delete:\t4.At Beginning\t5.At End\t6.At
dis();
Specific Location");
break;
printf("\n7.Display\t8.Exit\tChoice: ");
case 8:
scanf("%d", &ch);
return 0;
switch (ch) {
default:
case 1:
printf("Invalid input");
printf("Enter value: ");
}
scanf("%d", &val);
}
inB(val);
}
break;
void inB(int val) {
case 2:
struct node *newnode = (struct node
printf("Enter value: ");
*)malloc(sizeof(struct node));
scanf("%d", &val);
newnode->data = val;
inE(val);
newnode->prev = NULL;
break;
newnode->next = head;
case 3:
if (head != NULL)
head->prev = newnode; if (temp == NULL) {
head = newnode; printf("Invalid location. The position is beyond
the list length.\n");
}
free(newnode);
void inE(int val) {
return;
struct node *newnode = (struct node
*)malloc(sizeof(struct node)); }
newnode->data = val; }
newnode->next = NULL;
if (head == NULL) { newnode->next = temp->next;
newnode->prev = NULL; newnode->prev = temp;
head = newnode;
} else { if (temp->next != NULL)
struct node *temp = head; temp->next->prev = newnode;
while (temp->next != NULL)
temp = temp->next; temp->next = newnode;
newnode->prev = temp; }
temp->next = newnode; void deB() {
} if (head == NULL) {
} printf("Empty List\n");
void inS(int val, int loc) { return;
struct node *newnode = (struct node }
*)malloc(sizeof(struct node));
struct node *temp = head;
newnode->data = val;
head = head->next;
if (loc == 1) {
if (head != NULL)
newnode->next = head;
head->prev = NULL;
newnode->prev = NULL;
free(temp);
if (head != NULL)
}
head->prev = newnode;
void deE() {
head = newnode;
if (head == NULL) {
return;
printf("Empty List\n");
}
return;
struct node *temp = head;
}
for (int i = 1; i < loc - 1; i++) {
struct node *temp = head;
temp = temp->next;
if (head->next == NULL) {
head = NULL; }
} else { void dis() {
while (temp->next != NULL) if (head == NULL) {
temp = temp->next; printf("Empty List\n");
temp->prev->next = NULL; } else {
} struct node *temp = head;
free(temp); printf("List: NULL<---");
} while (temp != NULL) {
void deS(int loc) { printf("%d", temp->data);
if (head == NULL) { if (temp->next != NULL)
printf("Empty List\n"); printf("<===>");
return; temp = temp->next;
} }
struct node *temp = head; printf("--->NULL\n");
if (loc == 1) { }
head = temp->next; }
if (head != NULL)
head->prev = NULL;
free(temp);
return;
}
for (int i = 1; i < loc; i++) {
temp = temp->next;
if (temp == NULL) {
printf("Invalid location. The position is beyond
the list length.\n");
return;
}
}
if (temp->next != NULL)
temp->next->prev = temp->prev;
if (temp->prev != NULL)
temp->prev->next = temp->next;
free(temp);

You might also like