Doubly Linked List Implementation
#include<stdio.h>
#include<stdlib.h>
struct Node
{
struct Node *prev;
int item;
struct Node *next;
};
void insertAtFirst(struct Node **s, int data)
{
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
n->prev = NULL;
n->next = *s;
*s = n;
if(*s!=NULL)
{
*s = n;
}
else
{
(*s)->prev = n; // very important point
// first node in list now becomes second node
// so prev should have address of new node
*s = n;
}
}
void insertAtLast(struct Node **s, int data)
{
struct Node *t;
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
n->next = NULL;
if(*s==NULL)
{
n->prev = NULL;
*s = n;
}
else
{ t=*s;
while(t->next!=NULL)
t = t->next;
n->prev = t;
t->next = n;
}
}
void insertAfter(struct Node **s,struct Node *temp,int data)
{
struct Node *t;
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
t=*s;
if(*s == NULL)
{
n->prev = NULL;
n->next = NULL;
*s = n;
}
else
{
while(t->next !=temp)
t = t->next;
n->next = temp->next;// make changes to new node first
n->prev = temp;
if(temp->next !=NULL)
temp->next->prev = n; // put address of new node to previous of next
temp->next = n;
}
struct Node * findTemp(struct Node *start,int data)
{
while(start!=NULL)
{
if(start->item == data)
return start;
start = start -> next;
}
return NULL;
}
void deleteAtFirst(struct Node **s)
{
struct Node *t;
t=*s;
if(*s==NULL)
printf("\nDelete at First Error: No nodes in list\n");
else
{
if(t->next == NULL)
{
*s = NULL;
}
else
{
*s = t->next;
(*s)->prev = NULL;
}
free(t);
}
}
void deleteAtLast(struct Node **s)
{
struct Node *t,*secondLast;
if(*s==NULL)
printf("\nDeletion Error: No last node to be deleted\n");
else
{
t = *s;
while(t->next !=NULL)
t = t->next;
if(t->prev == NULL)
*s = NULL;
else
t-> prev-> next =NULL;
free(t);
}
}
void deleteAt(struct Node **s,struct Node *temp)
{
struct Node *t= *s;
if(*s==NULL)
printf("\nDeletion Error: No elements inside list");
else if(temp==t)
{
temp->next ->prev = NULL;
*s =temp->next;
free(t);
}
else
{
while(t->next != temp)
t = t->next;
if(temp->next == NULL)
{
free(t->next);
t->next = NULL;
}
else
{
t->next=temp->next;
temp->next -> prev = t;
free(temp);
}
}
}
void viewList(struct Node *start)
{
while(start!=NULL)
{
printf(" %d",start->item);
start = start ->next;
}
}
int menu()
{
int choice;
printf("\n-------Doubly Linked List Implementation--------------\n");
printf("\n1.Insert At First");
printf("\n2.Insert At Last");
printf("\n3.Insert After");
printf("\n4.Delete At First");
printf("\n5.Delete At Last");
printf("\n6.Delete At ");
printf("\n7.Exit\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *start = NULL;
int element,data;
struct Node *temp;
while(1)
{
system("clear");
printf("Data :");
viewList(start);
switch(menu())
{
case 1:
printf("Enter the data to be inserted first\n");
scanf("%d",&element);
insertAtFirst(&start,element);
break;
case 2:
printf("\nEnter the data to be inserted last\n");
scanf("%d",&element);
insertAtLast(&start,element);
break;
case 3:
printf("\nTo insert after given node , provide the data after which you want to insert\n");
scanf("%d",&element);
printf("\n Enter number of your choice to be inserted after ");
scanf("%d",&data);
temp = findTemp(start,element);
if(temp)
insertAfter(&start,temp,data);
break;
case 4:
printf("\nDeleting First node\n");
deleteAtFirst(&start);
break;
case 5:
printf("\nDeleting last node\n");
deleteAtLast(&start);
case 6:
printf("\nTo delete given node , provide the data which you want to insert\n");
scanf("%d",&element);
temp = findTemp(start,element);
if(temp)
deleteAt(&start,temp);
break;
case 7:
exit(0);
default:
printf("\nEnter valid choice");
}
getchar();
}
return 0;
}
#include<stdlib.h>
struct Node
{
struct Node *prev;
int item;
struct Node *next;
};
void insertAtFirst(struct Node **s, int data)
{
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
n->prev = NULL;
n->next = *s;
*s = n;
if(*s!=NULL)
{
*s = n;
}
else
{
(*s)->prev = n; // very important point
// first node in list now becomes second node
// so prev should have address of new node
*s = n;
}
}
void insertAtLast(struct Node **s, int data)
{
struct Node *t;
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
n->next = NULL;
if(*s==NULL)
{
n->prev = NULL;
*s = n;
}
else
{ t=*s;
while(t->next!=NULL)
t = t->next;
n->prev = t;
t->next = n;
}
}
void insertAfter(struct Node **s,struct Node *temp,int data)
{
struct Node *t;
struct Node *n;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
t=*s;
if(*s == NULL)
{
n->prev = NULL;
n->next = NULL;
*s = n;
}
else
{
while(t->next !=temp)
t = t->next;
n->next = temp->next;// make changes to new node first
n->prev = temp;
if(temp->next !=NULL)
temp->next->prev = n; // put address of new node to previous of next
temp->next = n;
}
struct Node * findTemp(struct Node *start,int data)
{
while(start!=NULL)
{
if(start->item == data)
return start;
start = start -> next;
}
return NULL;
}
void deleteAtFirst(struct Node **s)
{
struct Node *t;
t=*s;
if(*s==NULL)
printf("\nDelete at First Error: No nodes in list\n");
else
{
if(t->next == NULL)
{
*s = NULL;
}
else
{
*s = t->next;
(*s)->prev = NULL;
}
free(t);
}
}
void deleteAtLast(struct Node **s)
{
struct Node *t,*secondLast;
if(*s==NULL)
printf("\nDeletion Error: No last node to be deleted\n");
else
{
t = *s;
while(t->next !=NULL)
t = t->next;
if(t->prev == NULL)
*s = NULL;
else
t-> prev-> next =NULL;
free(t);
}
}
void deleteAt(struct Node **s,struct Node *temp)
{
struct Node *t= *s;
if(*s==NULL)
printf("\nDeletion Error: No elements inside list");
else if(temp==t)
{
temp->next ->prev = NULL;
*s =temp->next;
free(t);
}
else
{
while(t->next != temp)
t = t->next;
if(temp->next == NULL)
{
free(t->next);
t->next = NULL;
}
else
{
t->next=temp->next;
temp->next -> prev = t;
free(temp);
}
}
}
void viewList(struct Node *start)
{
while(start!=NULL)
{
printf(" %d",start->item);
start = start ->next;
}
}
int menu()
{
int choice;
printf("\n-------Doubly Linked List Implementation--------------\n");
printf("\n1.Insert At First");
printf("\n2.Insert At Last");
printf("\n3.Insert After");
printf("\n4.Delete At First");
printf("\n5.Delete At Last");
printf("\n6.Delete At ");
printf("\n7.Exit\n");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *start = NULL;
int element,data;
struct Node *temp;
while(1)
{
system("clear");
printf("Data :");
viewList(start);
switch(menu())
{
case 1:
printf("Enter the data to be inserted first\n");
scanf("%d",&element);
insertAtFirst(&start,element);
break;
case 2:
printf("\nEnter the data to be inserted last\n");
scanf("%d",&element);
insertAtLast(&start,element);
break;
case 3:
printf("\nTo insert after given node , provide the data after which you want to insert\n");
scanf("%d",&element);
printf("\n Enter number of your choice to be inserted after ");
scanf("%d",&data);
temp = findTemp(start,element);
if(temp)
insertAfter(&start,temp,data);
break;
case 4:
printf("\nDeleting First node\n");
deleteAtFirst(&start);
break;
case 5:
printf("\nDeleting last node\n");
deleteAtLast(&start);
case 6:
printf("\nTo delete given node , provide the data which you want to insert\n");
scanf("%d",&element);
temp = findTemp(start,element);
if(temp)
deleteAt(&start,temp);
break;
case 7:
exit(0);
default:
printf("\nEnter valid choice");
}
getchar();
}
return 0;
}
Comments
Post a Comment