0% found this document useful (0 votes)
274 views11 pages

Concatenate Two SLL Into One: Pseudo Code

1. The document provides pseudo code to implement various data structures using linked lists in C including: - Concatenating two singly linked lists into one - Copying the elements of one doubly linked list into another - Implementing a queue with first-in first-out behavior using a singly linked list - Implementing a stack with last-in first-out behavior using a singly linked list - Implementing a circular queue using a singly linked list 2. For each data structure, functions are provided to add/remove elements, display elements, and a main driver function is described. 3. Pseudo code includes details of dynamically allocating memory, adding/removing elements from the head and tail, and
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
274 views11 pages

Concatenate Two SLL Into One: Pseudo Code

1. The document provides pseudo code to implement various data structures using linked lists in C including: - Concatenating two singly linked lists into one - Copying the elements of one doubly linked list into another - Implementing a queue with first-in first-out behavior using a singly linked list - Implementing a stack with last-in first-out behavior using a singly linked list - Implementing a circular queue using a singly linked list 2. For each data structure, functions are provided to add/remove elements, display elements, and a main driver function is described. 3. Pseudo code includes details of dynamically allocating memory, adding/removing elements from the head and tail, and
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

26/08/2020 Pranava Raman BMS 2019103555

1. Concatenate two SLL into one

Pseudo Code:-
Initialize the Singly linked list
 
struct Node{
    int data;
    struct Node *next;
};
 
Function Getdetails(Node **list):
Initialize a variable 'temp' of type Int
While(true):
Get value for 'temp'
If temp==0:
Break
 
Initialize 'ptr' of type Node*
Store temp in ptr->data
Insert (ptr,list)
 
Function Insert (Node *ptr, Node **listhead):
If (*listhead) equals NULL:
Make (*listhead) point to ‘ptr’
Else:
Store (*listhead)->next in ‘ptr->next’
Make (*listhead)->next as ‘ptr’
 
Function concat(Node *list1, Node *list2):
Initialize 'temp' of type Node*
Make temp point to ‘list1’
 
While (temp->next is NOT NULL):
Make ‘temp’ as ‘temp->next’
//bringing temp to the end of list1
 
Make temp->next point to ‘list2’
 
Function Display (Node *list):
Initialize 'temp' of type Node*
Make temp point to list
 
While (temp is NOT NULL)
Print temp->data
Make temp as temp->next
Function main: //driver function
Initialize list1 and list2 of type Node* with value NULL
Call Getdetails (&list1)
Call Getdetails (&list2)
 
Call Concat (list1, list2)
Call Display (list1)
 
 
Output:-

2. Copy one DLL into another


Pseudo Code:-
Initialize struct Node
struct Node{
    int data;
    struct Node *next;
    struct Node *prev;

} typedef Node;
Node *head = NULL, *head2=NULL, *rear = NULL, *rear2=NULL;

Function insDetails(int dat, Node **head, Node **rear):

Dynamically allocate a sizeof(Node) bytes and assign to *new


Save ‘dat’ in ‘new->data’
Make ‘new->next’ point NULL
Make ‘new->prev’ point NULL

If ‘*head’ AND ‘*rear’ point to NULL:


Make ‘*head’ and ‘*rear’ point to ‘new’
Else:
Make ‘(*rear)->next’ point to ‘new’
Make ‘(*rear)’ as ‘new’

Function Copylist():

Assign a ‘temp’ variable of Node* type point to ‘head’

While (‘temp’ is NOT NULL):


Call insDetails(temp->data, &head2, &rear2)
Make ‘temp’ point to ‘temp->next’

Function Display (Node *list):

Initialize 'temp' of type Node*


Make temp point to list
 
While (temp is NOT NULL)
Print ‘temp->data’
Make ‘temp’ as ‘temp->next’

Function main: //driver function


Initialize a variable 'temp' of type Int

While(true): //adding the values to first list


Get value for 'temp'
If temp==0:
Break
Call insDetails (temp, &head, &rear)

Call Display (head)


Call Copylist ()
Call Display (head2)

