DS Lab 8 - Linked List Implementation As Stack and Queue
DS Lab 8 - Linked List Implementation As Stack and Queue
Table of Contents
1. Introduction............................................................................................................................................................3
1.1 Linked Lists as Stack.............................................................................................................................................3
1.2 Linked Lists as Queue............................................................................................................................................3
1.3Relevant Lecture Readings..........................................................................................................................................4
2. Activity Time boxing.............................................................................................................................................4
3. Objectives of the experiment.................................................................................................................................4
4. Concept Map..........................................................................................................................................................4
4.1 Linked Lists as Stack in C++......................................................................................................................................4
4.2Linked Lists as Queue in C++......................................................................................................................................5
5. Homework before Lab.........................................................................................................................................................7
5.1 Problem Solution Modeling........................................................................................................................................7
5.2 Problem description:...................................................................................................................................................7
5.3Practices from home.....................................................................................................................................................7
5.3.1 Task-1......................................................................................................................................................................7
5.3.2 Task-2......................................................................................................................................................................7
6. Procedure& Tools................................................................................................................................................................8
6.1 Tools............................................................................................................................................................................8
6.2 Walk through Tasks [Expected time = 25mins]..............................................................................................8
7. Practice Tasks....................................................................................................................................................................10
7.1 Practice Task 1 [Expected time = 30mins]..................................................................................................10
7.2 Practice Task 2 [Expected time = 30mins]..................................................................................................10
7.3Out comes...................................................................................................................................................................10
7.4Testing........................................................................................................................................................................10
8.Evaluation Task (Unseen)..................................................................................................................................................11
[Expected time = 60mins for two tasks]................................................................................................................................11
9. Evaluation criteria.............................................................................................................................................................12
10. Further Readings.............................................................................................................................................................12
10.1 Web sites related to C++ tutorials about linked list implementation of stack and queues.....................................12
10.2 Web sites containing supporting material...............................................................................................................12
1. Introduction
Objective of this lab is to make you understand the usage of linked list structure for
implementation of stack and queue ADT (Abstract Data Types). If we need to maintain a stack whose
individual elements are allocated memory space during program execution, we need to use linked list for
that purpose, same is case for queue. In case of array based stacks and queues we may only store finite
number of elements which will be limited to maximum size of array.
Stack can be implemented by linked lists structure. In this case we need to maintain a pointer to
node of linked list which always point to a node before the top of stack, when an element is inserted in list
it is placed at top of stack and when an element is removed it is also removed from top of stack. In this
way stacks can be used by linked list structures. As memory of elements of stack is dynamically allocated
so stack will never be filled (we don’t need to check for stack overflow conditions). Figure 1 provides
information about the use of stack as a linked list structure.
Because the size of the array to stock up the queue elements is fixed, only a finite number of queue
elements can be stored in the array. Also, the array implementation of the queue requires the array to be
handled in a special way together with the values of the indices queueFront and queueRear. The linked list
implementation of a queue simplifies many of the special cases of the array implementation and, because
the memory to store a queue element is allocated dynamically, the queue is never full. It means we don’t
need to check for queue overflow condition, we need to maintain only queue underflow condition that is
we need to check whether queue is empty or not while removing an element from the queue.
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in lab.
4.1 Linked Lists as Stack in C++
The first operation that isto be considered is the default constructor. The default constructor initializes the
stack to an empty state when an object of stack class is declared. Thus, this function sets stackTop pointer
to NULL. The definition of this function is as follows:
template<class Type>
linkedStackType<Type>::linkedStackType()
{
stackTop = NULL;
}
The operations isEmptyStack and isFullStack are easy to understand. The stack is empty if stackTop is
NULL. Also, because the memory for elements of stack is allocated and deallocated dynamically, the
stack is never full. Thus, the function isFullStack always returns the value false. The definitions of the
functions to implement these operations are asfollows:
template<class Type>
bool linkedStackType<Type>::isEmptyStack() const
{
return(stackTop == NULL);
} //end isEmptyStack
template<class Type>
Department of Computer Science Page 4
IIU, Islamabad
Lab 8: Linked Lists as Stack and Queue
template<class Type>
void linkedStackType<Type>::push(const Type &newElement)
{
nodeType<Type> *newNode; //pointer to create the new node
newNode = new nodeType<Type>; //create the node
newNode->info = newElement; //store newElement in the node
newNode->link = stackTop; //insert newNode before stackTop
stackTop = newNode; //set stackTop to point to the
//top node
} //end push
template<class Type>
void linkedStackType<Type>::pop()
{
nodeType<Type> *temp; //pointer to deallocate memory
if(stackTop != NULL)
{
temp = stackTop; //set temp to point to the top node
stackTop = stackTop->link; //advance stackTop to the
//next node
delete temp; //delete the top node
}
else
cout<< "Cannot remove from an empty stack." <<endl;
}//end pop
4.2Linked Lists as Queue in C++
Some operations related to implementation of queue as a linked list are provided in this section.
The queue is empty if queueFront pointer is NULL. Memory to store the queue elements is allocated
dynamically. Therefore, the queue is never full and so the function to implement the isFullQueue
operation returns the value false. Implementation of functions for checking empty or full queue is given
below:
template<class Type>
boollinkedQueueType<Type>::isEmptyQueue() const
{
return(queueFront == NULL);
} //end
template<class Type>
Department of Computer Science Page 5
IIU, Islamabad
Lab 8: Linked Lists as Stack and Queue
The addQueue operation inserts a new element at the rear of the queue. To implement this operation, we
will use the pointer queueRear. If the queue is non-empty, the operation front returns the first element of
the queue and therefore the element of the queue indicated by the pointer queueFront is returned. If
thequeue is empty, the function front stops execution of program. If the queue is nonempty, the operation
back returns the last element of the queue and so the element of the queue indicated by the pointer
queueRear is returned. If the queue isempty, the function back stops the execution of program. Similarly,
if the queue is nonempty, the operation deleteQueue removes the first element of the queue, and so we
access the pointer queueFront for this purpose. The definitions of the functions to implement these
operations are as follows:
template<class Type>
void linkedQueueType<Type>::addQueue(const Type&newElement)
{
nodeType<Type> *newNode;
newNode = new nodeType<Type>; //create the node
newNode->info = newElement; //store the info
newNode->link = NULL; //initialize the link field to NULL
if (queueFront == NULL) //if initially the queue is empty
{
queueFront = newNode;
queueRear = newNode;
}
else //add newNode at the end
{
queueRear->link = newNode;
queueRear = queueRear->link;
}
}//end addQueue
template<class Type>
Type linkedQueueType<Type>::front() const
{
assert(queueFront != NULL);
return queueFront->info;
} //end front
template<class Type>
Type linkedQueueType<Type>::back() const
{
assert(queueRear!= NULL);
return queueRear->info;
} //end back
template<class Type>
void linkedQueueType<Type>::deleteQueue()
{
nodeType<Type> *temp;
if (!isEmptyQueue())
{
temp = queueFront; //make temp point to the first node
queueFront = queueFront->link; //advance queueFront
delete temp; //delete the first node
if (queueFront == NULL) //if after deletion the
//queue is empty
queueRear = NULL; //set queueRear to NULL
}
else
cout<< "Cannot remove from an empty queue" <<endl;
}//end deleteQueue
6.1 Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.
6.2 Walk through Tasks [Expected time =
25mins]
Following screens in figure 2, 3 and 4 represent source code implementation of a linear linked list
classas a stack in Microsoft Visual Studio 2017. You are required to type, debug and execute this program
in Microsoft Visual Studio 2017.
This linked list implementation of stack program provides functionality to use a stack. It contains a
structure to represent the nodes of linked list and a class named “stack” which will be representing the
stack as linked list. This class contains constructor, push, pop and display member functions. In main
function we create an object of stack class and call member functions of class to push and pop elements
from the stack.
7. Practice Tasks
Department of Computer Science Page 9
IIU, Islamabad
Lab 8: Linked Lists as Stack and Queue
This section will provide information about the practice tasks which are required to be performed in lab
session. Design solutions of problems discussed in each task and placesolution code in a folder specified
by your lab instructor.
Lab instructors are guided to help students learn how ACM problems work and provide students
with certain practice/home tasks on the pattern of ACM Problems.
Customer name
Address
Payment of order
7.3Out comes
After completing this lab, student will be able to understand and develop programs related to linked list
implementation of stacks and queuesin C++ using Microsoft Visual Studio 2017 environment.
7.4Testing
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
Department of Computer Science Page 11
IIU, Islamabad
Lab 8: Linked Lists as Stack and Queue
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student has
finished the complete/partial task(s).