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

[CSC10004 - DSA] Exercise 1 - Stack and Queue

Uploaded by

lenhatthanh1004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

[CSC10004 - DSA] Exercise 1 - Stack and Queue

Uploaded by

lenhatthanh1004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Exercise 1 Data Structures and Algorithms CSC10004

Exercise 1

Implementing Stack and Queue from scratch

1 Description
Stack and Queue are two fundamental data structures in programming. Stack allows adding and
retrieving elements according to the LIFO (Last In, First Out) principle, while Queue allows adding
and retrieving elements according to the FIFO (First In, First Out) principle. In this Exercise 1,
you are required to implement these two data structures, using struct and template.

1.1 Stack (Array version)


Implement a stack using array with the following variables:

• items: data array.

• top: index of the top item (-1 if stack is empty).

• maxSize: maximum size of the stack, non-negative integer.

• Other variables if necessary.

and functions:

• init: initialize an empty stack with the provided size.

• copyStack: initialize a stack from another stack.

• release: clean up the stack when not in use anymore.

• isEmpty: check the stack is empty or not.

• push: an operator to push a new item into stack. Print to the console if the stack is full.

• pop: an operator to pop the top item.

• topValue: get the value of the top item.

• print: print the stack to the console.

• Other functions if necessary.

University of Science Faculty of Information Technology Page 1


Exercise 1 Data Structures and Algorithms CSC10004

Please note that the program should be organized into (at least) 3 separate files, including stack.h
(containing the declaration of the stack), stack.cpp (containing the implementation for the stack),
and main.cpp (to use the stack as a library).
Figure 1 and Figure 2 below are examples of stack.h and stack.cpp files.

Figure 1: Example of header file for stack using array.

Figure 2: Example of implementation file for stack using array.

In main.cpp, create a menu to test the stack operators (pop and push). Run the program with
normal test cases, when the stack is empty, and when the stack is full. Capture the results and
include them in the report.

University of Science Faculty of Information Technology Page 2


Exercise 1 Data Structures and Algorithms CSC10004

1.2 Stack (Linked List version)


Implement a stack using linked list with the following variables:

• top: a pointer to the top node of the stack. The node here is a simple struct, including data
and a pointer to another node.
For example:
template <typename T>
struct Node
{
T data;
Node* next;
};

• Other variables if necessary.

and functions:

• init: initialize an empty stack.

• copyStack: initialize a stack from another stack.

• release: clean up the stack when not in use anymore.

• isEmpty: check the stack is empty or not.

• push: an operator to push a new item into stack. Print to the console if the stack is full.

• pop: an operator to pop the top item.

• topValue: get the value of the top item.

• print: print the stack to the console.

• Other functions if necessary.

Similar to when implementing the stack (array version), please organize the program into (at least)
3 separate files (stack.h, stack.cpp, and main.cpp). In main.cpp, create a menu to test the stack
operators (pop and push). Run the program with normal test cases, when the stack is empty, and
when the stack is full. Capture the results and include them in the report.

University of Science Faculty of Information Technology Page 3


Exercise 1 Data Structures and Algorithms CSC10004

Figure 3 below is an example of stack.h file for stack using linked list.

Figure 3: Example of header file for stack using linked list.

1.3 Queue (Array version)


Similar to requirement 1.1, please implement a queue using array and report carefully.

Figure 4: Example of header file for queue using array.

University of Science Faculty of Information Technology Page 4


Exercise 1 Data Structures and Algorithms CSC10004

1.4 Queue (Linked List version)


Similar to requirement 1.2, please implement a queue using linked list and report carefully.

Figure 5: Example of header file for queue using linked list.

1.5 Recursive versions


During the implementation of the above data structures, there will be functions that use loops.
Convert these loop functions into recursive form. Analyze the theoretical time complexity
and compare the actual execution time when using recursion and loops.
Please record the results in the report.

University of Science Faculty of Information Technology Page 5


Exercise 1 Data Structures and Algorithms CSC10004

2 Submission
Source code folder and Report file must be contributed in the form of a compressed file (.zip, .rar)
and named according to your Student ID. For example:
StudentID.zip
Source
Report.pdf

2.1 Source code


The source code folder contains programming files. For example:
Source
Stack Array
stack.h
stack.cpp
recursive stack.h
recursive stack.cpp
main.cpp
...
Stack Linked List
Queue Array
Queue Linked List

2.2 Report
The report must fully give the following information:

• Student information (Student ID, Full name, etc.).

• Self-evaluation.

• Detailed experiments.

• Exercise feedback (what you learned, what was difficult).

• The report needs to be well-formatted and exported to PDF. If there are figures cut off by
the page break, etc., points will be deducted.

• References (if any).

University of Science Faculty of Information Technology Page 6


Exercise 1 Data Structures and Algorithms CSC10004

3 Assessment
No. Details Score
1 Stack (Array version) 15%
2 Stack (Linked List version) 15%
3 Queue (Array version) 15%
4 Queue (Linked List version) 15%
5 Recursive versions 20%
6 Report 20%
Total 100%

4 Notices
Please pay attention to the following notices:

• This is an INDIVIDUAL assignment.

• Duration: about 2 weeks.

• Any plagiarism, any tricks, or any lie will have a 0 point for the course grade.

University of Science Faculty of Information Technology Page 7

You might also like