Output:-
3. Always do insertion at last and delete at delete (FIFO)

Pseudo Code:-
Initialaize struct Node:
struct Node{
    int data;
    struct Node* next;
}typedef Node;
Node *head=NULL, *rear=NULL;

Function Enqueue(int dat):


Dynamically allocate sizeof(Node) bytes to ‘new’ pointer
Make new->data store ‘dat’
Make new->next point to NULL

If ‘head’ and ‘rear’ points to NULL:


Make head & rear point to new
Else:
Make ‘rear->next’ point to ‘new’
Make ‘rear’ as ‘new’

Function Dequeue():
If ‘head’ points to NULL:
Print “Underflow”
Return

Assign a ‘temp’ variable of type Node* point to ‘head’


Make ‘head’ point to ‘head->next’
Free the dynamically allocated storage in ‘temp’
If ‘head’ points to NULL:
Make ‘rear’ point to NULL

Function Display():
If ‘head’ points to NULL:
Print “Underflow”
Return
Assign a ‘temp’ variable of type Node* point to ‘head’
While (‘temp’ is NOT NULL):
Print ‘temp->data’
Make ‘temp’ point to ‘temp->next’

/* Make a driver code asking from the user each time to choose the
function */
Output:
4. Always do insertion and deletion at first (LIFO)
Pseudo Code:
Initialaize struct Node:
struct Node{
    int data;
    struct Node* next;
}typedef Node;
Node *head=NULL;

Function Push(int dat):

Dynamically allocate sizeof(Node) bytes and assign the address to


‘new’ pointer

Assign ‘dat’ to ‘new->data’


Make ‘new->next’ point to NULL

If ‘head’ points to NULL:


Make ‘head’ point to ‘new’
Else:
Make ‘new->next’ point to current ‘head’
Make ‘head’ point to ‘new’

Function Pop():
If ‘head’ points to NULL:
Print “Underflow”
Return

Assign a ‘temp’ variable of type Node* point to ‘head’


Make ‘head’ point to ‘head->next’
Free the dynamically allocated storage in ‘temp’

Function Peek():
If ‘head’ points to NULL:
Print “Underflow”
Return
Print ‘head->data’
Function Display():
If ‘head’ points to NULL:
Print “Underflow”
Return
Assign a ‘temp’ variable of type Node* point to ‘head’
While (‘temp’ is NOT NULL):
Print ‘temp->data’
Make ‘temp’ point to ‘temp->next’
/* Make a driver code asking from the user each time to choose the
function */

Output:
5. Make circular queue using SLL

Pseudo Code:
Initialaize struct Node:
struct Node{
    int data;
    struct Node* next;
}typedef Node;
Node *head=NULL, *rear=NULL;

Function Enqueue(int dat):


Dynamically allocate sizeof(Node) bytes to ‘new’ pointer
Make ‘new->data’ store ‘dat’
Make ‘new->next’ store NULL //temporary

If ‘head’ and ‘rear’ points to NULL:


Make ‘head’ & ‘rear’ point to ‘new’
Else:
Make ‘rear->next’ point to ‘new’
Make ‘rear’ as ‘new’

Make ‘rear->next’ point to ‘head’ //circular implementation

Function Dequeue():

If ‘head’ points to NULL:


Print “Underflow”
Return

Assign a ‘temp’ variable of type Node* point to ‘head’


If ‘head’ is Equal to ‘rear’:
Make ‘head’ and ‘rear’ point to NULL
Else:
Make ‘head’ point to ‘head->next’
Make ‘rear->next’ point to current ‘head’

Free the dynamically allocated storage in ‘temp’

Function Display():

If ‘head’ points to NULL:


Print “Underflow”
Return
Assign a ‘temp’ variable of type Node* point to ‘head’
While (true):
Print ‘temp->data’
Make ‘temp’ point to ‘temp->next’

If ‘temp’ is Equal to ‘head’:


Break

Function Discard():
While head is NOT NULL:
Call Dequeue()

/* Make a driver code asking from the user each time to choose the
function */

OUTPUT:

You might also like