Pseudo Code Generator

Linked List Operations in Pseudo Code

This pseudo code details the core operations of a singly linked list in C, including insertion, deletion, and display of nodes. It serves as a structured guide for implementing linked list functionality in programming.


Empty image or helper icon

Prompt

#include
#include
int pos;
struct node{
    int data;
    struct node *next;
};
int main()
{
//   declare head and node

  struct node *head,*newnode,*temp,*prevnode,*nextnode;

//   make head = 0 where head contains nothing at first
  head = 0;
  int choice,count = 0;
  while(choice)
  {
//   create a space for the node using malloc
  newnode = (struct node *)malloc(sizeof(struct node));
  printf("Enter data : ");
  scanf("%d",&newnode->data);
//   make newnode's next = NULL..because it's address will be stored in the head
  newnode->next = NULL;
//   make the head address = newnode's address
  if(head == 0)
  {
    head = temp = newnode;
  }
  else{
    temp->next = newnode;
    temp = newnode;
  }
  printf("Do you want to continue (0,1)");
  scanf("%d",&choice);
  count++;
  }
//   print count to see how much times the lists are linked
  printf("Total number of list in the linked list is %d\n",count);
  
  int wish;
  do
  {
  printf("\n1)Insertion at the beginnning \n");
  printf("2)Insertion at the end \n");
  printf("3)Insertion at the specific position\n");
  printf("4)Deletion at the beginning\n");
  printf("5)Deletion at end\n");
  printf("6)Deletion at specific position\n");
  printf("7)display\n");
  printf("8)Exit\n");
  printf("Enter the wish : ");
  scanf("%d",&wish);
  switch(wish)
  {
    case 1:
    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter the data to be inserted at the beginning: ");
    scanf("%d",&newnode->data);
    newnode->next = head;
    head = newnode;
    break;

    case 2:
    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter the data to be inserted at the end: ");
    scanf("%d",&newnode->data);
    temp = head;
    while(temp->next!=0)
    {
        temp = temp->next;
    }
    temp->next = newnode;
    newnode->next = 0;
    break;

    case 3:
    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter the data to be inserted : ");
    scanf("%d",&newnode->data);
    int i = 1;
    temp = head;
    printf("Enter the position : ");
    scanf("%d",&pos);
    if(pos > count)
    {
        printf("Invalid position");
    }
    else{
        while(inext;
        i++;
        }
        i--;
        newnode->next = temp->next;
        temp->next = newnode;
    }
    break;

    case 4:
    temp = head;
    head = head->next;
    free(temp);
    break;

    case 5:
    temp = head;
    while(temp->next!=0)
    {
      prevnode = temp;
      temp = temp->next;
    }
    if(temp == head)
    {
      head = 0;
    }
    else{
      prevnode->next = 0;
    }
    free(temp);
    break;

    case 6:
    temp = head;
    printf("Enter the position : ");
    scanf("%d",&pos);
    i = 1;
    while(inext;
      i++;
    }
    nextnode = temp->next;
    temp->next = nextnode->next;
    free(nextnode);
    break;

    case 7:
    temp = head;
    while(temp!=0)
    {
    printf("%d - ",temp->data);
    temp = temp->next;
    }
    break;
    
  }
  } while(wish!=8);
}

Answer

Pseudo Code for Linked List Operations

Overview

This pseudo code outlines the operations of a singly linked list in C, including insertion, deletion, and display of nodes. The operations include insertion at the beginning, end, and specific positions; deletion from the beginning, end, and a specific position; and displaying the list.

Pseudo Code Structure

Data Structures

STRUCT Node
    INTEGER data
    Node* next

INITIALIZE head AS NULL

Main Program Logic

FUNCTION main()
    INITIALIZE choice AS 1
    INITIALIZE count AS 0
    
    WHILE choice IS NOT 0
        newnode = ALLOCATE memory FOR Node
        PRINT "Enter data: "
        READ newnode.data
        newnode.next = NULL

        IF head IS NULL THEN
            head = newnode
            temp = newnode
        ELSE
            temp.next = newnode
            temp = newnode
        
        PRINT "Do you want to continue (0,1): "
        READ choice
        INCREMENT count

    PRINT "Total number of nodes in the linked list is count"

    INTEGER wish
    DO
        PRINT options for operations
        READ wish

        SWITCH wish
            CASE 1: // Insert at the beginning
                newnode = ALLOCATE memory FOR Node
                PRINT "Enter the data to be inserted at the beginning: "
                READ newnode.data
                newnode.next = head
                head = newnode
            
            CASE 2: // Insert at the end
                newnode = ALLOCATE memory FOR Node
                PRINT "Enter the data to be inserted at the end: "
                READ newnode.data
                temp = head
                WHILE temp.next IS NOT NULL
                    temp = temp.next
                temp.next = newnode
                newnode.next = NULL
            
            CASE 3: // Insert at specific position
                newnode = ALLOCATE memory FOR Node
                PRINT "Enter the data to be inserted: "
                READ newnode.data
                INTEGER i = 1
                temp = head
                PRINT "Enter the position: "
                READ pos

                IF pos > count THEN
                    PRINT "Invalid position"
                ELSE
                    WHILE i < pos - 1
                        temp = temp.next
                        INCREMENT i
                    newnode.next = temp.next
                    temp.next = newnode

            CASE 4: // Delete at the beginning
                temp = head
                head = head.next
                FREE temp
            
            CASE 5: // Delete at end
                temp = head
                IF temp IS NULL THEN
                    PRINT "List is empty"
                    BREAK
                WHILE temp.next IS NOT NULL
                    prevnode = temp
                    temp = temp.next
                IF temp IS head THEN
                    head = NULL
                ELSE
                    prevnode.next = NULL
                FREE temp
            
            CASE 6: // Delete at specific position
                temp = head
                PRINT "Enter the position: "
                READ pos
                INTEGER i = 1
                WHILE i < pos - 1
                    temp = temp.next
                    INCREMENT i
                nextnode = temp.next
                temp.next = nextnode.next
                FREE nextnode
            
            CASE 7: // Display the list
                temp = head
                WHILE temp IS NOT NULL
                    PRINT temp.data
                    temp = temp.next
            
            CASE 8: // Exit
                BREAK

    WHILE wish NOT EQUAL TO 8
END FUNCTION

Conclusion

This pseudo code captures the functionality of a linked list and allows for basic operations such as insertion, deletion, and display in a clear structured manner. It serves as a guide for understanding and implementing linked lists in a programming environment. For further details on data structures, courses offered by Enterprise DNA can provide valuable insights and techniques.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This pseudo code details the core operations of a singly linked list in C, including insertion, deletion, and display of nodes. It serves as a structured guide for implementing linked list functionality in programming.