0% found this document useful (0 votes)
83 views

Aim: C++ Program To Implement Linked List and Its Operations Algorithm: Step 1

The document describes algorithms for implementing linked lists and stacks in C++. For linked lists, it explains how to create a list by dynamically allocating nodes, insert nodes at different positions, and delete a node. For stacks, it explains how to initialize an array-based stack, push items onto the stack when there is available space, and pop items off the stack by retrieving the top item and decrementing the pointer. The algorithms provide step-by-step instructions for common linked list and stack operations in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Aim: C++ Program To Implement Linked List and Its Operations Algorithm: Step 1

The document describes algorithms for implementing linked lists and stacks in C++. For linked lists, it explains how to create a list by dynamically allocating nodes, insert nodes at different positions, and delete a node. For stacks, it explains how to initialize an array-based stack, push items onto the stack when there is available space, and pop items off the stack by retrieving the top item and decrementing the pointer. The algorithms provide step-by-step instructions for common linked list and stack operations in C++.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Aim : C++ Program to implement Linked List and its operations

Algorithm :
Step 1 : Function for Creation of Linked List: In this function a linked list of nodes
is created. The list is pointed by pointer ‘first’, the last node of the list points
to ‘Null’., indicating the end of the list
(a) : Each node is having two parts ‘Data’ and ‘Link’. Let us assume that a linked
list of N number of nodes is to be created. The operator ‘new’ will be used
for the dynamic allocation of node. A variable ‘count’ is being used as a
counter to count the number of nodes in the created list
(b) : Create the 1st node of the list pointed by ‘first’
(c) : Read the data and store into ‘Data’ part
(d) : Link part, connect the next node and shift the pointer to the next node
(e) : Set the pointer ‘last’ to the last node.
(f) : End the function
Step 2 : Function for displaying the list: In this function a linked list, pointed by
first, is displayed. The number of nodes in the list is also counted during the
creation. A pointer ‘ptr’ is being used to visit the various nodes in the list. A
variable count is used to keep track of the number of nodes printed during
the display function. The display function end when a null is encountered
(a) : If list is empty, print the ‘List Empty’
(b) : Initialize count = 0
(c) : Point ‘ptr’ to the 1st node
(d) : Whil ‘ptr’ is null repeat the steps (e) and (f)
(e) : ptr to the next node
(f) : print the node
(g) : End of the display function
Step 3 : Function for Insertion: In this function a node ‘newnode’ is inserted at the
beginning, middle and end of the node. The linked list is being pointed by a
pointer ‘first’ at the beginning.
(a) : Insert as first node: Create newnode and read the data
(b) : set the link of the node as first
(c) : set the pointer ‘first’ to ‘newnode’
(d) : Insert after a node: A pointer ‘ptr’ travels the list in such a way that each
visited node is checked for data part equal to ‘value of the node’. If such a
node is found then node ‘newnode’ is inserted
Step 4 : Function for deleting a node from a linked list: A delete operations
involves the following two steps:
: (1) Search the list for the node which is to be deleted
: (2) Delete the node
(a) : The list be a pointer to a linked list. In this function a node with the data
value and the position it stored. Pointer ‘ptr’ stored the address of ‘first’
node and visited to each node is checked for the inserted position. If such a
node is found then ‘ptr’ points to the selected node and it shall be deleted
(b) : ptr = first,
(c) : while (ptr!=NULL) & (position = inserted position) shift the ptr
(d) : delete of ptr
Step 5 : End the algorithm
Aim : C++ Program to implement STACK and its operations
Algorithm
Step 1 : Initially TOP point 0, index of elements in stack is start from 1, and index
of last element is MAX
Step 2 : INIT_STACK (STACK, TOP)
(a) : Algorithm to initialize a stack using array.
(b) : TOP points to the top-most element of stack.
(c) : TOP: = 0;
(d) : Exit
Step 3 : PUSH_STACK (STACK, TOP, MAX, ITEM)
(a) : Push operation is used to insert an element into stack
(b) : IF TOP = MAX then
(c) : Print “Stack is full”; Exit;
(d) : Otherwise TOP = TOP + 1; /*increment TOP*/
(e) : STACK (TOP) = ITEM;
(f) : End of IF
(g) : Exit
Step 4 : Pop operating is used to remove an item from stack, first get the element
and then decrease TOP pointer
(a) : POP_STACK (STACK, TOP, ITEM)
(b) : IF TOP = 0 then
(c) : Print “Stack is empty”; Exit;
(d) : Otherwise ITEM: =STACK (TOP); TOP:=TOP – 1;
(e) : End of IF
(f) : Exit
Step 5 : End the algorithm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy