call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
Data structure
1. What is
Array , Stack and Queue ??
Presented By: Presented To:
Adeel Kamran 03 Dr.Sajid Ali
Ali Raza Sheikh 04
Ali Raza 07
University of Educatuon Lahore D.G Khan
Campus
1
3. What is Linear Data Structure
In linear data structure, data is arranged in
linear sequence.
Data items can be traversed in a single
run.
In linear data structure elements are
accessed or placed in contiguous(together in
sequence) memory location.
University of Educatuon Lahore D.G Khan Campus 3
5. Array
• Array is type of linear data Structure
• In array that store the same data type having different index location
• Array index is start from zero
• Array having fix size
University of Educatuon Lahore D.G Khan Campus 5
6. Array Examples
• In real world array is widely use in cloth making
• Array of people in library
• Array of process
University of Educatuon Lahore D.G Khan Campus 6
7. Array operations
• Insert()
to add element to array
• Search()
to find element in array
• Update()
to update existing element in an array
University of Educatuon Lahore D.G Khan Campus 7
8. C++ implementation of Array insertion
#include<iostream>
using namespace std;
int main(){
int arr[]={10,20,30,40,50};
int item = 25;
int k = 0;
int n=5;
int j=n;
for(int i=0;i<5;i++){
cout<<"value at index = "<<i<<" "<<arr[i]<<endl;
}
University of Educatuon Lahore D.G Khan Campus 8
9. void update()
{
int value;
int loc;
cout<<"Enter Element for update in array ";
cin>>value;
cout<<"Enter location in an array (0-4) ";
cin>>loc;
arr[loc] = value;
n=n+1;
cout<<"Enter Element of you want to add = ";
cin>>item;
cout<<"Enter location which you want of add elemnt =
";
cin>>location;
while(j>=location){
arr[j+1]=arr[j];
j--;
}
arr[location]=item;
for(int i=0;i<6;i++){
cout<<"Value ="<<i<<" "<<arr[i]<<endl;
}
update();
search();
}//end of main
University of Educatuon Lahore D.G Khan Campus 9
10. Void search(){
cout<<"Enter value fo search = ";
cin>>item;
for(i =0;i<5;i++){
if(arr[i]==item){
key = true;
break;
}
}
if(key==true){
cout<<"value found at index "<<i;
}
else{
cout<<"value not found ";
}
}
University of Educatuon Lahore D.G Khan Campus 10
12. WHAT Is
A stack is called a last-in-first-out (LIFO)
collection. This means that the last thing we
added (pushed) is the first thing that gets
pulled (popped) off.
• A stack is a sequence of items that are
accessible at only one end of the sequence.
University of Educatuon Lahore D.G Khan Campus 12
14. Operations that can be performed on
STACK:
PUSH.
POP.
University of Educatuon Lahore D.G Khan Campus 14
15. Last In First Out
B
A
D
C
B
A
C
B
A
E
D
C
B
A
top
top
top
top
top
A
University of Educatuon Lahore D.G Khan Campus 15
16. Array-based Stack Implementation
• Allocate an array of some size (pre-defined)
• Maximum N elements in stack
• Bottom stack element stored at element 0
• last index in the array is the top
• Increment top when one element is pushed, decrement after pop
University of Educatuon Lahore D.G Khan Campus 16
17. #include<iostream>
#include<conio.h>
#define SIZE 10
using namespace std;
static int top=-1;
class stack
{
private:
int ar[SIZE];
public:
void push(int item);
void pop();
void peep();
};// end of class
void stack::push(int item)
{
if(top==SIZE-1)
cout<<"nThe Stack is Full!!!";
else
{ ar[++top]=item;
cout<<"nElement succesfully pushed in the Stack!!!";
}
}// end of void push
void stack::pop()
{
if(top<0)
cout<<"nStack Under flow!!!";
else
{ top--;
cout<<"nElement sucessfully popped from the Stack!!!";
}
}// end of void pop
void stack::peep()
{ if(top<0)
cout<<"nThe Stack is Empty it cannot be Peeped!!!";
else
for(int i=top;i>=0;i--)
cout<<ar[i]<<" ";
}// end of void peep
University of Educatuon Lahore D.G Khan Campus 17
18. int main()
{
char choice;
int ch,num;
stack ob;
do
{
cout<<"nntttS T A C K O P E R A T I O N S";
cout<<"nttt-------------------------------";
cout<<"nn1.PUSH";
cout<<"n2.POP";
cout<<"n3.PEEP";
cout<<"n4.EXIT";
cout<<"nnEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"nEnter the Element you want to Push:";
cin>>num;
ob.push(num);
break;// end of case1
case 2: ob.pop();
break;// end of case2
case 3: ob.peep();
break;// end of case3
case 4: exit(0);// end of case4
default: cout<<"nPlease Enter a Valid Choice(1-4)!!!";
}// end of switch
cout<<"nDo you want to Continue(Y/N):";
cin>>choice;
}// end of do
while(choice=='y' || choice=='y');
getch();
}// end of main
University of Educatuon Lahore D.G Khan Campus 18
19. The Towers of Hanoi
A Stack-based Application
• GIVEN: three poles
• a set of discs on the first pole, discs of different sizes, the smallest
discs at the top
• GOAL: move all the discs from the left pole to the right one.
• CONDITIONS: only one disc may be moved at a time.
• A disc can be placed either on an empty pole or on top of a larger
disc.
University of Educatuon Lahore D.G Khan Campus 19
24. APPLICATIONS OF STACKS ARE:
I. Reversing Strings:
• A simple application of stack is reversing strings.
To reverse a string , the characters of string are
pushed onto the stack one by one as the string
is read from left to right.
• Once all the characters
of string are pushed onto stack, they are
popped one by one. Since the character last
pushed in comes out first, subsequent pop
operation results in the reversal of the string.
University of Educatuon Lahore D.G Khan Campus 24
25. For example:
To reverse the string ‘REVERSE’ the string is
read from left to right and its characters are
pushed . LIKE:
onto a stack.
University of Educatuon Lahore D.G Khan Campus 25
26. II. Checking the validity of an expression
containing nested parenthesis:
• Stacks are also used to check whether a given
arithmetic expressions containing nested
parenthesis is properly parenthesized.
• The program for checking the validity of an
expression verifies that for each left
parenthesis
braces or bracket ,there is a corresponding
closing symbol and symbols are appropriately
nested.
University of Educatuon Lahore D.G Khan Campus 26
28. An Other Example of Stack in use of
• POSTFIX
• PREFIX
• INFIX
University of Educatuon Lahore D.G Khan Campus 28
29. Example: postfix expressions
• Postfix notation is another way of writing arithmetic
expressions.
• In postfix notation, the operator is written after the
two operands.
infix: 2+5 postfix: 2 5 +
• Expressions are evaluated from left to right.
• Precedence rules and parentheses are never
needed!!
University of Educatuon Lahore D.G Khan Campus 29
32. How to make header file in C++
University of Educatuon Lahore D.G Khan Campus 32
33. #ifndef HEADER_FILE_NAME
#ifdef HEADER_FILE_NAME
// write header file code here
// save this file *.h extension
// and save this file
// C:Program Files (x86)Dev-CppMinGW64include
#ifdef
University of Educatuon Lahore D.G Khan Campus 33
35. Queue
• Queue is a type of data structure.
• Queue is based on Technique FIFO(first in first out).
• It store the new object at the end of previous object.
• It remove the object that have firstly entered in Queue.
University of Educatuon Lahore D.G Khan Campus 35
36. Example
• People waiting to buy tickets.
• Single traffic line.
University of Educatuon Lahore D.G Khan Campus 36
37. Operations on Queue
• Enqueue();
In Enqueue is function, we can insert an object in Queue.
• Dequeue();
In Dequeue is function, can remove the object in Queue.
University of Educatuon Lahore D.G Khan Campus 37
38. #ifndef Queue
#ifdef Queue
#include<iostream>
using namespace std;
class Queue{
public:
int arr[5]; int size=5; int top;
int count, first;
Queue(){
first=-1;
count=0;
top=0;
}//end of constructor
void enqueue(int data){
if(count>=size){
cout<<"Queue is full"<<endl;
}
else{
first++;
arr[first]=data;
count++;
cout<<"data Added to Queue"<<endl;
}//end of else
}//end of enqueue
University of Educatuon Lahore D.G Khan Campus 38
39. void dequeue(){
if(count<=0){
cout<<"Queue is empy"<<endl;
}
else{
cout<<"value is Dequeuing =
"<<arr[top]<<endl;
top++;
count--;
}//end of else
}//end of dequeue
};// end of class
#enfif
#include<Queue.h>
int main(){
Queue obj;
obj.dequeue();
obj.enqueue(10);
obj.enqueue(20);
obj.enqueue(30);
obj.dequeue();
}
University of Educatuon Lahore D.G Khan Campus 39