Data structures-Lab-Programs-II BCA A - PROGRAM 3
Data structures-Lab-Programs-II BCA A - PROGRAM 3
ON
2024
Material Prepared
By
Algorithm:
1. Initialization
2. Main Loop
Enter a loop that continues until the user chooses to exit (flag == 1).
Inside the loop:
o Present a menu with options for Push (1), Pop (2), Show Top (3), Display Stack
(4), and Exit (5).
o Get the user's choice.
o Use a switch statement to perform actions based on the choice:
Case 1 (Push):
Prompt the user to enter a value.
Call the push function with the entered value.
Case 2 (Pop):
Call the pop function to remove the top element.
Case 3 (Show Top):
Call the showTop function to display the data and address of the
top element.
Case 4 (Display Stack):
Call the displayStack function to traverse and print the data and
addresses of all elements in the stack.
Case 5 (Exit):
Set the flag to 0 to exit the loop.
3. Helper Functions
isEmpty: Checks if the top pointer is NULL, indicating an empty stack. Returns true for
empty, false otherwise.
push:
o Allocates memory for a new node.
o Assigns the user-provided value to the data field.
o Sets the address field to point to the memory location of the data.
o Sets the link field of the new node to the current top node.
o Updates the top pointer to point to the newly created node.
pop:
o Checks if the stack is empty. If empty, displays an error message.
o Otherwise, stores the current top node in a temporary pointer.
o Updates the top pointer to point to the next node (effectively removing the top
element).
o Deallocates the memory of the popped node using delete.
showTop:
o Checks if the stack is empty. If empty, displays an error message.
o Otherwise, displays the data and address of the element at the top of the stack
(pointed to by top).
displayStack:
o Checks if the stack is empty. If empty, displays an error message.
o Otherwise, creates a temporary pointer to traverse the stack.
o Iterates through the stack using the link field until the temporary pointer reaches
NULL (end of the stack).
o Within the loop, prints the data and address of the current node pointed to by the
temporary pointer.
o Moves the temporary pointer to the next node in the stack using the link field.
4. Program Termination
Exit the loop when the user chooses to exit (flag becomes 0).
SOURCE CODE:
// Structure of the Node with an additional field for storing the address
struct Node {
int data; // Integer data stored in the node
void* address; // Pointer to void to hold any memory address of the data
Node* link; // Pointer to the next node in the stack
};
Node* top = NULL; // Global pointer to the top node of the stack, initialized as NULL
OUTPUT:
1.Push
2.Pop
3.show Top
4.displayStack
5.exit
---------------
Enter Value:
100
1.Push
2.Pop
3.show Top
4.displayStack
5.exit
---------------
Enter Value:
200
1.Push
2.Pop
3.show Top
4.displayStack
5.exit
---------------
Enter Value:
300
1.Push
2.Pop
3.show Top
4.displayStack
5.exit
---------------
1.Push
2.Pop
3.show Top
4.displayStack
5.exit
---------------
-------