Circular Doubly Linked List
Circular Doubly Linked List program
#include<stdio.h>
#include<stdlib.h>
struct Node
{
struct Node *prev;
int item;
struct Node *next;
};
void insertAtFirst(struct Node **l,int data)
{
struct Node *n ;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
if(*l == NULL)
{
*l = n;
n->next = n;
n->prev = n;
}
else
{
n->next = (*l)->next;
(*l)->next = n;
n->prev = *l;
}
}
void insertAtLast(struct Node **l,int data)
{
struct Node *n ,*t;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
if(*l == NULL)
{
*l = n;
n->prev = n;
n->next = n;
}
else
{
n->next = (*l)->next;//first node address
n->prev = *l;
(*l)->next = n;
*l = n;
}
}
void insertAfter(struct Node *temp,int data,struct Node **l)
{
struct Node *n ;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
if(temp==*l)
insertAtLast(l,data);
else
{
n->next = temp ->next;
n->prev = temp;
temp->next = n;
n->next->prev = n;
}
}
void deleteFirst(struct Node **l)
{
struct Node *t ;
t = (*l)->next ; // first node address
if((*l)==NULL)
printf("Deletion Error: List is empty");
else
{
if(t==*l)
*l =NULL;
else
{
(*l)->next = t->next;
t->next->prev = *l;
}
free(t);
}
}
void deleteLast(struct Node **l)
{
struct Node *r;
struct Node *p,*s;
r = (*l)->next;
if(*l==NULL)
printf("Deletion Error: List is empty");
else if(*l == r)
{
free(*l);
*l = NULL;
}
else
{
p = (*l)->prev ; // address of previous node
s = *l;
s->next->prev = p; // store address of second last node in prev of first node
p->next = s->next;
*l = p;
free(s);
}
}
void deleteNode(struct Node *temp,struct Node **l)
{
struct Node *r,*s,*p,*n;
r = (*l)->next;
if(*l==temp)
deleteLast(l);
else if(temp == r)
deleteFirst(l);
else
{
temp->next->prev = temp->prev;
temp->prev->next = temp -> next;
free(temp);
}
}
struct Node * findElement(struct Node *last,int element)
{
struct Node *t;
t = last->next; // first node address
do
{
if(t->item==element)
return t;
t = t->next;
}while(t!=last->next);
return NULL;
};
void viewList(struct Node *last)
{
struct Node *t;
if(last != NULL)
{
t = last;
do
{
t = t->next;
printf("%d ",t->item);
}while(t->next != last->next);
}
}
int menu()
{
int choice;
printf("\nSingly linked list demonstration\n");
printf("\n1.Insert Item as first node");
printf("\n2.Insert Item as last node");
printf("\n3.Insert Item after given node");
printf("\n4.Delete first node");
printf("\n5.Delete last node");
printf("\n6.Delete particular node");
printf("\n7.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *last = NULL;
struct Node *temp;
int item,element;
while(1)
{ printf("Data: ");
viewList(last);
system("clear");
switch(menu())
{
case 1:
printf("\nInserting at first\n");
printf("Enter the item ");
scanf("%d",&item);
insertAtFirst(&last,item);
break;
case 2:
printf("\nInserting at last\n");
printf("Enter the item ");
scanf("%d",&item);
insertAtLast(&last,item);
break;
case 3:
printf("\nInserting at the given node\n");
printf("\nEnter the element after which you want to insert\n");
scanf("%d",&element);
printf("\nEnter the item to be inserted\n");
scanf("%d",&item);
temp = findElement(last,element);
if(temp!=NULL)
insertAfter(temp,item,&last);
break;
case 4:
printf("\nDeleting at first\n");
deleteFirst(&last);
break;
case 5:
printf("\nDeleting at last\n");
deleteLast(&last);
break;
case 6:
printf("\nDeleting a particular node\n");
printf("Enter the element to be deleted");
scanf("%d",&element);
temp = findElement(last,element);
if(temp!=NULL)
deleteNode(temp,&last);
break;
case 7:
exit(0);
default:
printf("\nEnter valid options\n");
}
}
getchar();
return 0;
}
#include<stdlib.h>
struct Node
{
struct Node *prev;
int item;
struct Node *next;
};
void insertAtFirst(struct Node **l,int data)
{
struct Node *n ;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
if(*l == NULL)
{
*l = n;
n->next = n;
n->prev = n;
}
else
{
n->next = (*l)->next;
(*l)->next = n;
n->prev = *l;
}
}
void insertAtLast(struct Node **l,int data)
{
struct Node *n ,*t;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
if(*l == NULL)
{
*l = n;
n->prev = n;
n->next = n;
}
else
{
n->next = (*l)->next;//first node address
n->prev = *l;
(*l)->next = n;
*l = n;
}
}
void insertAfter(struct Node *temp,int data,struct Node **l)
{
struct Node *n ;
n = (struct Node*) malloc(sizeof(struct Node));
n->item = data;
if(temp==*l)
insertAtLast(l,data);
else
{
n->next = temp ->next;
n->prev = temp;
temp->next = n;
n->next->prev = n;
}
}
void deleteFirst(struct Node **l)
{
struct Node *t ;
t = (*l)->next ; // first node address
if((*l)==NULL)
printf("Deletion Error: List is empty");
else
{
if(t==*l)
*l =NULL;
else
{
(*l)->next = t->next;
t->next->prev = *l;
}
free(t);
}
}
void deleteLast(struct Node **l)
{
struct Node *r;
struct Node *p,*s;
r = (*l)->next;
if(*l==NULL)
printf("Deletion Error: List is empty");
else if(*l == r)
{
free(*l);
*l = NULL;
}
else
{
p = (*l)->prev ; // address of previous node
s = *l;
s->next->prev = p; // store address of second last node in prev of first node
p->next = s->next;
*l = p;
free(s);
}
}
void deleteNode(struct Node *temp,struct Node **l)
{
struct Node *r,*s,*p,*n;
r = (*l)->next;
if(*l==temp)
deleteLast(l);
else if(temp == r)
deleteFirst(l);
else
{
temp->next->prev = temp->prev;
temp->prev->next = temp -> next;
free(temp);
}
}
struct Node * findElement(struct Node *last,int element)
{
struct Node *t;
t = last->next; // first node address
do
{
if(t->item==element)
return t;
t = t->next;
}while(t!=last->next);
return NULL;
};
void viewList(struct Node *last)
{
struct Node *t;
if(last != NULL)
{
t = last;
do
{
t = t->next;
printf("%d ",t->item);
}while(t->next != last->next);
}
}
int menu()
{
int choice;
printf("\nSingly linked list demonstration\n");
printf("\n1.Insert Item as first node");
printf("\n2.Insert Item as last node");
printf("\n3.Insert Item after given node");
printf("\n4.Delete first node");
printf("\n5.Delete last node");
printf("\n6.Delete particular node");
printf("\n7.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
return choice;
}
int main()
{
struct Node *last = NULL;
struct Node *temp;
int item,element;
while(1)
{ printf("Data: ");
viewList(last);
system("clear");
switch(menu())
{
case 1:
printf("\nInserting at first\n");
printf("Enter the item ");
scanf("%d",&item);
insertAtFirst(&last,item);
break;
case 2:
printf("\nInserting at last\n");
printf("Enter the item ");
scanf("%d",&item);
insertAtLast(&last,item);
break;
case 3:
printf("\nInserting at the given node\n");
printf("\nEnter the element after which you want to insert\n");
scanf("%d",&element);
printf("\nEnter the item to be inserted\n");
scanf("%d",&item);
temp = findElement(last,element);
if(temp!=NULL)
insertAfter(temp,item,&last);
break;
case 4:
printf("\nDeleting at first\n");
deleteFirst(&last);
break;
case 5:
printf("\nDeleting at last\n");
deleteLast(&last);
break;
case 6:
printf("\nDeleting a particular node\n");
printf("Enter the element to be deleted");
scanf("%d",&element);
temp = findElement(last,element);
if(temp!=NULL)
deleteNode(temp,&last);
break;
case 7:
exit(0);
default:
printf("\nEnter valid options\n");
}
}
getchar();
return 0;
}
Comments
Post a Comment