Monday, 11 December 2017

Algorithm to implement a double ended queue ADT of integers using Doubly linked list.

Algorithm to implement above program:
Initialize the front and rear nodes with NULL values
struct node *front=NULL,*rear=NULL,*cur,*temp

1. Algorithm for insertion at rear end:
Step 1: Create a new node
cur=(struct node*) malloc (sizeof (struct node))
Step 2: Read the content of node (cur->data)
Step 3: Assign new node right link to NULL
cur->right=NULL
Step 4: Assign new node to front & rear node 
Check if(rear==NULL&&front==NULL)
rear=front=cur
Step 5: otherwise
rear->right=cur
cur->left=rear
rear=cur
Step 6:stop

2. Algorithm for insertion at front end:
Step 1: Create a new node
cur=(struct node*) malloc (sizeof (struct node))
Step 2: Read the content of node (cur->data)
Step 3: Assign new node right link to NULL
cur->right=NULL
Step 4: Assign new node to front & rear node
Check if(rear==NULL&&front==NULL)
rear=front=cur
Step 5: otherwise
cur->right=front
front->left=cur
front=cur
Step 6: stop

3. Algorithm for deletion at front end:
Step 1: check if(front==NULL)
Print deQueue is empty
Step 2: otherwise
Check if(front==rear)
Print Deleted data is front->data
front=rear=NULL
Step 3: otherwise
temp=front;
print Deleted data is temp->data
front=front->right
front->left=NULL
free(temp)
Step 4: stop

4. Algorithm for deletion at rear end:
Step 1: check if(front==NULL)
Print deQueue is empty
Step 2: otherwise
Check if(front==rear)
Print Deleted data is rear->data
front=rear=NULL
Step 3: otherwise
temp=rear;
print Deleted data is temp->data
if(front==rear)
front=rear=NULL
rear=rear->left
rear->right=NULL
free(temp)
Step 4: stop

5. Algorithm for displaying the dequeue:
Step 1: check if(front==NULL)
Print deQueue is empty
Step 2: otherwise
temp=front;
repeat steps a-b until temp!= NULL
a. printf("%d ",temp->data)
b. temp=temp->right;
Step 3: stop


Thanks
Mukesh Rajput

No comments:

Post a Comment

Thanks
Mukesh Rajput