Tacks & Ueues: Let's Learn
Tacks & Ueues: Let's Learn
Introduction to Data Structure Array and Link list Stack Array implementation of Stack Linked Implementation of Stack Queue
A mathematical and logical model of data is known as Data Structure. Primitive data structure: The data structure, which is available in the compiler, is known as a primitive data structure. Non-primitive data structure: The data structure, which is not available in the compiler, is known as non-primitive data structure.
Linear Data Structure: The data structure in which each element has access to maximum of one predecessor element and maximum of one successor element is known as linear data structure. Example: Stack, Queue, etc. Non-linear Data Structure: The data structure in which each element can access any number of predecessor elements and any number of successor elements is known as Non-linear data structure. Example: Tree, Graphs, etc.
Static Data Structure: The data structure in which the number of elements is fixed, is known as Static Data Structure. Example: Arrays Dynamic Data Structure: The data structure in which the number of elements is not fixed, is known as Dynamic Data Structure. Example: Linked List.
It is a static data structure. It is a homogeneous collection of data. The elements in the array are stored on consecutive memory locations. Array is also known as a subscripted variable, e.g., A[i] is ith element of the array A.
It is a non-primitive linear data structure in which insertion and deletion of elements takes place from only one end, known as top.
It is a non-primitive linear data structure in which insertion and deletion of elements takes place from two opposite ends rear and front respectively.
STACKS
Stacks is LIFO (Last In First Out) structure and physically can be implemented as an array or as a linked list. Stack, when implemented as an array is functionally same as any other array except that here, adding an element and deletion is done from the same direction just like a pile of books.
STACK
Inserting an element in an array is known as PUSH. Deleting an element from an array is known as POP.
STACK
A stack is a list in which insertion and deletion takes place only at one end called top. Thus, called LIFO.
Representation of STACK
data1 data2 data3 data4 TOP TOP data4 data3 data2 data1 TOP data4 data3 data2 data1 data1 data2 data3 data4 TOP
Each one of the above has one open and one close end and data movement takes place from open end.
STACKS
The fundamental operations that can be performed on stack are PUSH and POP. When element is added on the stack top is called PUSH.
And When Data is removed from the stack top, the operation is called POP.
STACK
The stack operation can be explained as follows:
Stack operation Push(a) Push(b) Push( c) Pop() Pop()
Pop(b)
a
STACKS
A stack is a list, any list implementation can be used to implement stack. We can implement stack by the following data structures:
Array called Linear Stack Linked List called Linked Stack
Linear Stack
int S[5];
Stack array
To hold address of location where data is inserted or deleted
int TOP;
When PUSH is selected, TOP is incremented, And data is added at that subscript location
20 7 8
OVERFLOW ARRAY IS FULL
7 8
9
10
Push 7
9
10
9
10
Push 14
CONTINUED.
TOP
20 7 8
Top
7 8
Top
9
10
9
10
9
10
Pop 20
Pop 7
A variable which holds an address of a memory location of another variable is known as a Pointer Variable (or only pointer). Example int amt, *p;
amt
900
0x8ffebab4
*P
0x8ffebab4
NEW operator in C++ returns the address of a block of unallocated bytes (depending on data type a pointer pointing to). DELETE operator in C++ reverses the process of new operator, by releasing the memory location from a pointer. It de allocates memory assigned by NEW.
A pointer, which stores the address of struct type data, is known as Pointer to structure. struct abc { int X,Y; }; struct *g=new abc;
0x8ff134ab
G->X G->X
0x8ff134ab
G->Y
struct STACK // structure for stack { int data; To hold address of STACK *link; First node of the list }; struct *TOP; TOP pointer to holds address of dynamic objects of link stack. As we push a node TOP element get shifted and new node becomes first node. LIFO implementation every new node becomes first node. When we pop Top node is deleted and next node becomes first node.
Push operation
NULL
* Temp
0x8ffab2e6
link NULL
* TOP
0x8ffab2e6
Cont..
*TOP 0x8ffab2e6 Another new memory is allocated to an object * Temp
TOP will get shifted 0x8ffab2e8 temp-> link = Top Y becomes first node X becomes second node Top=temp * TOP
0x8ffab2e8 data Y
0x8ffab2e8
link
0x8ffab2e6
link NULL
Cont..
POP operation
data
Y
0x8ffab2e8
0x8ffab2e6
data X
0x8ffab2e6
Queues
Queue is FIFO (First In First Out) structure and physically can be implemented as an array or as a linked list. Queue, when implemented as an array is functionally same as any other array except that here, adding an element and deletion is done from the one direction and deletion from other just like any queue of peoples.
Queues
Inserting an element in an array is known as insert. Deleting an element from an array is known as delete But this is done with the help of two parameters rear and front.
Queue
A Queue is a data structure in which insertion is done at the end and deletion is done from the front of queue. It is FIFO .
Representation of Queue
Front
Rear
Front
Rear
Front
Each one of the above has two open end Front and Rear. Insertion is done from Rear and deletion form Front
QUEUE
The fundamental operations that can be performed on Queue are Insert and Delete. When element is added on the Queue Front is called Insert. And When Data is removed from the Queue Rear, the operation is called Delete.
QUEUE
The Queue operation can be explained as follows:
Queue operation Insert(a) Insert(b) Insert( c) Delete() Delete() Content of array Front=0 Rear=0 Front=0 Rear=1 Front=0 Rear=2 Front=1 Rear=2 Front=2 Rear=2 a
a b
a b c
If we try to insert Overflow occurs Though first two cells are empty
b c
Linear Queue
int Q[5];
Queue array
To hold address of location where data is inserted or deleted
When INSERT is selected, Rear is incremented, And data is added at that subscript location
QUEUE
A Queue is a list, any list implementation can be used to implement Queue. We can implement Queue by the following data structures:
Array called Linear Queue Linked List called Linked Queue
20 7 8
OVERFLOW QUEUE is full
7 8
rear
9
Front
9
10
Front Front
9
10
10
Insert 7
Insert 20 Insert 14
Rear is incremented
Rear++
20 Rear 7 8
20 7 8
Rear
20 7
Underflow occurs when QUEUE is empty
Front
9
10
Front
Front
Delete
struct QUEUE // structure for QUEU { int data; To hold address of QUEUE *link; First and Last node of the list }; struct *Front,*Rear; Front and Rear pointer to holds address of dynamic objects of link stack. As we insert a node Rear element get shifted and new node becomes next node. FIFO implementation every new node added at end. When we Delete Front node is deleted and next node becomes first node.
Insert operation
NULL NULL Initially Front and 0x8ffab2e6 Rear is assigned A new memory is NULL Front=Rear = Temp
allocated and address is stored in temp
data X 0x8ffab2e6 Front and Rear will hold address of First location
* Temp
link NULL
* Front
* Rear
0x8ffab2e6 0x8ffab2e6
Cont..
*Front * Rear
0x8ffab2e6 0x8ffab2e6
Rear will get shifted temp-> link = Rear Y becomes Last node
Rear=temp
data X
0x8ffab2e6
link NULL
Cont..
Delete operation
* Temp
0x8ffab2e6 0x8ffab2e8
Temp=Front Front=Front->link
Front will get shifted Y becomes first node X will be released Thus Front will be
* Front 0x8ffab2e8 link NULL
0x8ffab2e6
data
X
0x8ffab2e6
0x8ffab2e8
data Y
0x8ffab2e8
CIRCULAR QUEUE
The fundamental operations that can be performed on Circular Queue are Insert and Delete. When overflow occurs though the free cells are available, Rear reaches ends Circular Queue is implemented to avoid this drawback. In Circular Queue as soon as Rear reaches maximum it should reset to 0.
QUEUE
The Queue operation can be explained as follows:
Queue operation Insert(a) Insert(b) Insert( c) Delete() Insert (d) Content of array Front=0 Rear=0 Front=0 Rear=1 Front=0 Rear=2 Front=1 Rear=2 Front=2 Rear=0 a
a b
a b c
Overflow occurs only when Array is FULL. Rear moves to 0 if array is empty
b c