//add and delete at front in doubly linked list//
/////////////////////////////////////////////////
#include"stdio.h"
#include"conio.h"
#include"alloc.h"
#include"stdlib.h"
typedef struct node
{
int data;
struct node *next,*pre;
}node;
void insert(node **,int);
void display(node *);
void del(node **);
void main()
{
node *first=NULL;
clrscr();
insert(&first,15);
insert(&first,25);
insert(&first,35);
printf("list is.\n");
display(first);
del(&first);
display(first);
getch();
}
void insert(node **first,int d)
{
node *newnode=(node *)malloc(sizeof(node));
if(newnode==NULL) {
printf("memory overflow");
exit(0); }
if((*first)==NULL) {
newnode->next=NULL;
newnode->pre=*first;
*first=newnode; }
else {
newnode->next=*first;
(*first)->pre=newnode;
newnode->pre=*first;
*first=newnode; }
newnode->data=d;
}
void display(node *first)
{
while(first!=NULL)
{
printf(" %d ",first->data);
first=first->next;
}
}
void del(node **first)
{
node *q;
if(*first==NULL){
printf("memory underflow");
exit(0); }
q=*first;
*first=q->next;
if(q->next==NULL) {
*first=NULL; }
else {
(*first)->pre=*first;
(*first)=q->next; }
free(q);
printf("\nafter deletion");
}
No comments:
Post a Comment