Data Structures and Algorithms: A Comprehensive Guide to Queues, Lists, and Linked Lists

Circular Queue

Algorithms for Inserting an Element in a Circular Queue

  1. 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

  1. Start
  2. Read position of element to be inserted say it be ‘pos’
  3. Read element to be inserted say it be ‘el’
  4. Swap all elements one position right from last index to pos-1
  5. Now location for new element is free so set new element at that location as, a. list[pos]=el
  6. Increment size of an array by one as,
    n=n+1;
  7. stop

Delete in List

  1. Start
  2. Read position of element to be deleted ‘pos’
  3. Swap all elements one position left from specified position to last element.
  4. Decrement size of an list by one as,

n=n-1;

  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

  1. Set next of new node to null
    Newnode-next=null;
  2. If (first ==null) then
    Set, first-last-Newnode and exit.
  3. else Set, last-next=newnode; Set, last-newnode;
  4. 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

  1. Start
  2. If(first==null) then print delete and exit
  3. Else if (first==last)
    Print deleted item as, first.info;
    first=last =null
  4. Store the address of first node in a temporary variable temp.
    Set, temp=first;
  5. Set first to next of first.
    Set, first=first->next;
  6. Free the memory reserved by temp variable.
    free(temp);

Delete at Last

  1. Start
  2. If (first==null) then
    Print “Void deletion” and exit
  3. else if (first==last)
    Print deleted item as, first.info,
    Set, first-last-null,
  4. else
    temp=first,
    while(temp-next!=last)
    Set, temp-temp→next,
    Set, temp-next=null;
    Set, last=temp;

Delete at Middle

  1. Start
  2. Read position of a node which to be deleted let it be ‘pos’
  3. if first null then,
    Print “void deletion” and exit Otherwise,
  4. Enter position of a node at which you want to delete a new node. Let this po ‘pos’
  5. Set, temp-first
  6. for(i=1; iSet, temp=temp->next.
  7. Print deleted item is temp.next.info
  8. Set, loc= temp->next;
  9. Set, temp->next=loc->next;
  10. End

Insert Beginning Circular Linked List

  1. Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
  2. Read data item to be inserted say it be ‘el’
  3. Set Newnode->info=el
  4. if first==null then
    Set, Newnode ->next =Newnode
    Set, first=Newnode
    Set, last= Newnode
  5. else
    Set, Newnode->next=start
    Set, first-Newnode Set, last->next=Newnode
  6. End

Insert End Circular Linked List

  1. Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
  2. Read data item to be inserted say it be ‘el’
  3. Set Newnode-info-el
  4. if start=null then
    Set, Newnode->next=Newnode
    Set, start->Newnode
    start=Newnode
    Set, last =Newnode
  5. else
    Set, last.next=Newnode
    Set, last=Newnode
    Set, last.next-start
  6. End

Delete Begin Circular L List

  1. Start
  2. if first==null then
    Print “Empty list” and exit
  3. else
    Print the deleted element=first info Set, first-first-next Set, last-next=first;
  4. Stop

Delete End Circular L List

  1. Start
  2. if first==null then Print “empty list” and exit
  3. else if first==last
    Print deleted element=first.info
    Set, first=last=null
  4. 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
  5. 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

  1. Start
  2. Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType))
  3. Read data item to be inserted say it be ‘el’
  4. Set Newnode->info=el
  5. Set Newnode-prev=Newnode→next=null
  6. If first==null then
    Set, first=last=Newnode Otherwise,
  7. Set Newnode->next=first
  8. Set first->prev=Newnode
  9. first=Newnode

Insert End Double Linked List

  1. Start
  2. Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType))
  3. Read data item to be inserted say it be ‘el’
  4. Set Newnode->info=el
  5. Set Newnode->next=NULL
  6. If first==NULL then Set, first=last=Newnode Otherwise,
  7. Set last->next=Newnode
  8. Set Newnode->prev=last
  9. Set last=Newnode
  10. stop

Delete Begin Double L List

  1. Start
  2. if first==NULL then
    Print “empty list” and exit
  3. else
    Set, temp=first Set, first-first-next Set, first-prev=null
    Free(temp)

Delete End Double L List

  1. Start
  2. if first==NULL then
    Print “empty list” and exit
  3. else if(first==last) then Set, first->last=NULL
  4. else
    Set, temp->first;
    while(temp next!=last)
    temp->temp next
    end while
    Set temp->next=null
    Set, last=temp
  5. Stop

Insert Begin Circular D L List

  1. Start
  2. Create a new node by using malloc function as, Newnode=(NodeType*)malloc(sizeof(NodeType));
  3. Read data item to be inserted say it be ‘el’
  4. Set Newnode->info=el
  5. If first==null then
    Set, first-last-Newnode
    Set, Newnode->next=Newnode
    Set, Newnode->prev=Newnode Otherwise,
  6. Set Newnode->next=first
  7. Set, first->prev=Newnode
  8. Set first =Newnode
  9. Set last->next=first
  10. Set first->prev-last
  11. Stop

Insert End Circular D L List

  1. Start
  2. Create a new node by using malloc function as, Newnode (NodeType*)malloc(sizeof(NodeType));
  3. Read data item to be inserted say it be ‘el’
  4. Set Newnode->info=el
  5. If first==null then
    Set, first=last=Newnode
    Set. Newnode-> next Newnode
    Set, Newnode->prev=Newnode
    Otherwise,
  6. Newnode->next=first
  7. Set, first->prev=Newnode
  8. Set last->next=Newnode
  9. Set Newnode->prev=last
  10. Set last=Newnode
  11. 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