Top Banner
Let’s learn TACKS & UEUES for CLASS XII( C++) SENIOR SECONDARY GROUP Presented By : NITI ARORA
49

Rana Junaid Rasheed

Jan 22, 2018

Download

Education

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Rana Junaid Rasheed

Let’s learn

TACKS & UEUES

for CLASS XII( C++)

SENIOR SECONDARY GROUP

Presented By : NITI ARORA

Page 2: Rana Junaid Rasheed
Page 3: Rana Junaid Rasheed

Introduction to Data Structure

Array and Link list

Stack

Array implementation of Stack

Linked Implementation of Stack

Queue

Array implementation of Queue

Linked Implementation of Queue

Page 4: Rana Junaid Rasheed

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. 

Page 5: Rana Junaid Rasheed

 

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 l inear 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. 

Page 6: Rana Junaid Rasheed

Static Data Structure: The data structure in which the number of elements is fixed, is known as Static Data Structure. Example: ArraysDynamic Data Structure: The data structure in which the number of elements is not fixed, is known as Dynamic Data Structure. Example: Linked List.

Page 7: Rana Junaid Rasheed

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.

Page 8: Rana Junaid Rasheed

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.

Page 9: Rana Junaid Rasheed

STACKSStacks 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.

Page 10: Rana Junaid Rasheed

STACKInserting an element in an array is known as PUSH.

Deleting an element from an array is known as POP.

Implementation of STACK in computers

When functions are called.

To convert a infix expression to postfix.

To evaluate a postfix expression.

Page 11: Rana Junaid Rasheed

STACKA stack is a list in which insertion and deletion takes place only at one end called top. Thus, called LIFO.

Representation of STACKdata1

data2

data3

data4

TOP

TOP

data4

data3

data2

data1

data1 data2 data3 data4 TOP

TOP data4 data3 data2 data1

Each one of the above has one open and one close end and data movement takes place from open end.

Page 12: Rana Junaid Rasheed

Basic operation and implementation of stacks

• Creation of stack

• Check for empty stack

• Check for full stack

• Add element in stack

• Delete element in stack

• Print stack

Page 13: Rana Junaid Rasheed

STACKSThe 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.

Page 14: Rana Junaid Rasheed

STACKThe stack operation can be explained as follows: Stack operation Content of array

Push(a) aPush (a)

a

Push(b)

b

aPush( c)

C

b

a

Pop( c)

b

aPop(b)

a

Push(b) ba

Push( c) cbaPop() baPop() a

Page 15: Rana Junaid Rasheed

STACKSA 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

Page 16: Rana Junaid Rasheed

Linear Stackint S[5];

When PUSH is selected, TOP is incremented,

And data is added at that subscript location

When POP is selected, TOP is decremented,

And data is removed from that subscript location

Stack array

int TOP;To hold address of location where data is inserted or deleted

Page 17: Rana Junaid Rasheed

Lets see working of Linear Stack

8

9

10

Push 7

7

8

9

10

Push 20

20

7

8

9

10

Push 14

OVERFLOW

ARRAY IS FULLTOP

TOP

TOP

Top is incremented

TOP++

Page 18: Rana Junaid Rasheed

CONTINUED….

20

7

8

9

10

Pop 20

7

8

9

10

Pop 7

8

9

10

UNDERFLOW OCCURS WHEN STACK IS EMPTY

TOP

Top

Top

TOP is decremented

TOP --

Page 19: Rana Junaid Rasheed

Lets see this using a program

Program Code for the Same is

Click here to execute program

Click here to see program code

Page 20: Rana Junaid Rasheed

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    900Requires 2

bytes

0x8ffebab4

   0x8ffebab4*P

Requires 2 bytes

Pointer P holds address of amt

Page 21: Rana Junaid Rasheed

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.

Page 22: Rana Junaid Rasheed

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;

Holds address of dynamic object of struct abc

 G     

0x8ff134abG->X

0x8ff134ab

G->X G->Y

To allocate dynamic allocation and

store address in point g

Page 23: Rana Junaid Rasheed

struct STACK // structure for stack{

int data;STACK *link;

};struct *TOP;

To hold address ofFirst node of the list

TOP pointer to holds address of dynamic objects of l ink 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 f irst node.

Page 24: Rana Junaid Rasheed

Lets see working of Linked stack

* TOP* TOP * TempNULL 0x8ffab2e6

A new memory is allocated and address is stored in temp

X NULLdata link

0x8ffab2e6

Top = Temp

Top will hold address of new location

* TOP* TOP0x8ffab2e6

Thus, TOP will have this address.

Push operation

Initially top is assigned NULL

Temp holds address of new

location

Page 25: Rana Junaid Rasheed

Cont…..*TOP*TOP

* Temp0x8ffab2e6

0x8ffab2e8

Another new memory is allocated to an object

Ydata link

0x8ffab2e8

* TOP* TOP

0x8ffab2e8

X NULLdata link

0x8ffab2e6

temp-> link = Top

Top=temp

0x8ffab2e6

Now TOP is

TOP will get shiftedY becomes first node

X becomes second node

Page 26: Rana Junaid Rasheed

Cont…..

* TOP* TOP * Temp0x8ffab2e8 0x8ffab2e8

An object is deleted from top

Ydata link

0x8ffab2e8

Thus Top will be

* TOP* TOP

0x8ffab2e6

X NULL data link

Temp=TOP

TOP=TOP->link

0x8ffab2e6

delete temp (to release memory)

