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

Data structures-Lab-Programs-II BCA A - PROGRAM 3

Data structures program

Uploaded by

8122826049s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Data structures-Lab-Programs-II BCA A - PROGRAM 3

Data structures program

Uploaded by

8122826049s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

STUDY MATERIAL

ON

DATA STRUCTURES PRACTICALS


(University of Madras-syllabus 2023-2024)

BCA II YEAR – III SEMESTER

2024

Material Prepared

By

G.T. BAKYARAJ, B.Com., MCA., MBA., MFT.,


Assistant Professor
Department of Computer Science
Peri College of Arts and Science, Mannivakkam, Chennai-600048.
PROGRAM 3

Aim: Write a C++ program for Linked list implementation of Stack.

Algorithm:

1. Initialization

 Include necessary headers (iostream).


 Define the Node structure with data, address (void pointer), and link fields.
 Initialize a global pointer top to NULL (representing an empty stack).

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:

#include <iostream> // Include header for input/output operations


using namespace std; // Use the standard namespace to avoid writing std::

// 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

// Function to check if the stack is empty


bool isEmpty() {
if (top == NULL) { // Check if the top pointer is NULL
return true; // Return true if the stack is empty
} else {
return false; // Return false if the stack is not empty
}
}

// Function to push a new element onto the stack


void push(int value) {
Node* ptr = new Node(); // Allocate memory for a new node
ptr->data = value; // Assign the value to the data field of the new node
ptr->address = &(ptr->data); // Get the address of the data and store it in the address field
ptr->link = top; // Set the link of the new node to the current top node
top = ptr; // Update the top pointer to point to the new node
}

// Function to pop an element from the stack


void pop() {
if (isEmpty()) { // Check if the stack is empty
cout << "Stack is Empty" << endl;
} else {
Node* ptr = top; // Store the top node in a temporary pointer
top = top->link; // Update the top pointer to point to the next node
delete(ptr); // Deallocate memory of the popped node
}
}

// Function to display the element at the top of the stack


void showTop() {
if (isEmpty()) { // Check if the stack is empty
cout << "Stack is Empty" << endl;
} else {
cout << "Element at top is : " << top->data << " (Address: " << top->address << ")" <<
endl;
}
}

// Function to display all elements in the stack


void displayStack() {
if (isEmpty()) { // Check if the stack is empty
cout << "Stack is Empty" << endl;
} else {
Node* temp = top; // Create a temporary pointer to traverse the stack
while (temp != NULL) { // Loop until the end of the stack
cout << temp->data << " (Address: " << temp->address << ") ";
temp = temp->link; // Move to the next node
}
cout << endl;
}
}
// Main function to handle user interaction
int main() {

int choice, flag = 1, value;

// Menu Driven Program using Switch


while (flag == 1) {
cout << "\n1.Push \n2.Pop \n3.Display first element \n 4.display linked list address
\n5.exit\n";
cout << "---------------------------\n";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter Value:\n";
cin >> value;
push(value);
break;
case 2:
pop();
break;
case 3:
showTop();
break;
case 4:
displayStack();
break;
case 5:
flag = 0;
break;
}
}
}

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

---------------

300 (Address: 0x9bb608) 200 (Address: 0x9bb5f0) 100 (Address: 0x9b1050)

1.Push

2.Pop

3.show Top

4.displayStack

5.exit

---------------

-------

You might also like