Stacks Program1 and 2 Explained
Stacks Program1 and 2 Explained
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int top;
} Stack;
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
return 0;
}
This C program implements a stack using an array. It defines a `Stack` structure with an array `data` to store
the stack elements and an integer `top` to keep track of the top element of the stack.
1. **Include necessary header files:** The program includes `stdio.h` and `stdlib.h` for standard
input/output operations and dynamic memory allocation.
2. **Define constants:** The program defines a constant `MAX_SIZE` to specify the maximum size of the
stack.
3. **Define the Stack structure:** The program defines a `Stack` structure using a typedef. The structure
contains an array `data` to store the stack elements and an integer `top` to keep track of the top element.
4. **Function definitions:**
- `push`: Takes a pointer to a `Stack` (`Stack* stack`) and an integer `element` as arguments. It checks if
the stack is full (`top` is equal to `MAX_SIZE - 1`) and prints "Stack Overflow" if so. Otherwise, it increments
`top` and adds the `element` to the `data` array.
- `pop`: Takes a pointer to a `Stack` (`Stack* stack`) as an argument. It checks if the stack is empty (`top`
is -1) and prints "Stack Underflow" if so. Otherwise, it returns the element at the top of the stack and
decrements `top`.
- `peek`: Takes a pointer to a `Stack` (`Stack* stack`) as an argument. It checks if the stack is empty (`top`
is -1) and prints "Stack is empty" if so. Otherwise, it returns the element at the top of the stack without
removing it.
5. **Main function:**
- Declares a `Stack` variable `stack` and initializes its `top` to -1.
- Pushes three elements (1, 2, and 3) onto the stack using the `push` function.
- Prints the top element of the stack using the `peek` function.
- Pops the top element of the stack using the `pop` function and prints it.
- Prints the new top element of the stack using the `peek` function.
This program demonstrates the basic operations of a stack (push, pop, and peek) using an array-based
implementation.
Linked List-based Stack Implementation:
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct {
Node* top;
} Stack;
int main() {
Stack stack;
stack.top = NULL;
push(&stack, 1);
push(&stack, 2);
push(&stack, 3);
return 0;
}
This C program implements a stack using a linked list. The stack is represented by a
linked list where each node contains the data and a pointer to the next node.
1. Include necessary header files: The program includes stdlib.h for dynamic
memory allocation.
2. Define structures:
• Node : Represents a node in the linked list. It contains an integer data to
store the element and a pointer next to point to the next node in the list.
• Stack : Represents the stack. It contains a pointer top that points to the top
node of the stack.
3. Function definitions:
• push : Takes a pointer to a Stack (Stack* stack ) and an integer element as
arguments. It allocates memory for a new node, assigns the element to its
data, sets its next pointer to the current top node, and updates the top
pointer to point to the new node.
• pop : Takes a pointer to a Stack (Stack* stack ) as an argument. It checks if
the stack is empty (top is NULL) and prints "Stack Underflow" if so.
Otherwise, it removes the top node from the stack, frees the memory, and
returns the data of the removed node.
• peek : Takes a pointer to a Stack (Stack* stack ) as an argument. It checks if
the stack is empty (top is NULL) and prints "Stack is empty" if so. Otherwise,
it returns the data of the top node without removing it.
4. Main function:
• Declares a Stack variable stack and initializes its top pointer to NULL .
• Pushes three elements (1, 2, and 3) onto the stack using the push function.
• Prints the top element of the stack using the peek function.
• Pops the top element of the stack using the pop function and prints it.
• Prints the new top element of the stack using the peek function.
This program demonstrates the implementation of a stack using a linked list, which
allows for dynamic allocation and deallocation of memory for stack elements.
This C program implements a stack using a linked list. The stack is represented by a linked list where each
node contains the data and a pointer to the next node.
1. **Include necessary header files:** The program includes `stdlib.h` for dynamic memory allocation.
2. **Define structures:**
- `Node`: Represents a node in the linked list. It contains an integer `data` to store the element and a
pointer `next` to point to the next node in the list.
- `Stack`: Represents the stack. It contains a pointer `top` that points to the top node of the stack.
3. **Function definitions:**
- `push`: Takes a pointer to a `Stack` (`Stack* stack`) and an integer `element` as arguments. It allocates
memory for a new node, assigns the `element` to its `data`, sets its `next` pointer to the current `top` node,
and updates the `top` pointer to point to the new node.
- `pop`: Takes a pointer to a `Stack` (`Stack* stack`) as an argument. It checks if the stack is empty (`top`
is `NULL`) and prints "Stack Underflow" if so. Otherwise, it removes the top node from the stack, frees the
memory, and returns the data of the removed node.
- `peek`: Takes a pointer to a `Stack` (`Stack* stack`) as an argument. It checks if the stack is empty (`top`
is `NULL`) and prints "Stack is empty" if so. Otherwise, it returns the data of the top node without removing
it.
4. **Main function:**
- Declares a `Stack` variable `stack` and initializes its `top` pointer to `NULL`.
- Pushes three elements (1, 2, and 3) onto the stack using the `push` function.
- Prints the top element of the stack using the `peek` function.
- Pops the top element of the stack using the `pop` function and prints it.
- Prints the new top element of the stack using the `peek` function.
This program demonstrates the implementation of a stack using a linked list, which allows for dynamic
allocation and deallocation of memory for stack elements.