0% found this document useful (0 votes)
4 views2 pages

TP - Stacks and Queues

The document outlines exercises for implementing a stack using an array and a queue using a linked list. It specifies operations for both data structures, including pushing, popping, peeking, and checking sizes for the stack, as well as enqueueing, dequeueing, and checking sizes for the queue. The document also includes test cases to validate the implementations of both data structures.

Uploaded by

atarhini748
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)
4 views2 pages

TP - Stacks and Queues

The document outlines exercises for implementing a stack using an array and a queue using a linked list. It specifies operations for both data structures, including pushing, popping, peeking, and checking sizes for the stack, as well as enqueueing, dequeueing, and checking sizes for the queue. The document also includes test cases to validate the implementations of both data structures.

Uploaded by

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

Stacks and queues

Exercise 1:
Implement a stack data structure using an array and provide the following operations:

1. `push(element)`: Adds an element to the top of the stack.


2. `pop()`: Removes and returns the element from the top of the stack.
3. `peek()`: Returns the element at the top of the stack without removing it.
4. `isEmpty()`: Returns true if the stack is empty, false otherwise.
5. `size()`: Returns the number of elements in the stack.

Write a program to implement the stack data structure using an array. Test your
implementation by performing the following operations:

1. Create an empty stack.


2. Push the elements 10, 20, and 30 onto the stack.
3. Print the size of the stack.
4. Check if the stack is empty and print the result.
5. Pop an element from the stack and print it.
6. Peek at the top element of the stack and print it.
7. Push the element 40 onto the stack.
8. Print the size of the stack again.

Exercise 2:
Write a program to implement the queue data structure using a linked list. Test your
implementation by performing the following operations:
1. Create an empty queue.
2. Enqueue the elements "apple", "banana", and "cherry" into the queue.
3. Print the size of the queue.
4. Check if the queue is empty and print the result.
5. Dequeue an element from the queue and print it.
6. Peek at the front element of the queue and print it.
7. Enqueue the element "date" into the queue.
8. Print the size of the queue again.

You might also like