Monday, April 6, 2015

Write a program to delete Last node in Doubly Link List.

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *prev;
int data;
struct node *next;
};

void append(struct node **q)
{
if(*q==NULL)
 {
   *q=(struct node *)malloc(sizeof(struct node));

  printf("\nenter the data in the link Doubly Link list: ");
  scanf("%d",&(*q)->data);
   (*q)->next=NULL;
   (*q)->prev=NULL;
 }
else
 {
  struct node *temp=*q;

   while(temp->next!=NULL)
    temp=temp->next;

  temp->next=(struct node*)malloc(sizeof(struct node));
  temp=temp->next;

  printf("\nEnter data in the Begining ");
  scanf("%d",&temp->data);
   temp->next=NULL;
  temp->prev=(*q);
  }
}


void display(struct node *q)
{
 struct node *temp;
  temp=q;

 while(temp!=NULL)
 {
  printf(" %d",temp->data);
  temp=temp->next;
  }
}

void delete(struct node **q)
{
 struct node *temp,*t;
  temp=*q;
   while(temp->next!=NULL)
   {
      t=temp;
     temp=temp->next;
    }
    free(temp);
    t->next=NULL;
  }


void main()
{
struct node *start;
 clrscr();
  start=NULL; //empty list
  append(&start);
  append(&start);
  append(&start);
  append(&start);
  printf("\nDoubly Link List is :");
   display(start);
    delete(&start);
    printf("\nAfter deleting a node in the list :");
   display(start);
getch();
} 

The output will be :







 


No comments:

Post a Comment