Вы находитесь на странице: 1из 8

Lab Manual

CSC-216(L6) – Data Structures


(Prepared by Haji Gul)

Course Objective
o Implement various data structures and their algorithms, and apply them in implementing simple applications.
o Analyze simple algorithms and determine their complexities.
o Apply the knowledge of data structures to other application domains.
o Design new data structures and algorithms to solve problems

Class Policy
1. A student must reach the class-room in time. Students late than 8 mins may join the class but are not entitled to be marked
present.
2. Attendance shall be marked at the start of the class and students failing to secure 75% attendance will not be allowed to sit
in the Final Exam.
3. Those who are absent on the announcement date of the assignment/test, must get the topic/chapter of the test/assignment
confirmed through their peers
Array Data Elements Size
o In general the size or number of data elements of the array can be computed from the index
set by the formula.

Accessing Methods for Array Index


Dope Vector Method

o DVM used for accessing of array elements. The DVM mathematical formula included
below through which we can access elements of an array.

o MA(i) = Sa + ( i - 1) * w

o MA  memory address.

o Sa  start address.

o I  subscript of an element to be accessed.

o w  is the word length for integer(w=2) and for character (w=1).

DVM Example
DVM for Two dimensional Array

Solution of DVM for 2D Array


Abstract Data Types
Abstract Data type (ADT) is a type (or class) for objects whose behaviour is defined by a set of
value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these
operations will be implemented. It does not specify how data will be organized in memory and
what algorithms will be used for implementing the operations. It is called “abstract” because it
gives an implementation-independent view.

Stack Data Structure (Introduction and Program)


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).
Mainly the following three basic operations are performed in the stack:
o Push: Adds an item in the stack. If the stack is full, then it is said to be an Overflow
condition.
o Pop: Removes an item from the stack. The items are popped in the reversed order in which
they are pushed. If the stack is empty, then it is said to be an Underflow condition.
o Peek or Top: Returns top element of stack.
o isEmpty: Returns true if stack is empty, else false.
How to understand a stack practically?
There are many real-life examples of a stack. Consider the simple example of plates stacked over
one another in a canteen. The plate which is at the top is the first one to be removed, i.e. the plate
which has been placed at the bottommost position remains in the stack for the longest period of
time. So, it can be simply seen to follow LIFO/FILO order.

Time Complexities of operations on stack:


push(), pop(), isEmpty() and peek() all take O(1) time. We do not run any loop in any of these
operations.

Applications of stack:
o Infix to postfix / prefix conversion.
o In Graph Algorithms like Topological sorting and Strongly Connected Component.

Implementation:
There are two ways to implement a stack:
o Using array
o Using linked list
Implementing Stack using Arrays:
Recommended: Please solve it on PRACTICE first, before moving on to the solution.
//C++ program to implement basic stack operations
#include <cstdlib> //use for dynamic memory management, random number generation etc
#include <iostream>
#define maxsize 5
void push();
void pop();
void display();
int stack[maxsize];
int top = -1;
using namespace std;
int main(int argc, char *argv[])
{
int choice;
char ch;
do
{
cout<<"\n 1. Push";
cout<<"\n 2. Pop";
cout<<"\n 3. Display";
cout<<"\n Enter your Choice : ";
cin>>choice;
cout<<"\n";
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
default: cout<<"\n You entered an invalid choice.....\n";
}
cout<<"\n Do you wish to perform another operation (y/n)?";
cin>>ch;
}while(ch == 'y' || ch == 'Y');
system("PAUSE");
return EXIT_SUCCESS;
}

void push()
{
int item;
if(top == maxsize-1)
{
cout<<"\nThe Stack is Full....";
}
else
{
cout<<"\nEnter the Item to be pushed : ";
cin>>item;
top = top + 1;
stack[top]= item;
cout<<"\nElement pushed successfuly.....\n";
}
}

void pop()
{
int item;
if(top == -1)
{
cout<<"\nThe Stack is empty, No element to Pop.....\n";
}
else
{
item = stack[top];
top = top -1;
cout<<item<<" is poped from the stack Successfuly....\n";
}
}

void display()
{
int i;
if(top == -1)
{
cout<<"The Stack is Empty, no element to display......\n";
}
else
{
for(i=top; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}
}
Stack implementation Using linked list?
Help and detail source link:
o “https://www.geeksforgeeks.org/c-programs-gq/stack-queue-cc-programs-gq/”
o Data Structures and Algorithm Analysis in C++ by Mark Allen Weiss
o Data Structure and Algorithm in C++ by Adam Drozdek

Вам также может понравиться