Illustrate to implement deque using circular linked list?

1 answer

Answer

1021534

2026-04-28 06:06

+ Follow

#include <stdio.h>

#include <conio.h>

#include <alloc.h>

struct node

{

int data ;

struct node *link ;

} ;

struct dqueue

{

struct node *front ;

struct node *rear ;

} ;

void initdqueue ( struct dqueue * ) ;

void addqatend ( struct dqueue *, int item ) ;

void addqatbeg ( struct dqueue *, int item ) ;

int delqatbeg ( struct dqueue * ) ;

int delqatend ( struct dqueue * ) ;

void display ( struct dqueue ) ;

int count ( struct dqueue ) ;

void deldqueue ( struct dqueue * ) ;

void main( )

{

struct dqueue dq ;

int i, n ;

clrscr( ) ;

initdqueue ( &dq ) ;

addqatend ( &dq, 11 ) ;

addqatbeg ( &dq, 10 ) ;

addqatend ( &dq, 12 ) ;

addqatbeg ( &dq, 9 ) ;

addqatend ( &dq, 13 ) ;

addqatbeg ( &dq, 8 ) ;

addqatend ( &dq, 14 ) ;

addqatbeg ( &dq, 7 ) ;

display ( dq ) ;

n = count ( dq ) ;

printf ( "\nTotal elements: %d", n ) ;

i = delqatbeg ( &dq ) ;

printf ( "\nItem extracted = %d", i ) ;

i = delqatbeg ( &dq ) ;

printf ( "\nItem extracted = %d", i ) ;

i = delqatbeg ( &dq ) ;

printf ( "\nItem extracted = %d", i ) ;

i = delqatend ( &dq ) ;

printf ( "\nItem extracted = %d", i ) ;

display ( dq ) ;

n = count ( dq ) ;

printf ( "\nElements Left: %d", n ) ;

deldqueue ( &dq ) ;

getch( ) ;

}

/* initializes elements of structure */

void initdqueue ( struct dqueue *p )

{

p -> front = p -> rear = NULL ;

}

/* adds item at the end of dqueue */

void addqatend ( struct dqueue *p, int item )

{

struct node *temp ;

temp = ( struct node * ) malloc ( sizeof ( struct node ) );

temp -> data = item ;

temp -> link = NULL ;

if ( p -> front NULL )

return ;

while ( p -> front != NULL )

{

temp = p -> front ;

p -> front = p -> front -> link ;

free ( temp ) ;

}

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.