Created
April 6, 2019 18:20
-
-
Save jatinsharrma/51b5ecca89346bcb2e729eacda6076fc to your computer and use it in GitHub Desktop.
Doubly Linked List implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
//Prototyping | |
void append(void); | |
void add_begin(void); | |
void add_after(void); | |
void delete_node(void); | |
void display(void); | |
void reverse(void); | |
int length(void); | |
// Intializing | |
struct node{ | |
struct node* prev; | |
int data; | |
struct node* next; | |
}; | |
struct node* root = NULL, *end = NULL; | |
int len; | |
//--------------------------------------------------------------------------------------------------- | |
int main(){ | |
while(1){ | |
printf("\nDoubly liked list operations : \n"); | |
int option; | |
printf(" 1. Append \n 2. Add at begin \n 3. Add at after \n 4. Length \n 5. Display \n 6. Delete \n 7. Reverse \n 8. Quit \n"); | |
printf("Enter your choice : "); | |
scanf("%d", &option); | |
switch(option){ | |
case 1 : append(); | |
break; | |
case 2 : add_begin(); | |
break; | |
case 3 : add_after(); | |
break; | |
case 4 : length(); | |
break; | |
case 5 : display(); | |
break; | |
case 6 : delete_node(); | |
break; | |
case 7 : reverse(); | |
break; | |
case 8: exit(0); | |
break; | |
default : printf("\nInvalid Input \n"); | |
} | |
printf("%d", end -> data); | |
} | |
return 0; | |
} | |
//--------------------------------------------------------------------------------- | |
void append(){ | |
struct node* temp; | |
temp = (struct node*)malloc(sizeof(struct node)); | |
printf("\nEnter node data : "); | |
scanf("%d",&temp -> data); | |
temp -> prev = NULL; | |
temp -> next = NULL; | |
if(root == NULL){ | |
root = temp; | |
temp -> prev = root; | |
end = temp; | |
} | |
else{ | |
struct node* p; | |
p = root; | |
while (p-> next != NULL){ | |
p = p -> next; | |
} | |
p -> next = temp; | |
temp -> prev = p; | |
end = temp; | |
} | |
} | |
//------------------------------------------------------------------------------- | |
int length(){ | |
struct node* p; | |
p = root; | |
int count = 0; | |
while (p != NULL){ | |
p = p -> next; | |
count++; | |
} | |
len = count; | |
if (count == 0){ | |
printf("\nList Empty\n"); | |
} | |
else{ | |
printf("\nTotal elements in list : %d\n",count); | |
} | |
return len; | |
} | |
//------------------------------------------------------------------------------------------ | |
void display(){ | |
struct node* temp; | |
temp = root; | |
if( temp == NULL){ | |
printf("\nNothing to display\n"); | |
} | |
else{ | |
printf("\n"); | |
while(temp != NULL){ | |
printf("%d --> ",temp -> data); | |
temp = temp -> next; | |
} | |
printf("NULL"); | |
} | |
printf("\n"); | |
} | |
//---------------------------------------------------------------------------------------------------- | |
void reverse(){ | |
struct node* temp; | |
temp = end; | |
if( temp == NULL){ | |
printf("\nNothing to display\n"); | |
} | |
else{ | |
printf("\nNULL"); | |
while(temp != root){ | |
printf("<--%d ",temp -> data); | |
temp = temp -> prev; | |
} | |
printf("<--%d", root -> data); | |
} | |
printf("\n"); | |
} | |
//---------------------------------------------------------------------------------------------------- | |
void add_begin(){ | |
struct node* temp; | |
temp = (struct node*)malloc(sizeof(struct node)); | |
printf("\nEnter node data : "); | |
scanf("%d",&temp->data); | |
temp -> next = NULL; | |
struct node* p; | |
p = root; | |
if (p == NULL){ | |
root = temp; | |
temp -> prev = root; | |
end = temp; | |
} | |
else{ | |
temp -> next = p; | |
root = temp; | |
temp -> prev = root; | |
p -> prev = temp; | |
} | |
} | |
//------------------------------------------------------------------------------------------------------ | |
void add_after(){ | |
if (root == NULL){ | |
printf("\nThere are no elements in the list\n"); | |
} | |
else{ | |
int loc; | |
printf("\nEnter element after which node to be inserted : "); | |
scanf("%d", &loc); | |
struct node* temp; | |
temp = (struct node*)malloc(sizeof(struct node)); | |
printf("\nEnter node data : "); | |
scanf("%d",&temp->data); | |
temp -> next = NULL; | |
temp -> prev = NULL; | |
struct node* p; | |
p = root; | |
while(p->next != NULL ){ | |
if (p -> data == loc){ | |
break; | |
} | |
else{ | |
p = p -> next; | |
} | |
} | |
if (p-> next == NULL && p -> data != loc){ | |
printf("\nEntered element not found in the list\n"); | |
} | |
else if (p -> next == NULL){ | |
p -> next = temp; | |
temp -> prev = p; | |
end = temp; | |
} | |
else{ | |
temp -> next = p -> next; | |
p -> next = temp ; | |
temp -> prev = p; | |
} | |
} | |
} | |
//------------------------------------------------------------------------------------------------------ | |
void delete_node(){ | |
int loc; | |
printf("\nEnter the element to be deleted : "); | |
scanf("%d",&loc); | |
struct node* p, *temp; | |
p = root; | |
if (root == NULL){ | |
printf("\nList empty\n"); | |
} | |
else{ | |
int count =0; | |
while(p -> next != NULL){ | |
if (p-> data == loc){ | |
break; | |
} | |
else{ | |
p = p -> next; | |
} | |
count ++; | |
} | |
if(count == 0){ | |
root = p -> next; | |
p-> prev = NULL; | |
free(p); | |
end = NULL; | |
} | |
else{ | |
if (p -> next == NULL && p -> data != loc){ | |
printf("\nElement not found \n"); | |
} | |
else if(p -> next == NULL){ | |
temp = p -> prev; | |
temp -> next = NULL; | |
p -> prev = NULL; | |
free(p); | |
end = temp; | |
} | |
else{ | |
struct node * temp; | |
temp = p -> prev; | |
temp -> next = p-> next; | |
p -> prev = NULL; | |
p -> next = NULL; | |
free(p); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment