0% found this document useful (0 votes)
4 views

Doubly Linked List

Uploaded by

tejsvibhat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Doubly Linked List

Uploaded by

tejsvibhat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Aim:- To create a doubly linked list with nodes having information

about employees that is name and ID and perform insertion in the


beginning and deletion from end operations.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>

struct node {
struct node *prev;
int id;
char emp_name[40];
struct node *next;
}*start, *node, *new_node;

int CreateList(int data, char name[40]);


int Add_at_beg(int data, char name[40]);
int Delete_at_end();
int Display();

int main() {
int ch, n, m, pos, i;
char e_name[40];
start = NULL;
node -> prev = NULL;
node -> next = NULL;
clrscr();
while(1) {
printf("Enter choice : \n");
printf("1. Create List\n");
printf("2. Add at Beginning\n");
printf("3. Delete at End\n");
printf("4. Display\n");
printf("5. Quit\n");

scanf("%d", &ch);
switch (ch) {
case 1:

printf("Enter number of nodes: ");


scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Enter ID of Employee : ");
scanf("%d", &m);
printf("Enter Employee Name :");
scanf("%s", e_name);
CreateList(m, e_name);
}
break;

case 2:
printf("Enter Roll Number :");
scanf("%d", &m);
printf("Enter Name : ");
scanf("%s", e_name);
Add_at_beg(m, e_name);
Display();
break;

case 3:
Delete_at_end();
break;

case 4:
Display();
break;

case 5:
exit(0);

default :
printf("Wrong choice");
}
}
}

int CreateList(int data, char name[40]) {


struct node *node, *new_node;
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> id = data;
strcpy(new_node -> emp_name, name);
new_node -> next = NULL;
if(start == NULL) {
start = new_node;
}
else {
node = start;
while(node -> next != NULL)
node = node -> next;
node -> next = new_node;
new_node -> prev = node;
new_node -> next = NULL;
}
return 0;
}

int Display() {
struct node *node;
if(start == NULL) {
printf("List is Empty!");
return 0;
}
node = start;
while(node != NULL) {
printf("%d\t%s \n", node -> id, node -> emp_name);
node = node -> next;
}
printf("\n");
return 0;
}

int Add_at_beg(int data, char name[40]) {


struct node *new_node;
new_node = (struct node *)malloc(sizeof(struct node));
node = start;
new_node -> next = start;
new_node -> id = data;
strcpy(new_node -> emp_name, name);
node -> prev = new_node;
new_node -> prev = NULL;
start = new_node;
return 0;
}

int Delete_at_end() {
node = start;
if (node == NULL) {
printf("Underflow!");
return 0;
}
while(node -> next -> next != NULL) {
node = node -> next;
}
node -> next -> prev = NULL;
node -> next = NULL;
Display();
return 0;
}

You might also like