0x8ffab2e6

TOP will get shiftedX becomes first node

Y will be released

POP operation

Page 27: Rana Junaid Rasheed

Lets see this using a program

Program Code for the Same is

Click here to execute program

Click here to see program code

Page 28: Rana Junaid Rasheed

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.

Page 29: Rana Junaid Rasheed

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.

Implementation of queue in computers

When program is executed.

Page 30: Rana Junaid Rasheed

QueueA 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

data2

data3

data4

data4

data3

data2

data2 data3 data4

data4 data3 data2

Each one of the above has two open end Front and Rear. Insertion is done from Rear and deletion form Front

Rear

Front

Front

Rear

Front Rear

FrontRear

Page 31: Rana Junaid Rasheed

Basic operation and implementation of QUEUE

• Creation of Queue

• Check for empty Queue

• Check for full Queue

• Add element in Queue

• Delete element in Queue

• Print Queue

Page 32: Rana Junaid Rasheed

QUEUEThe 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.

Page 33: Rana Junaid Rasheed

QUEUEThe Queue operation can be explained as follows: Queue operation Content of array

Insert(a) Front=0 Rear=0

Insert(b) Front=0 Rear=1

Insert( c) Front=0 Rear=2Delete() Front=1 Rear=2Delete() Front=2 Rear=2

a

a b

a b c

b cc

If we try to insertOverflow occurs

Though first two cells are empty

Page 34: Rana Junaid Rasheed

Linear Queueint Q[5];

When INSERT is selected, Rear is incremented,

And data is added at that subscript location

When DELETE is selected, Front is decremented,

And data is removed from that subscript location

Queue array

int Front, Rear;To hold address of location where data is inserted or deleted

Page 35: Rana Junaid Rasheed

QUEUEA 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

Page 36: Rana Junaid Rasheed

Lets see working of LINEAR QUEUE

8

9

10

Insert 7

7

8

9

10

Insert 20

20

7

8

9

10

Insert 14

OVERFLOW

QUEUE is full

Front

rear

rear

rear

Front Front

Rear is incremented

Rear++

Page 37: Rana Junaid Rasheed

Lets see working of Queue as an array

20

7

8

9

10

Delete

20

7

8

9

Delete

20

7

8

Underflow occurs when QUEUE is empty

Rear

Front

RearRear

Front

Front

Front is incremented

Front++

Page 38: Rana Junaid Rasheed

Lets see this using a program

Program Code for the Same is

Click here to execute program

Click here to see program code

Page 39: Rana Junaid Rasheed

struct QUEUE // structure for QUEU{

int data;QUEUE *link;

};struct *Front,*Rear;

To hold address ofFirst and Last node of the list

Front and Rear pointer to holds address of dynamic objects of l ink 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 f irst node.

Page 40: Rana Junaid Rasheed

Lets see working of Linked Queue

* Front * Rear* Front * Rear * TempNULL NULL 0x8ffab2e6

A new memory is allocated and address is stored in temp

X NULLdata link

0x8ffab2e6

Front=Rear = Temp

Front and Rear will hold address of First location

* Front * Rear* Front * Rear0x8ffab2e6 0x8ffab2e6

Thus, Front and Rear will have this address.

Insert operation

Initially Front and Rear is assigned NULL

Temp holds address of new

location

Page 41: Rana Junaid Rasheed

Cont…..*Front * Rear*Front * Rear

* Temp0x8ffab2e6 0x8ffab2e6

0x8ffab2e8

Another new memory is allocated to an object

Xdata link

0x8ffab2e6

* Rear* Rear

0x8ffab2e8

Y NULLdata link

0x8ffab2e8

temp-> link = Rear

Rear=temp

0x8ffab2e8

Now Rear is

Rear will get shiftedY becomes Last node

Page 42: Rana Junaid Rasheed

Cont…..

* Front * Rear* Front * Rear * Temp0x8ffab2e6 0x8ffab2e8

0x8ffab2e6

An object is deleted from Front

Xdata link

0x8ffab2e6

Thus Front will be

* Front* Front

0x8ffab2e8

Y NULL data link

Temp=Front

Front=Front->link

0x8ffab2e8

delete temp (to release memory)

0x8ffab2e8

Front will get shiftedY becomes first node

X will be released

Delete operation

Page 43: Rana Junaid Rasheed

Lets see this using a program

Program Code for the Same is

Click here to execute program

Click here to see program code

Page 44: Rana Junaid Rasheed

CIRCULAR QUEUEThe 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.

Page 45: Rana Junaid Rasheed

QUEUEThe Queue operation can be explained as follows: Queue operation Content of array

Insert(a) Front=0 Rear=0

Insert(b) Front=0 Rear=1

Insert( c) Front=0 Rear=2Delete() Front=1 Rear=2Insert (d) Front=2 Rear=0

a

a b

a b c

b cd c

Overflow occurs only whenArray is FULL.

Rear moves to 0 if array is empty

Page 46: Rana Junaid Rasheed

Lets see this using a program

Program Code for the Same is

Click here to execute program

Click here to see program code

Page 47: Rana Junaid Rasheed

Do you have any

Page 48: Rana Junaid Rasheed

TEST YOUR KNOWLEDGE

• What is difference between Stack & Queue?

• What is Dynamic Allocation?

• What is the significance of Top?

• What is the significance of Front & Rear?

• What is Overflow?

• What is Underflow?

• Where Stack is Implemented?

Page 49: Rana Junaid Rasheed