Data Structures and Algorithms: A Comprehensive Guide to Queues, Lists, and Linked Lists
Circular Queue
Algorithms for Inserting an Element in a Circular Queue
- Check queue full condition as,
if (front (rear+1)%MAXSIZE)
print Queue is full and exit
else
rear (rear+1)% MAXSIZE; [increment rear by 1]
queue[rear]=item;
Algorithms for Deleting an Element from a Circular Queue
[Checking empty condition]
if (rear==front)
Print Queue is empty and exit
else
front (front+1)%MAXSIZE; [increment front by 1]
item=cqueue[front];
return item;
List
Insert New Element in List
- Start
- Read position of element to be inserted say it be ‘pos’
- Read element to be inserted say it be ‘el’
- Swap all elements one position right from last index to pos-1
- Now location for new element is free so set new element at that location as, a. list[pos]=el
- Increment size of an array by one as,
n=n+1; - stop
Delete in List
- Start
- Read position of element to be deleted ‘pos’
- Swap all elements one position left from specified position to last element.
- Decrement size of an list by one as,
n=n-1;
- Stop
LinkedList
Insert Beginning in Single Linked List
Let ‘first’ and ‘last’ are the pointer to first node and last node in the current list respectively.
1. Start
2. Create a new node using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
3. Read data item to be inserted say ‘el’
4. Assign data to the info field of new node Newnode.info-el;
5. Set next of new node to first
Newnode.next=first;
6.Set the first pointer to the new node
first Newnode;
7.End
Insert at End
- Set next of new node to null
Newnode-next=null; - If (first ==null) then
Set, first-last-Newnode and exit. - else Set, last-next=newnode; Set, last-newnode;
- End
Insert at Middle
Enter position of a node at which you want to insert a new node.
5. Set, temp=first;
6.if (first==null) then print “void insertion” and exit.
7. for(i=1; itemp-temp->next;
8. Set, Newnode->next=temp-> next;
9. Set, temp->next =Newnode.
10.end
Delete at First
- Start
- If(first==null) then print delete and exit
- Else if (first==last)
Print deleted item as, first.info;
first=last =null - Store the address of first node in a temporary variable temp.
Set, temp=first; - Set first to next of first.
Set, first=first->next; - Free the memory reserved by temp variable.
free(temp);
Delete at Last
- Start
- If (first==null) then
Print “Void deletion” and exit - else if (first==last)
Print deleted item as, first.info,
Set, first-last-null, - else
temp=first,
while(temp-next!=last)
Set, temp-temp→next,
Set, temp-next=null;
Set, last=temp;
Delete at Middle
- Start
- Read position of a node which to be deleted let it be ‘pos’
- if first null then,
Print “void deletion” and exit Otherwise, - Enter position of a node at which you want to delete a new node. Let this po ‘pos’
- Set, temp-first
- for(i=1; iSet, temp=temp->next.
- Print deleted item is temp.next.info
- Set, loc= temp->next;
- Set, temp->next=loc->next;
- End
Insert Beginning Circular Linked List
- Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
- Read data item to be inserted say it be ‘el’
- Set Newnode->info=el
- if first==null then
Set, Newnode ->next =Newnode
Set, first=Newnode
Set, last= Newnode - else
Set, Newnode->next=start
Set, first-Newnode Set, last->next=Newnode - End
Insert End Circular Linked List
- Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
- Read data item to be inserted say it be ‘el’
- Set Newnode-info-el
- if start=null then
Set, Newnode->next=Newnode
Set, start->Newnode
start=Newnode
Set, last =Newnode - else
Set, last.next=Newnode
Set, last=Newnode
Set, last.next-start - End
Delete Begin Circular L List
- Start
- if first==null then
Print “Empty list” and exit - else
Print the deleted element=first info Set, first-first-next Set, last-next=first; - Stop
Delete End Circular L List
- Start
- if first==null then Print “empty list” and exit
- else if first==last
Print deleted element=first.info
Set, first=last=null - else
Set, temp=first while(temp next!=last) Set, temp-temp→next End while
Print the deleted element-last-info
Set, last=temp
Set, last-next=first - Stop
Queue
Algorithm for Insertion an Item in Queue (Enqueue)
Initialize front=0 and rear=-1
If rear>=MAXSIZE-1 Print “queue overflow” and return
Else
Set, rear= rear+1
queue[rear]=item
Algorithm to Delete an Element from the Queue (Dequeue)
If rear Print “queue is empty” and return
Else
Item = queue [front++]
Print “item” as a deleted element
Insert Begin Double Linked List
- Start
- Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType))
- Read data item to be inserted say it be ‘el’
- Set Newnode->info=el
- Set Newnode-prev=Newnode→next=null
- If first==null then
Set, first=last=Newnode Otherwise, - Set Newnode->next=first
- Set first->prev=Newnode
- first=Newnode
Insert End Double Linked List
- Start
- Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType))
- Read data item to be inserted say it be ‘el’
- Set Newnode->info=el
- Set Newnode->next=NULL
- If first==NULL then Set, first=last=Newnode Otherwise,
- Set last->next=Newnode
- Set Newnode->prev=last
- Set last=Newnode
- stop
Delete Begin Double L List
- Start
- if first==NULL then
Print “empty list” and exit - else
Set, temp=first Set, first-first-next Set, first-prev=null
Free(temp)
Delete End Double L List
- Start
- if first==NULL then
Print “empty list” and exit - else if(first==last) then Set, first->last=NULL
- else
Set, temp->first;
while(temp next!=last)
temp->temp next
end while
Set temp->next=null
Set, last=temp - Stop
Insert Begin Circular D L List
- Start
- Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
- Read data item to be inserted say it be ‘el’
- Set Newnode->info=el
- If first==null then
Set, first-last-Newnode
Set, Newnode->next=Newnode
Set, Newnode->prev=Newnode Otherwise, - Set Newnode->next=first
- Set, first->prev=Newnode
- Set first =Newnode
- Set last->next=first
- Set first->prev-last
- Stop
Insert End Circular D L List
- Start
- Create a new node by using malloc function as, Newnode (NodeType*)malloc(sizeof(NodeType));
- Read data item to be inserted say it be ‘el’
- Set Newnode->info=el
- If first==null then
Set, first=last=Newnode
Set. Newnode-> next Newnode
Set, Newnode->prev=Newnode
Otherwise, - Newnode->next=first
- Set, first->prev=Newnode
- Set last->next=Newnode
- Set Newnode->prev=last
- Set last=Newnode
- Stop
Delete Beg Cir Dou Link List
Start
If first==null then
Print “Empty linked list” and exit
3. Else if first==last then
Set, temp=first
Set, first=last=null
4. Otherwise,
Set, temp=first
Set, first=first->next
Set, first->prev=last
Set, last->next=first
5. Free(temp)
6.Stop
Delete End Circular D L List
3.Else if first==last then
Set, temp=first
Set, first=last=null
Otherwise,
Set, temp=first
While (temp-next!=last)
Set, temp=temp→next
End while
Set, last=temp
Set, last->next=first
Set, first->prev=last
Stop
