Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Electrical Engineering Department - ITU

CS243L: Data Structures & Algorithms Lab

Course Instructor: Mr. Umer Farooq Dated: 18/10/2022

Lab Engineer: Muhammad Usama Riaz Semester: Fall 2022

Session: 2021-2024 Batch: BSEE2021

Lab 7. Implementing Stack using Linked List

Report Total
Name Roll number Scaled to 10
(out of 100) (out of 10)

SUMAMA BIN RIAZ BSEE21028

Checked on: ____________________________

Signature: ____________________________
Objective
The objective of this lab is to provide the knowledge of basic data structures and their implementations.

Equipment and Component


Component Description Value Quantity
Computer Available in lab 1

Conduct of Lab
1. Students are required to perform this experiment individually.
2. In case the lab experiment is not understood, the students are advised to seek help from
the course instructor, lab engineers, assigned teaching assistants (TA) and lab attendants.

Theory and Background


In computer science, a linked list is a linear collection of data elements whose order is not given
by their physical placement in memory. Instead, each element points to the next. It is a data
structure consisting of a collection of nodes which together represent a sequence.
A stack is a basic computer science data structure and can be defined in an abstract,
implementation-free manner, or it can be generally defined as a linear list of items in which all
additions and deletion are restricted to one end that is Top. Stack is a linear data structure which
follows a particular order in which the operations are performed. The order may be LIFO (Last
in First Out) or FILO (First in Last Out).
Lab Tasks
Task 1
Implement the MyStack class using Linked List and perform these operation/functions:
// Add function to insert an element in stack
void push (int)
{
}
// Add function to remove an element from stack
void pop ()
{
}
// Add function to show the element at the top of the stack
void showTop ()
{
}
// Add function to check if stack is empty
void isEmpty ()
{
}

// Add function to display stack elements


void displayStack ()
{
}

#include <iostream>
using namespace std;

struct node{
int data;
node* next;
node(){
next = nullptr;
}
};

class stack{
node* head;
public:
void pop(){
node* t = head;
head = head->next;
free(t);
}
void init(int x)
{
node* t = new node();
t->data = x;
t->next = head;
head = t;
}
int top(){
return head->data;
}
bool is_empty(){
return head == nullptr;
}
void disp(){
node* curr = head;
while(curr != nullptr){
cout << curr->data << " ";
curr = curr->next;
}
}
};

int main(){
int n, x;
cout << "Enter size of stack: ";
cin >> n;
stack st;
cout << "Is stack empty: ";
st.is_empty() ? cout << "Empty\n" : cout << "Not empty\n";
for(int i = 1; i <= n; i++){
cout << "Enter stack element: "; cin >> x;
st.init(x);
}
cout << "Popping the " << x << " from stack: \n";
st.pop();
cout << "Now seeing the top of stack: " << st.top() << endl;
cout << "Now displaying full stack: " << endl;
st.disp();
cout << "Is stack empty: ";
st.is_empty() ? cout << "\nEmpty\n" : cout << "\nNot empty\n";
cout << "Do you want to pop all elements: 1 to proceed 0 to exit: ";
bool p;
cin >> p;
if(p){ for(int i = 1; i<= n-1; i++){ st.pop();} cout << "Is stack empty: "; st.is_empty() ? cout <<
"\nEmpty\n" : cout << "\nNot empty\n";}
else{ return 0;}
}
Assessment Rubric for Lab
Method for assessment:
Lab reports and instructor observation during lab sessions. Outcome assessed:
a. Ability to conduct experiments, as well as to analyze and interpret data (P) b. Ability to function on multi-disciplinary teams (A)
c. Ability to use the techniques, skills, and modern engineering tools necessary for engineering practice (P)
Performance Max Obtained
Task CLO Description Exceeds expectation Meets expectation Does not meet expectation
metric marks marks
Executes without errors Executes without errors, user
Does not execute due to syntax
excellent user prompts, prompts are understandable,
1. Realization errors, runtime errors, user
good use of symbols, minimum use of symbols or
of experiment 1 1 Functionality 40 prompts are misleading or non-
spacing in output. Through spacing in output. Some
(a) existent. No testing has been
testing has been completed testing has been completed
completed (0-19)
(35-40) (20-34)
Actively engages and Cooperates with other group
Distracts or discourages other
2. Teamwork Group cooperates with other group member(s) in a reasonable
1 3 5 group members from conducting
(b) Performance member(s) in effective manner but conduct can be
the experiment (0-1)
manner (4-5) improved (2-3)
On Spot Able to make changes (8- Partially able to make changes
3. Conducting 1 1 10 Unable to make changes (0-4)
Changes 10) (5-7)
experiment (a,
c) Answered all questions (8- Unable to answer all questions
1 1 Viva 10 Few incorrect answers (5-7)
10) (0-4)
4. Laboratory
Comments are added and Comments are added and
safety and Code
1 3 5 does help the reader to does not help the reader to Comments are not added (0-1)
disciplinary commenting
understand the code (4-5) understand the code (2-3)
rules (a)
Excellent use of white
space, creatively organized Includes name, and
Poor use of white space
work, excellent use of assignment, white space
5. Data (indentation, blank lines) making
1 3 Code Structure 5 variables and constants, makes the program fairly easy
collection (c) code hard to read, disorganized
correct identifiers for to read. Title, organized work,
and messy (0-1)
constants, No line-wrap (4- good use of variables (2-3)
5)
Solution is efficient, easy to A logical solution that is easy
6. Data A difficult and inefficient solution
1 4 Algorithm 20 understand, and maintain to follow but it is not the most
analysis (a, c) (0-5)
(15-20) efficient (6-14)
Documentation
7. Computer
1 2 & GitHub 5 Timely (4-5) Late (2-3) Not done (0-1)
use (c)
Submissions
Max Marks (total): 100 Obtained Marks (total):

Lab Engineer Signature: ________________________

You might also like