0% found this document useful (0 votes)
15 views7 pages

Chapter 3 4

Uploaded by

saralardjan98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views7 pages

Chapter 3 4

Uploaded by

saralardjan98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UMBB/ FS/ DI

The essential on Stacks and Queues (Version 2)

1. Stacks

1.1 Definition
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle.
Real-life Analogy: A stack of plates where the last plate added is the first one to be removed.

IN OUT

A Stack

1.2 Applications
• Expression evaluation and syntax parsing.
• Managing function calls (recursion).
• Conversion of recursive solutions to iterative using stacks.
• Undo functionality in text editors.
• Backtracking ( navigating mazes).
1.3 Basic Operations
 Push: Add an element to the top of the stack.
 Pop: Remove and return the top element.
 IsEmpty: Check if the stack is empty.

DRIFA HADJIDJ 1
1.4 Stack representation in the memory
A stack can be represented in the memory using the array ( contiguous representation) or using the linear list
(dynamic representation).

A-Contiguous representation

Declaration
const int Max= 100;

struct stack
{ int element[Max]; // any type can be chosen
int top;
}

The primitives

Initialise
void initStack(Stack &s) // Passing parameter by reference
{ p.top=Nullptr;}

void initStack(Stack *s) // or Passing parameter by address


{ s->top=0;}

Check if the stack is empty


Bool stackEmpty(Stack s)
{ if (s.top==0)
return true;
else
return false;
}

Add an element to the top of the stack


Void push(Stack &s, int val)
{ if (s.top==Max)
cout<< "the Stack is full ";
else
{
s.element[s.top]= val;
s.top++;
}
}

Remove and return the top element


Void pop(Stack &s, int &val)
{ if (StackEmpty(s))
cout<< "the Stack is empty! "<<endl;
else
{
s.top--;
val=s.element[s.top];
DRIFA HADJIDJ 2
}
}
B- Representation by linear list (Dynamic Stack)

Declaration
struct elementStackL
{ int data ; // same declaration as a simply list
elemetStackL *next;
};

Primitives when you choose to push and to pop at the beginning of the list

Initialise
Void initStackL(StackL *&top)
{ top==NULL;}

Check if the stack is empty


Bool stackLEmpty(StackL *top)
{ return top==NULL;}

Push an element
Void push(elementStackL *& top, int val)

// Same principle as insertBiginning

Pop an element
Void pop(elementStackL *&top,int &val)

// Same principle as deleteBiginning but retrieving the value of the deleted element

DRIFA HADJIDJ 3
2.Queues

2.1 Definition

A queue is a linear data structure that follows the First In, First Out (FIFO) principle.

Real-life Analogy: A queue at a ticket counter where the first person in line is served first.

OUT IN

The front A queue the rear

2.2 Applications
• Process scheduling in operating systems.
• Data buffering (e.g., in network routers).
• Breadth-First Search (BFS) in graphs.

2.3 Basic Operations

1. Enqueue: Add an element to the rear of the queue.


2. Dequeue: Remove an element from the front of the queue.
3. IsEmpty: Check if the queue is empty.

2.4. Simple Queue

A simple queue is a basic queue with FIFO operations

A.Contiguous representation

Declaration
const int Max=……;
struct queue
{ int element[Max];
int nbrelement;
}

The primitives

Initialise

DRIFA HADJIDJ 4
void initqueue(queue &q)
{ q.nbrelement==0;}

Check if the queue is empty


bool queueEmpty (queue q)
{ if (q.nbrelement==0)
return true;
else
return false;
}

Add an element in the queue


void enqueue(queue &q, int val)
{ if (q.nbrelement==Max)
cout <<" The queue is full. "<<endl;
else
{
q.elment[q.nbrelement]=val;
q.nbrelement++
}
}

Extract an element from the queue


void dequeue(queue &q, int &val)
{ if (queueEmpty(q)
cout <<" the queue is empty. "<<endl;
else
{
val=q.element[0];
for(int i=0; i< q.nbrelement-2; i++)
{
q.element[i]= q.element[i+1];
q.nbrelement--;
}
}
}

B-Representation by linear list (dynamic queue)

In this representation, we insert from one side of the list and remove from the other side.

Declaration
struct elemenQueueL
{ int data ;
elementQueu2L *suivant;
};

Les primitives

Initialisation
DRIFA HADJIDJ 5
void initQueueL(elementQueueL *&q)
{ q =NULL ;}

Test if the queue is empty


bool queueLEmpty(elementQueueL *q)
{ if (q==0)
return 1
else
return 0;
}

Remark: Primitive when you choose to add an element at the beginning of the list and to extract an element at the
end of the list.

Insert an element in the queue


void enqueueL(elementQueueL *&q, int val)

Same principle as insertBiginning

Extract an element from the queue


void dequeueL( elementQueueL *&q,int &x)

Same principle as supEnd but retrieving the information of the deleted element

2.5 Circular Queue

A circular queue overcomes the limitation of fixed size by wrapping around the array.

A.Contiguous representation

Declarations
const int DMax=…;
struct cqueue
{
int element[Dmax];
int IndiceIn,IndiceOut;
bool full ;
};

Primitives

initialise
void initcqueue(cqueue &q)
{ q.IndiceIn=0;
q.IndiceOut=0;
q.full=false;
}

DRIFA HADJIDJ 6
Check if the queue is empty
bool cqueueempty(cqueue q)
{ if((q. IndiceIn==q.IndiceOut)&& (!q.full))
return true;
else
return false;
}

Add an element in the queue


void enqueue(cqueue &q,int x)
{ if(q. full)
cout<<"the queue is full";
else
{ q.element[q. IndiceIn]=x;
q. IndiceIn=(q.IndiceIn+1)%DMax;
if (q.IndicIn==q.IndiceOut)
q.full=true
}
}

Extract an element from the queue


void dequeue(cqueue &q,int &x)
{ if (cqueueEmpty(q))
cout<<" the queue is empty";
else
{
x=q.element[q.IndiceOut];
q.IndiceOut=(q.IndiceOut+1)%DMax;
q.full=false;
}

DRIFA HADJIDJ 7

You might also like