Chapter 3 4
Chapter 3 4
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;}
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;}
Push an element
Void push(elementStackL *& top, int val)
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
2.2 Applications
• Process scheduling in operating systems.
• Data buffering (e.g., in network routers).
• Breadth-First Search (BFS) in graphs.
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;}
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 ;}
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.
Same principle as supEnd but retrieving the information of the deleted element
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;
}
DRIFA HADJIDJ 7