Lab Manual: CSC-216 (L6) - Data Structures
Lab Manual: CSC-216 (L6) - Data Structures
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.
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.
DVM Example
DVM for Two dimensional Array
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