You are on page 1of 9

round3

#include<stdio.h> // J .#include<stdlib.h>
#include<process.h>
type def struct II 2.typedef struct
{
int num;
struct node *next;
struct node *prev;
}NODE;

NODE *start=NULL, *last=NULL;


typedef struct node* nptr;

• vtid insert_beg(FILE *fp)

int item;
nptr p;
p=(nptr)malloc(sizeof(item)); // 3. p=(nptr)malloc(sizeof(NODE));
printf("\n enter the item:");
sea nf(" ¾d ",&item);

p->num=item;
if(start--NULL)
{
sta rt=last=p;
p->prev=NULL;
p->next=NULL;
}

• else
{
p->next=start;
start->prev=p;
p->prev=NULL;
start=p;
}
}
// 4.void insert_end(TILE
*fp)
void insert_end(FILE *fp)
Page I
round3
int item;
nptr p;
p=( n ptr)ma lloc( sizeof(item)); II 5.p=(nptr)malloc(sizeof(NODE));
printf("\nentcr the item: ");
II 6. scanf("%d",&item);
scanf("%d" ,item);

p==num=item; 117. p->num=item;


if(start==NULL)
{ sta rt=last=p;
p->prev=NULL;
p->next=NULL;
}
else
{
p->next=NULL;
p->prev=last;
last->next=p;
last=p;
}
}

void insert_spe(FILE *fp)


{
int item,i=l,pos,; II 8. int item,i=l,pos,res;
nptr p,q;
p=(nptr)malloc(sizeof(NODE));
printf("\n\nenter position for insertion: ");

scanf("%d" ,&pos);

res=count();
if(pos<= 111 pos>res)
{
printf("\n\ninsertion not possible");

}
else
{
Page 2
round3
printf(''\nenter the item : "):

~canf( "¾d" .&item):

p->num=item:
q=start;

q=q->next:
i++:

p->next=q->next;
q->next=p:
p->prev=q:
}
I
int count{)
{ int c=O:
nptrp;
p=~h• rt·:
while(p! =-NlJ I .L)
( p=p-> next;
c++;

n·turn ,:;

e ,·oid ,kleH·_hq!(FII .F •f'p)


(nptrp:
if( ~C:,rl •-..:~ l 11J .)

printf("\n\nlinkt'tl list is empt~");

'
i

if\ st:a rt==last)


{ p=~Cart:
start-==-la,t = ~l LL:
printf( " \n " ~di, ch-lc.•l<'d".p---'>num):
rou11dJ

free( p );
}
cl."JC
{ nptr p;
p=start;
start=start->next;
start->prev=NULL;
printf("\ndclcted node: o/nd",p->num);

} li 1OJl'ff(pk
}

void dcletc_end(FlLE *fp)


{ nptr p;
if(start NULL)
{
printf("\n\nlinked list is empty'');

}
if(start last)
{ p=start;
sta rt=last=NULL;
printf("\n %dis deldeted",p->num);

frec(p);
}

• else
f p=last;
last->prcv->next=NULL;
last=last->next; ff 11.last=b.st->pn-, ·:
printf("\no/od is deleted",p->num):

free(p);
}
}

void delete_spe(FILE *fp)


Page4
round3
{ int i=I.pos,res;
nptr p.q;
if(start NU LL)
{
printf("\n\nempty");

}
else
{
printf("\n enter position: ");

scanf(" %d " ,& pos);

// 12. p=q=start;
res=countQ;
if(pos>=211 pos<=res) II 13. if(pos<=2 ll pos>=res)
{
printf("\n\nDeletion is not possible");

}
else
{
while(i<pos)
{q=p;
p=p->next;
i++;
}
q->next=p->next;

• p->next->pre\'=q;
printf("deleted node: %d",p->num);

free(p);
}
}
}

\'oid tra\'erse(FILE *fp)


{ if(start==N U LL)
{

Page 5
round3
printf( "\nEmpty" );

}
else
{ nptr p;
p=start;
printf("ELEM ENTS ARE:-\n ");

while(p!=NULL)
{
printf("\n %d\n ",p->n um);

p=p->ncxt;
}
}
}

void reverse(FILE*fp)
{
if(start NULL)
{
printf("\nem pty" );

}
else
{
nptr p;
p=last;
while(p=NULL) // 14. while(p!=NULL)
{
printf(" \n%d",p->num);

p=p->prev; // J 5. p=p->prev;
}
}
}

Page 6
round3

void main()
{

int choice;
char ch~

FILE fp; // 16.FILE *fp;


fp=fopen("DLLtxt", "w"); // 17.fp=fopen("DLL.txt", "w");
char sentencel80);

printf("l: INSERT AT BEG\n");

printf("2: INSERT AT END\n");

printf("3: INSERT AT SPE\n");

printf("4: TRAVERSING\n");

printf("S: DELETE AT BEG\n");

printf("6: DELETE AT END\n");

• printf("7: DELETE AT SPE\n");

printf("8: TRAVERSING IN REVERSE\n");

do
{

printf("ENTER YOUR CHOICE\n");


Page 7
round.1

scanf(" %d ".&choice);

switch(choice)
{

case 1: insert_hcg(fp);
break;

case 2: insert_end(fp);
break;

case 3: insert_spc(fp);
break;

case 4: traverse(fp);
break;

case 5: delete_beg(fp);
break;

case 6: delete_end(fp);
break;

case 7: delete_spe(fp);
break;

case 8: reverse(fp );
break;
case 9: exit(O);

default :
printf("\n\ninvalid choice");

}
Page 8
round3
printf( " \nDO r \\-A~T TO '10RE (Y or N)\n");

sc2nf(''%s'' .&ch);

} while(ch=' Y' 11 ch='~-');


fflush(stdin ):
}

Page 9

You might also like