Skip to content

Instantly share code, notes, and snippets.

@jamesgeorge007
Created November 12, 2018 18:25
Show Gist options
  • Save jamesgeorge007/da337789147d9362ca264aeebb18c881 to your computer and use it in GitHub Desktop.
Save jamesgeorge007/da337789147d9362ca264aeebb18c881 to your computer and use it in GitHub Desktop.
Doubly Linked-list operations implemeted in C.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* left;
struct Node* right;
};
struct Node* header = NULL;
struct Node* temp;
void display()
{
temp = header;
if(temp == NULL)
{
printf("\nEmpty!");
exit(1);
}
while(temp != NULL)
{
printf("%d", temp->data);
temp = temp->right;
}
}
void insertAtBegin(int el)
{
temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = el;
temp->right = header;
header = temp;
temp->left = header;
display();
}
void insertAtAny(int el, int pos)
{
}
void insertAtRear(int el)
{
struct Node* ptr = header;
if(header == NULL)
{
insertAtBegin(el);
return;
}
temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = el;
temp->right = NULL;
while(ptr->right != NULL)
{
ptr = ptr->right;
}
ptr->right = temp;
temp->left = ptr;
display(1);
}
void deleteFromBegin()
{
}
void deleteFromAny(int el)
{
}
void deleteFromRear()
{
}
int main()
{
char choice;
int pos, el;
while(1)
{
printf("\n1.Insert at beginning\n2.Insert at any position\n3.Insert at rear\n4.Delete from beginning\n5.Delete from any position\n6.Delete from rear");
printf("\nYour choice: ");
fflush(stdin);
scanf("%c", &choice);
switch(choice)
{
case '1':
printf("\nElement to insert: ");
scanf("%d", &el);
insertAtBegin(el);
break;
case '2':
printf("\nEnter the position to insert and element: ");
scanf("%d%d", &el, &pos);
insertAtAny(el, pos);
break;
case '3':
printf("\nElement to insert: ");
scanf("%d", &el);
insertAtRear(el);
break;
case '4':
deleteFromBegin();
break;
case '5':
printf("\nEnter the data of the node to be deleted: ");
scanf("%d", &el);
deleteFromAny(el);
break;
case '6':
deleteFromRear();
break;
default:
printf("\nInvalid option!");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment