SlideShare a Scribd company logo
1 of 63
INTRODUCTION.
The term ‘list’ refers to a linear collection of data. One form of linear list
are arrays. A linked list is a linear collection of data elements, called
nodes pointing to the next node by means of a pointer. Each node is
divided into 2 parts: the first part containing the information and the
second part called ‘link’ or ‘next pointer’ containing the address of the
next node in the list.
A stack is a linear structure implemented in LIFO (Last In First Out)
manner where insertions and deletions are restricted to occur at one
end named stack top. LIFO means element last inserted would be the
first one to be deleted. The stack is also a dynamic data structure as it
can grow or shrink.
A queue is also linear structure implemented in FIFO ( First I First Out
) manner where insertions can occur at the ‘rear’ end and ‘deletions’
occur only at ‘front’ end. FIFO means elements are deleted in the same
order as they are inserted into.
LINKED LIST, STACKS AND QUEUES
A linked list consists of several nodes.
Name Roll No Next Name Roll No Next Name Roll No Next
struct student
{
char name[20];
int roll_NO;
student *next;
};
Address 1000 Address 1100 Address 1200
First Node Second Node Third Node
Each Node has a pointer,
which will point to the
location where the next
node being stored.
Each node consists of Data Part and Link Part.
ANIL 100 1100 BABU 101 1200 SUNIL 102 NULL
Data Part
Link Part

A structure having a member element
that refers to the structure itself is called
self-referential structure.
Name Roll No Next Name Roll No Next Name Roll No Next
Address 1000 Address 1100 Address 1200
First Node Second Node Third Node
NULL ANIL 100 1100
struct student
{
char name[20];
int roll_NO;
student *prev, *next;
};
Data Part
Link Part

The linked list are of two types. Singly linked list and doubly linled list. Singly
linked list contain node with single pointer pointing to the next node whereas
doubly linked list contain two pointer one pointing to previous node and other
pointing to the next node in sequence.
Address of
Previous Node.
Address of
Next Node.
1000 BABU 101 1200 1100 HARI 102 NULL
Prev Prev Prev

NEED FOR LINKED LIST. The linked list overcomes the drawback of arrays as in
linked list number of elements need not to be predetermined. More memory can
be allocated or released during the process as and when required, making
insertions and deletions much easier and simpler.
INFO Link INFO Link INFO Link INFO Link
Node 1 Node 2 Node 3 Node 4
Node 1 Node 2 Node 3 Node 4
Prev INFO Next Prev INFO Next Prev INFO Next Prev INFO Next
Singly linked list.
Doubly linked list.
Anil 100 67 78 63 208 1100 Babu 101 87 64 73 224 1200 Hari 102 87 64 73 224 NUL
Address 1100 Address 1200
Node 1 Node 2 Node 3
Address 1000

Static allocation. This memory allocation technique reserves fixed
amount of memory before actual processing take place, there for the number
of elements to be stored must be predetermined. Such type of allocation is
called static memory allocation. Declarations of variables, arrays are example
of static memory allocations.
int a,b,c; // Allocate 2 bytes each int a[10]; // Allocate 10 * 2 = 20 Bytes
char name[] = “Anil Kumar “; // Allocate 10 bytes
float x[10]; // Allocate 10 * 4 = 40 bytes. int a[3][3]; // Allocate 2 * ( 3 * 3 ) = 18.
Memory Allocation ( Dynamic vs. Static ). The memory can be
allocated in two manners: Dynamically and Statically.
Dynamic memory allocation. This technique facilitates allocation of
memory during the program execution itself, as and when required. This
technique of allocation is called dynamic memory allocation. Dynamic memory
allocation also facilitates release of memory.
int *a = new int; // Allocate 2 bytes and the address will be assigned in ‘a’
int *a = new int(10); // Allocate 2 bytes and initialize with the value 10.
int *a = new int [10]; // Allocate space for 10 integers. ( 10 * 2 = 20 bytes. )
When maintaining a dynamic data structure, start pointer is very important.
The start pointer is a special pointer which stores the very first address of a
linked list. The next pointer of last node stores NULL value which means this
is the last node.
Singly linked list.
Node 4 Node 1 Node 3 Node 2 Node 5
Address 1300 Address 1000 Address 1200 Address 1100 Address 1400
Boy NULLBad 1400 Betty 1100 is 1300 Kuttan 1200
struct node
{
char name[20];
node * next’
}*start, *temp;
temp = start;
while ( temp != NULL )
{
cout << temp->name ;
temp = temp->next ;
}
Start from
1000

temp = 1000
Start
Betty
temp = 1100
Kuttan
temp = 1200
 Is
temp = 1300
Bad
temp = 1400
Boy
temp = NULL
Representation of a Linked List in Memory. Simplest implementation of list requires
two linear array, one called as INFO for storing information and the other known as
LINK for storing address of next INFO. Also a separate variable START is required
to store the beginning address of the List.
The maintenance of linked list assumes the possibility of insertion and deletion of node
using ‘new’ and ‘delete’ operators. A special list consisting of unused memory cells is
maintained. This list is called free storage list or free pool. This list (AVAIL list) has its
own pointer to the next free node. With every insertion number of free nodes in AVAIL
is reduced and with every deletion this number is increased. The method of adding
free nodes in free pool is called garbage collection.
INFO
Index DATA
0 E
1 T
2
3 Y
4
5
6 T
7
8 B
9
LINK
LINK Index
6 0
3 1
9 2
NULL 3
7 4
NULL 5
1 6
5 7
0 8
4 9
Start from 8 Click Me 

START
From 8
AVAIL
From 2

Show FREE POOL

Free-store allocation in C++. In C++, every program is provided with a pool of
unallocated memory, it may utilize during execution. This memory is known as free
storage memory. Free storage memory is allocated through ‘new’ operator, and
returns a pointer pointing to the allocated memory.
class Node
{
public:
char name[20];
Node *next;
};
void main()
{
}
start
temp
name next
Address 10001000
Node * start, * temp; // Click Me
start = temp = new Node; // Click Me
1000
Create List

cin >> temp->name; // Click Me 
temp->next = new Node; // Click Me 
temp = temp->next; // Click Me 
temp->next = NULL; // Click Me 
cin >> temp->name; // Click Me 
temp->next = NULL; // Click MeNULL
NULL
ANIL
ANIL
name next
Address 20002000
2000
NULL
NULL
BABU
BABU
INSERTION. In a linked list new ITEM is either added in the beginning of the list or
in the middle of the list or in the end of the list.To add an ITEM at the beginning of
the list START is modified to point to the new node and the next of new node points
to the previous first node.
To add an ITEM in the end of the list the next pointer of the last node is made to
point to the new node and the next of new ITEM is made NULL.
Node 1 Node 2 Node 3 Node 4
Address 100 Address 200 Address 300 Address 400
JOY NULLANIL 200 HARI 300 BABU 400
X = new NODE; X->NEXT = START; START = X; Click Me START
100
Node 1 Node 2 Node 3 Node 4
Address 100 Address 200 Address 300 Address 400
JOY NULLANIL 200 HARI 300 BABU 400
Address 500
SUNIL
100
500
500
Address 500
SUNIL NULL
LAST->NEXT = new NODE; LAST->NEXT->NEXT=NULL;Click Me
500500
INSERTION. In a linked list new ITEM is either added in the beginning of the list or
in the middle of the list or in the end of the list.To add an ITEM at the beginning of
the list START is modified to point to the new node and the next of new node points
to the previous first node.
To add an ITEM in the end of the list the next pointer of the last node is made to
point to the new node and the next of new ITEM is made NULL.
start
NULL
START END
X
X = new NODE; // Click Me
X->Next = START; // Click Me
START = x; // Click Me
name next
ANIL
Address 10001000
1000NULL
1000
1000
name next
HARI
Address 20002000
20001000
2000
2000
name next
RAM
Address 3000
3000
3000
2000
3000
3000
END->next = new NODE; // Click Me
END = END->Next; // Click Me
START = END = new NODE; // Click Me
name next
ANIL NULL
Address 1000
name next
BIJU NULL
Address 2000
name next
HARI NULL
Address 3000END->Next = NULL; // Click Me 
10001000
1000 1000
2000
2000
2000
3000
3000
3000
OPERATIONS ON SINGLY LINKED LIST.
2. TRAVERSAL : Traversal means processing all data elements in a data
structure.
3. DELETION : Deletion means removal of a data element from a data
structure.
4. SEARCHING : Searching means search for a specified data element in a
data structure.
1. INSERTION : Insertion means addition of new data element in a data
structure.
5. REVERSAL
6. SPLITTING
7. CONCATINATION
1. Define a class or structure contains Information field and Link field.
2. Declare *START and *X of above class type and a character variable ‘ans’.
3. Assign NULL in START. START = NULL
4. Repeat following while ans = ‘Y’ (Yes) using do .. while loop.
Allocate new node X. X = new NODE
Read information of above node X. Cin >> X->info
Assign START in the Link field of X (new node). X->LINK = START
Assign X in START. START = X;
Print a Caption “Do you want to Continue (Y/N) ?. “
Read answer ans. ans = getche() or cin >> ans;
5. Assign START in X. ( To traverse from first node )
6. Repeat following while X != NULL
Print information fields of X. ( Eg. cout << X->INFO )
Assign LINK field of X in X. X = X->LINK
INSERT AT BEGINNING OF THE LIST.
ALGORITHM
1. Ptr = START
2. NEWPTR = new node
3. If NEWPTR = NULL then print “No space available”
4. Else
{
5. NEWPTR->INFO = ITEM.
6. NEWPTR->LINK = NULL
7. If START = NULL then START = NEWPTR
8. Else
{
NEWPTR->LINK = START
START = NEWPTR
}
INSERT AT BEGINNING OF THE LIST.
ALGORITHM
# include <iostream.h>
# include <conio.h>
struct student
{
char name[20]; int roll;
student *next;
};
main()
{
student *start = NULL, *x;
char ans; clrscr();
do
{
cout << "nnnDo You Want to Continue (Y/N) ?. ";
ans = getche();
}while(ans=='y' || ans == 'Y');
x = start;
cout << "nnnTraversal of List ";
while(x!=NULL)
{
cout << "nn" << x->name << ", " << x->roll;
x = x->next;
}
getch();
}
name next
ANIL
Address 1000
START
NULL
X
x = new student; // Click Me
cout << "nnnPlease Enter Name & Roll No ";
cin >> x->name >> x->roll;

x->next = start; // Click Me 
start = x; // Click Me 
1000
NULL
1000
name next
ANIL
Address 20002000
1000
2000
name next
ANIL
Address 30003000
2000
3000
# include <iostream.h>
# include <conio.h>
main()
{
student *start = NULL, *x;
char ans;
clrscr();
do
{
x = new student;
x->input();
x->next = start;
start = x;
cout << "nDo You Want to Continue (Y/N)?. ";
ans = getche();
}while(ans=='y' || ans == 'Y');
x = start;
cout << "nnnTraversal of List ";
while(x!=NULL)
{
x->output();
x = x->next;
}
getch();
}
class student
{
char name[20];
int roll;
public:
student *next;
void input()
{
cout << "nEnter Name & Roll No ";
cin >> name >> roll;
}
void output()
{
cout << "nn" << name << ", " << roll;
}
};
# include <iostream.h>
# include <conio.h>
main()
{
student *start = NULL, *x; char ans;
do
{
x = create_node();
insert_beg(x);
cout << "nYou Want to Continue?.";
ans = getche();
}while(ans=='y' || ans == 'Y');
cout << "nnnTraversal of List ";
display(start);
getch();
}
struct student
{
char name[20];
int roll;
public:
student *next;
}*start=NULL;
student * create_node()
{
student *x = new student;
cout << "nPlease Enter Name & Roll No ";
cin >> x->name >> x->roll;
return x;
}
void insert_beg(student *x)
{
x->next = start;
start = x;
}
void display(student *x)
{
while(x!=NULL)
{
cout << "nn" << x->name << ", " << x->roll;
x = x->next;
}
}
INSERT AT THE END OF THE LIST. ALGORITHM
1. Define a class or structure ‘NODE’ contains Information Field and Link Field.
2. Declare 2 pointers *START = NULL and *LAST of type NODE and char ans.
3. Repeat following while ans = ‘Y’ using do .. While loop.
if START == NULL then
{
Allocate new NODE and assign Address in START and LAST.
E.g START = LAST = new NODE.
}
else
{
Allocate new NODE and assign Address in link field of LAST Node.
E.g LAST->LINK = new NODE.
Assign LAST->LINK in LAST. E.g LAST = LAST->LINK.
}
Read information of LAST NODE. E.g cin >> LAST->INFO.
Assign NULL in the link field of LAST node. E.g LAST->LINK = NULL.
4. Repeat following while START != NULL. /* Traverse from START. */
Print Information Field of START. E.g cout << START->INFO.
START = START->LINK.
INSERTION AT END
1. Declare pointers START, PTR, NEWPTR, REAR
2. PTR = START
3. NEWPTR = new NODE
4. If newptr = NULL then print “No Space avilable.” and exit
5. Else NEWPTR->LINK = NULL
6. If START = NULL then START = REAR = NEWPTR
7. Else REAR->LINK = NEWPTR; and REAR = NEWPTR
INSERT AT THE END OF THE LIST.
ALGORITHM
# include <iostream.h>
# include <conio.h>
main()
{
student *start = NULL, *x; char ans;
do
{
x = create_node();
insert_end(x);
cout << "nYou Want to Continue?.";
ans = getche();
}while(ans=='y' || ans == 'Y');
cout << "nnnTraversal of List ";
display(start);
getch();
}
struct student
{
char name[20];
int roll;
public:
student *next;
}*start=NULL, *end = NULL;
student * create_node()
{
student *x = new student;
cout << "nPlease Enter Name & Roll No ";
cin >> x->name >> x->roll;
x->next = NULL;
return x;
}
void insert_end(student *x)
{
if(start == NULL) { start = end = x; }
else { end->next = x; end = x; }
}
void display(student *x)
{
while(x!=NULL)
{
cout << "nn" << x->name << ", " << x->roll;
x = x->next;
}
}
Deletion. Deletion of node from a linked list involves (i) Search for ITEM in the list
for availability. (ii) if available, make its previous node point to its next node.
Node 1 Node 2 Node 3
Address 100 Address 200 Address 300
ANIL 200 HARI 300 BABU 400
Address 500
SUNIL NULL
Node 5
Address 500
Node 4
Address 400
JOY 500
FIND JOY Click Me DELETE JOY Click Me 

OY
500
Case 1. If the node to be deleted happens to be the first node, then START is made to
point the second node in sequence and deleted node is added in the free-store.
DELNODE = START; START = DELNODE->NEXT; DELETE DELNODE;
In deletion of a node, there are 3 posibilities.
Case 3. If the node is at the end of the list, then its previous node X will point to NULL.
DELNODE = X->NEXT; X->NEXT = NULL; DELETE DELNODE;
Case 2. If the node happens to be in the middle of the list, its previous pointer is saved
in X and if the node’s pointer is PTR then X->LINK = PTR->LINK;
DELNODE = X->NEXT; X->NEXT = DELNODE->NEXT; DELETE DELNODE;
X becomes 300.
Node to be Delete Y = X->next(400)
TO DELETE ‘Y’
X->next = Y->next
Or
X->next = X->next->next
delete Y
X becomes 300.
Node to be Delete Y = X->next(400)
TO DELETE ‘Y’
X->next = Y->next
Or
X->next = X->next->next
delete Y
/* First of all initialize pointers */
1. if START = NULL then print “Under flow”
2. Else
PTR = START;
START = PTR->LINK;
delete PTR
3. END
DELETION FROM THE BEGINNING OF LIST.
ALGORITHM
# include <iostream.h>
# include <conio.h>
main()
{
student *x;
int ch;
clrscr();
do
{
cout << "nnn1. Insert Node.nn2. Delete
First Node.nn3. Display.nn4. Exit ";
cin >> ch;
if(ch==1) create_node();
else if(ch==2) delete_first();
else if(ch==3) display(start);
}while(ch!=4);
}
struct student
{
char name[20];
int roll;
student *next;
}*start=NULL;
void create_node()
{
student *x = new student;
cout << "nEnter Name & Roll No ";
cin >> x->name >> x->roll;
x->next = start;
start = x;
}
void delete_first()
{
student *delnode = start;
start = start->next;
delete delnode;
}
void display(student *x)
{
while(x!=NULL)
{
cout << "nn" << x->name << ", " << x->roll;
x = x->next;
}
}
Traversal of a linked list means processing all the
nodes of the list one by one. Following algorithm
traverses a list to print all the elements of it.
/* Initialize counters */
1. PTR = START
2. Repeat steps 3 and 4 while PTR != NULL
3. Print PTR->INFO
4. PTR = PTR->LINK
5. end.
TRAVERSAL.
ALGORITHM
STACKS. Logically, a Stack is a LIFO structure and physically it can be implemented
as an array or as a linked list. A Stack implemented as an array inherits all the
properties of an array and if implemented as a linked list, all characteristics of a linked
list are possed by it. But whatever way a stack may be implemented, insertions and
deletions occur at the top only. An insertion in a Stack called PUSHING and a deletion
from a Stack is called POPPING.
3
2
1
Top
Show Pushing 
6
Show Poping 
5
4
Array for storing stack elements.
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
Top
Show Pushing  Show Poping 
STACK ( ARRAY ).
ALGORITHM
1. Receive Data as an Ergument.
2. If TOP == N – 1 then Print ‘Stack if Full. ( Over Flow ) and return.
3. Else
Increment TOP by 1.
Assign the Data at TOPth position of the Array S.
1. If TOP == – 1 then Print ‘Stack is Empty. ( Under Flow ) and return FALSE.
2. Else
Assign TOPth element in X. ( X = S [ TOP ]
Decrement TOP by 1.
Return value of X.
PUSH
POP
/* Initialize top with invallid subscript value to show that it is empty.
1. Top = -1
2. Read Item /* Item which is to be pushed. */
3. If ( top == N – 1) then print “Over flow.”
4. Else top = top + 1. /* Increment top to move to next empty position.
5. Stack [ top ] = Item
6. end.
STACK ( ARRAY ) – PUSH.
ALGORITHM
Deletion of an data from stack removes the element at top most position
Algorithm
/* Firstly, check for underflow condition */
1. If Top == -1 then print “Underflow” and exit
2. Else print Stack [ Top ]
3. Top = Top – 1
4. end.
STACK ( ARRAY ) – POP.
ALGORITHM
STACK ( ARRAY ).
ALGORITHM
1. Declare int array S[10], TOP = -1, No ‘N’ and menu choice ‘ch’
2. Repeat following while ch != 4
a. Print Menu Options 1. Push, 2. Pop, 3. Exit.
b. Input Choice (ch) ( 1 – 3 )
c. If (ch==1) then
Input number.
Invoke the function PUSH by giving N E.g PUSH(N)
d. Else if ( ch == 2 ) then
Invoke the function POP() and assign the returned value in
‘N’
if ( N == -1 ) then print “Stack is Empty.”
else Print Poped Number ‘N’.
# include <iostream.h>
# include <conio.h> S = new int[n];
do
{
cout << "nn1. PUSH.n2. POP.n3. Exit ";
cout << "nnEnter Your Choice ( 1- 3 ) ?. ";
cin >> ch;
switch(ch)
{
case 1:
cout << "nEnter a Number ";
cin >> num;
push(num); break;
case 2:
num = pop();
if ( num== -1 )
cout << "nnStack is Empty.";
else
cout << "nThe Poped No is " << num;
break;
}
}while(ch!=3);
}
int *S, n, top=-1;
void push(int num)
{
if (top==n-1) cout << "nStack is Full.";
else *(S + ++top) = num;
}
int pop()
{
int x;
if (top==-1) return -1;
x = *(S + top--);
return x;
}
main()
{
int ch, num;
clrscr();
cout << "nEnter the Size of the Stack ";
cin >> n;
A linked list is a dynamic data structure where space requirements needed not
be predetermined. The creation of a Stack ( as a linked list ) is same as the
creation of a linked list. After getting a node for the ITEM to be inserted, TOP
(pointer pointing to the top ) points to the newly inserted node.
Algorithm – Pushing in a Linked Stack
/* Get new node for the ITEM to be inserted. */
1. NEWPTR = new NODE
2. NEWPTR->INFO = ITEM; NEWPTR->LINK = NULL;
/* Add node at the Top */
3. If top == NULL then TOP = NEWPTR;
4. Else NEWPTR->LINK = TOP;
5. TOP = NEWPTR;
6. end.
STACK ( using LINKED LIST ).
Algorithm – Popping from a Linked Stack.
1. If TOP == NULL then Print “Stack Empty.“
2. Else Print TOP->INFO
3. TOP = TOP->LINK
4. End.
STACK ( using LINKED LIST ).
ALGORITHM of POP
struct student
{
int num;
student *next;
}*TOP=NULL;
main()
{
int ch, n;
clrscr();
do
{
cout << "n1. PUSH.n2. POP.n3. Exit ";
cout << "nEnter Your Choice ( 1- 3 ) ?. ";
cin >> ch;
switch(ch)
{
case 1:
cout << "nnEnter a Number ";
cin >> n;
push(n); break;
case 2:
n = pop();
if ( n==-1) cout << "nStack is Empty.";
else cout << "nThe Poped No is " << n;
break;
}
}while(ch!=3);
}
int pop()
{
int x;
if ( TOP == NULL ) return -1;
student *delnode = TOP;
x = TOP->num;
TOP = TOP->next;
delete delnode;
return x;
}
void push(int n)
{
student *x = new student;
x->num = n;
x->next = TOP;
TOP = x;
}
# include <iostream.h>
# include <conio.h>
Application of STACK. Stacks are basically applied where LIFO scheme is
required. A simple example of stack is reversal of a given line. We can
accomplish this task by pushing each character on to a stack as it read. When
the line is finished, characters are then popped off the stack, and they will be
come off in the reverse order.
RShow Pushing  Show Poping E V E R S A L O F L I N E
When the operator is placed in between operands, such notation is called ‘infix’.
Example of infix notation. A + B 10 + 20 Answer 30
When the operator is placed before its operand, such notation is called ‘prefix’.
Example of pre-fix notation. + A B + 10 20 Answer 30
When the operator is placed after its operand, such notation is called ‘postfix’.
Example of post-fix notation. A B + 10 20 + Answer 30
POLISH STRING refers to the notation in which the operator symbol is placed
either before its operand ( pre–fix ) or after its operand ( post–fix ).
Infix Pre-fix Post-Fix
A + B + A B A B +
(A – C) * B * - A C B A C – B *
A + ( B * C ) + A * B C A B C * +
( A + B ) / ( C – D ) / + A B – C D A B + C D – /
(A+ ( B*C )) / (C – (D * B )) / + A * B C – C * D B A B C * + C D B * - /
Conversion of expressions.
While evaluating an infix expression, there is an evaluation order.
1. Brackets or Parenthesis
2. Exponentiation.
3. Multiplication or Devision
4. Addition or Subtraction.
The Operators with same priority are evaluated from left to right.
The steps to convert an infix expression to post-fix expression.
1. Determine the actual evaluation order by inserting Brackets.
2. Convert the expression in the inner most braces into post fix notation by
putting the operator after the operands.
3. Repeat Steps 2 until entire expression is converted into post-fix notation.
Show Post fix 
A + B+ B
Steps Expression on each steps
Example 10.1
1
2
3
4
Convert the expression in the inner most braces into post fix notation by
putting the operator after the operands.
( ( A + B ) * C ) / D
( ( A B + ) * C ) / D
( ( A B + C * ) ) / D
A B + C * D /
Click Here 
Click Here 
Click Here 
Steps ( ( A + B ) * C / D + E  F ) / G
1
2
3
4
5
6
7
( ( ( ( A + B ) * C ) / D ) + ( E  F ) ) / G Click Here 
Click Here 
Click Here 
Click Here 
( ( ( ( A B + ) * C ) / D ) + ( E  F ) ) / G
( ( ( A B + C * ) / D ) + ( E  F ) ) / G
( ( A B + C * D / ) + ( E  F ) ) / G
( ( A B + C * D / ) + ( E F  ) ) / G
( A B + C * D / E F  + ) / G
A B + C * D / E F  + G /
Click Here 
Click Here 
Click Here 
Show Post fix A + B+ B
Steps A * ( B + ( C + D ) * ( E + F ) / G ) * H
1
2
3
4
5
6
7
( A * ( B + ( ( C + D ) * ( E + F ) ) / G ) ) * H Click Here 
Click Here 
Click Here 
Click Here 
( A * ( B + ( ( C D + ) * ( E F + ) ) / G ) ) * H
( A * ( B + ( C D + E F + * ) / G ) ) * H
( A * ( B + ( C D + E F + * G / ) ) ) * H
( A * ( B C D + E F + * G / + ) ) * H
( A B C D + E F + * G / + * ) * H
A B C D + E F + * G / + * H *
Click Here 
Click Here 
Click Here 
Steps A + [ ( B + C ) + ( D + E ) * F ] / G
1
2
3
4
5
6
7
A + [ ( ( B + C ) + ( ( D + E ) * F ) ) / G ] Click Here 
Click Here 
Click Here 
Click Here 
A + [ ( ( B + C ) + ( ( D E + ) * F ) ) / G ]
A + [ ( ( B + C ) + ( D E + F * ) ) / G ]
A + [ ( ( B C + ) + ( D E + F * ) ) / G ]
A + [ ( B C + D E + F * + ) / G ]
A + [ B C + D E + F * + G / ]
A B C + D E + F * + G / +
Click Here 
Click Here 
Click Here 
Steps NOT A OR NOT B AND NOT C
1
2
3
4
5
6
( NOT A ) OR ( ( NOT B ) AND ( NOT C ) ) Click Here 
Click Here 
Click Here 
Click Here 
( NOT A ) OR ( ( B NOT ) AND ( NOT C ) )
( NOT A ) OR ( ( B NOT ) AND ( C NOT ) )
( NOT A ) OR ( B NOT C NOT AND )
( A NOT ) OR ( B NOT C NOT AND )
A NOT B NOT C NOT AND OR
Click Here 
Click Here 
Convert NOT A OR NOT B NOT C.
( Priority Order NOT AND OR )
Suppose X is an infix expression. Finds the equivalent Post fix expression Y
1. Push ‘(’ onto stack and add ‘)’ to the end of X
2. Scan X from left and repeat steps 3 to 6 for each element of X until stack is empty.
3. If an Operand is encountered add it to Stack.
4. If a left parenthesis is encountered push it on Stack.
5. If an operator is encountered, then
Repeatedly pop from STACK and add to Y each operator ( on the top of STACK)
which has same precedence as or higher than Operator.
Add operator to STACK.
6. If a right parenthesis is encountered then
Repeatedly pop from STACK and add to Y each operator ( on the top of STACK )
until a left parenthesis is encountered.
Remove the left parenthesis. ( Do not add the left parenthesis to Y )
7. end.
INFIX TO POSTFIX CONVERSSION.
Convert X : A + ( B * C – ( D / E  F ) * G ) * H
Symbol Stack Post-fix Expression Y
A
+
(
B
*
C
–
(
D
/
E

F
)
*
G
)
*
H
START

A
A
+
+ A
+ ( A
+ ( A B
+ ( * A B
+ ( * A B C
+ ( – A B C *
*
+ ( – ( A B C *
+ ( – ( A B C * D
+ ( – ( / A B C * D
+ ( – ( / A B C * D E
+ ( – ( /  A B C * D E
+ ( – ( /  A B C * D E F
+ ( – A B C * D E F  /
+ ( – * A B C * D E F  /
+ ( – * A B C * D E F  / G
+ A B C * D E F  / G * –
+ * A B C * D E F  / G * –
+ * A B C * D E F  / G * – H
A B C * D E F  / G * – H * +
# include <iostream.h>
# include <ctype.h>
char S[50]; int size = 50, top = -1;
main()
{
char infix[50], post[50]; int i, j;
cout << "n Enter Infix Expression "; cin >> infix;
for(i=j=0; infix[i] != NULL; i++)
{
if ( isalpha(infix[i]) ) post[j++] = infix[i];
else if ( infix[i] == '(' ) push ( '(' );
else if ( infix[i] == ')' )
{ while ( topele() != '(' && topele() != NULL )
{ post[j++] = pop(); }
pop(); }
else if (infix[i]=='^‘ || infix[i] == '*' || infix[i]=='/‘
|| infix[i] == '+' || infix[i] == '-' )
{
while ( presce(topele()) >= presce(infix[i])
&& topelement() != NULL )
{ post[j++] = pop(); }
push(infix[i]);
}
}
while ( topele() != NULL ) post[j++] = pop();
post[j] = NULL;
cout << "nnPost-fix expression is " << post;
}
char topele()
{ return ( top == -1 ) ? NULL : S[top]; }
char pop()
{
char x; if ( top == -1 ) return NULL;
x = S[top]; top--; return x;
}
void push(char opr)
{
if ( top == size - 1 ) return;
else S[++top] = opr;
}
int prece (char opr)
{
if (opr == '^') return 10;
else if (opr == '*' || opr == '/' ) return 9;
else if (opr == '+' || opr == '-' ) return 8;
return 0;
}
An infix expression is difficult for the machine to know and keep track of
precedence of operators. On the other hand a postfix expression itself
determines the precedence of operators ( as the placement of operators in a
post fix expression depends upon its precedence ). Therefore for the machine it
is easier to carry out a postfix expression than an infix expression.
Evaluation of Postfix expression. As postfix expression is without
parenthesis and can be evaluated as two operands and an operator at a time.
Evaluation rule of a postfix expression states :
While reading the expression from left to right, push the element in the stack
if it is an operand.
Pop the two operands from stack, if the element is an operator ( except NOT
operator ). In the case of NOT operator, POP one operand from the stack and
then evaluate it ( two operands and an operator ).
Push back the result of the expression. Repeat it till end of the expression.
For a binary operator, two operands are popped from stack and for a unary
operator, one operand is popped. Then the result is calculated using
operand(s) and the operator, and pushed back into the stack.
Evaluation of Postfix Expression. Algorithm
/* Reading of expression takes place from left to right */
1. Read the element.
2. If the element is operand then push the element in STACK
3. If the element is operator then
4. Pop two operands from stack ( POP one operand in case of
unary operators )
5. Evaluate the expression formed by the 2 operands and the
operator.
6. Push the result of the expression in the STACK.
7. If no more elements then POP the result else go to step 1.
8. end.
EVALUATION OF POSTFIX EXPRESSION.
Evaluate the postfix expression A B + C * D /
if A = 2, B = 3, C = 4 and D = 5
Element Action Taken
Intermediate
Calculation.
Stack






A ( 2 ) PUSH 2 None 2
B ( 3 ) PUSH 3 None 2 3
+ POP 3 & 2 3 + 2 = 5 5
C ( 4 ) PUSH 4 None 5 4
* POP 4 & 5 5 * 4 = 20 20
D ( 5 ) PUSH 5 None 20 5
/ POP 5 & 20 20 / 5 = 4 4
Evaluate the following expression in postfix form using a stack and show
the contents of the stack after execution of each operation.
Read each elements from left to right,
If it is an operand then PUSH it in Stack
If it is an operator then POP 2 values from the stack.
Perform Operation and Push back the Result
Click all these
buttons
Evaluate the expression 5 6 2 + * 12 4 / –
Element Action Taken
Intermediate
Calculation.
Stack
END RESULT 37






5 PUSH 5 None 5
6 PUSH 6 None 5 6
2 Push 2 3 + 2 = 5 5 6 2
+ POP 2 AND 6 6 + 2 = 8 5 8
* POP 8 & 5 5 * 8 = 40 40
12 PUSH 12 None 40 12
4 PUSH 4 None 40 12 4
/ POP 4 AND 12 12 / 4 = 3 40 3
- POP 3 AND 40 40 - 3 = 37 37
TRUE FALSE TRUE NOT FALSE TRUE OR NOT AND OR AND.
Element Action Taken
Intermediate
Calculation.
Stack










TRUE PUSH ‘TRUE’ None TRUE
FALSE PUSH ‘FALSE’ None TRUE FALSE
TRUE PUSH ‘TRUE’ None TRUE FALSE TRUE
NOT POP ‘TRUE’ FALSE TRUE FALSE FALSE
FALSE PUSH ‘FALSE’ None TRUE FALSE FALSE FALSE
TRUE PUSH ‘TRUE’ None TRUE FALSE FALSE FALSE TRUE
OR POP ‘TRUE & FALSE’ TRUE TRUE FALSE FALSE TRUE
NOT POP ‘TRUE’ FALSE TRUE FALSE FALSE FALSE
AND POP ‘FALSE & FALSE’ FALSE TRUE FALSE FALSE
OR POP ‘FALSE & FALSE’ FALSE TRUE FALSE
AND POP ‘TRUE & FALSE’ FALSE FALSE
TRUTH TABLE
FALSE AND FALSE = FALSE FALSE AND TRUE = FALSE
TRUE AND FALSE = FALSE TRUE AND TRUE = TRUE
void main()
{
char post[50]; int i, a, b, res;
cout << "nEnter Post-fix Expression ";
cin.getline(post,49);
cout << "nGiven Expression is " << post;
# include <iostream.h>
# include <conio.h>
# include <ctype.h>
# include <math.h>
char S[50]; int size, top = -1;
void push(char opr)
{
if ( top == size - 1 ) return;
else S[++top] = opr;
}
int pop()
{
char x;
if ( top == -1 ) return NULL;
x = S[top]; top--; return x-48;
}
for(i=0; post[i] != NULL; i++)
{
if ( post[i] >= '0' && post[i] <= '9' ) push(post[i]);
else if (post[i] == '^' || post[i] == '/' || post[i] == '*'
|| post[i] == '+' || post[i] == '-')
{
b = pop(); a = pop();
if (post[i] == '^') res = pow(a,b);
else if (post[i] == '*' ) res = a*b;
else if (post[i] == '/' ) res = a/b;
else if (post[i] == '+' ) res = a+b;
else res = a - b;
push(res+48);
}
}
res = pop();
cout << "nnnResult is " << res;
getch();
}
QUEUE. Logically a Queue is a FIFO structure and physically it can be
implemented either as an array or as a linked list. Whatever way a Queue is
implemented, insertions take place at the ‘rear’ end and the deletions at the
‘front’ end.
Queue as an Array. When a Queue is created as an array, its number of
elements is declared before processing. The beginning of the array
becomes its ‘front’ end and end of the array becomes its ‘rear’ end. ‘Font’
stores the index of the first element in the queue and ‘rear’ stores the index
of last element in the Queue. The number of elements in the Queue at any
time can be calculated from the values of the ‘front’ and the ‘rear’
If ‘front’ = 0 then no. of elements = 0
else no. of elements = front – rear + 1
QUEUE
Insertion in Array – Queue
1. If rear = NULL then rear = front = 0 and set ITEM in 0th index of Array QUEUE.
2. Else if rear = N – 1 then print “QUEUE IS FULL, OVERFLOW”
3. Else QUEUE [ rear + 1 ] = ITEM and rear = rear + 1.
4. end.
Deletion in Array Queue.
1. If front = NULL then print Queue is empty.
2. Else ITEM = Queue [front] and
if front = rear then front = rear = NULL else front = front + 1
3. end.
QUEUE
# include <iostream.h>
# include <conio.h>
int *Q, size, front = -1, rear = -1;
void main()
{
int ch, n;
cout << "nnnPlease Enter the Size ";
cin >> size; Q = new int[size];
do
{
cout << "nMAIN MENUnnn1. Insert.
nn2. Delete.nn3. Quit";
cout << "nEnter your Choice (1 - 3) ?. ";
cin >> ch;
if (ch==1)
{
cout << "nEnter a No "; cin >> n;
insert(n);
}
else if (ch==2)
{
n = del();
if (n == -1) cout << "nQueue is Empty.";
else cout << "nThe Deleted No is " << n;
}
}while(ch != 3);
}
int del()
{
int x;
if ( front == -1 ) return -1;
else
{
x = Q[front];
if (front == rear) front = rear = -1;
else front++;
return x;
}
}
void insert( int n )
{
if ( rear == -1 )
{ front = rear = 0; Q[rear] = n; }
else if ( rear == size - 1)
cout << "nnQueue is Full.";
else Q[++rear] = n;
}
INSERTION IN A LINKED QUEUE. Linked Queues are the
queues having link among its elements. Two pointers are mentioned to
store ‘front’ position and ‘rear’ position.
Insertion in a Linked Queue. Insertion in a linked queue also take
place only at the rear end, the rear gets modified with every insert.
Algorithm
/* Allocate space for ITEM to be inserted. */
1. NEWPTR = new NODE
2. NEWPTR->INFO = ITEM; NEWPTR->LINK = NULL
/*INSERT IN THE QUEUE */
3. IF REAR = NULL then front = rear = NEWPTR
4. Else rear->link = NEWPTR; rear = NEWPTR;
5. end
Deletion in a linked Queue. Deletions in a linked queue take place
from the front end. Therefore the front gets modified with every delete.
Algorithm Deletion from a linked Queue.
1. If front = NULL then
2. Print Queue is empty.
3. Else
{
4. ITEM = front->INFO
5. If front = rear then
6. Front = rear = NULL
7. Else
8. Front = front->link
}
9. end`
# include <iostream.h>
# include <conio.h>
void main()
{
int ch, n;
do
{
cout << "nMAIN MENUn 1. Insert.
nn 2. Delete.nn3. Exit";
cout << "nEnter Choice ( 1 - 3 ) ?. ";
cin >> ch;
if (ch == 1)
{
cout << "nnEnter the No "; cin >> n;
insert(n);
}
else if (ch==2)
{
n = del();
if (n==-1)
cout << "nnQueue is empty.";
else
cout << "nThe Deleted No is " << n;
}
}while(ch != 3);
}
int del()
{
int item=-1; node *x;
if(front==NULL) return -1;
item = front->num; front= front->next;
x = front; delete x; return item;
void insert(int item)
{
if (rear == NULL)
front = rear = new node;
else
{
rear->next = new node;
rear = rear->next;
}
rear->num = item; rear->next= NULL;
}
struct node
{
int num; node *next;
} *front=NULL, *rear=NULL;
Variations in QUEUE. Two popular variations of queues are
Circular Queues and Dequeue (Double ended Queues).
Circular Queues are the queues implemented in circular form
rather than a strait line. Circular Queues overcome the problem of
unutilized space in linear queues implemented as arrays
Dequeue ( double ended queues ) are the refined
queues in which elements can be added or removed at either end
but not in the middle. There are two variations of dequeue Input
restricted Dequeue and Output restricted Dequeue.
An input restricted Dequeue which allows insertions at only one
end but allows deletions at both end of the list. An output
restricted dequeue is a dequeue which allows deletions at only
one end of the list but allows insertions at both ends of the list.
Circular Queues are the queues implemented in circular form rather than a
strait line. Circular Queues overcome the problem of unutilized space in linear
queues implemented as arrays
INSERTION
Advance Rear.
Assign Data at
Advanced Position.
Click Here FRONT = -1
REAR = -1
0
0
FRONT
1234
Q [0]
Q [1]
Q [2]
Q [3]
Q [4]
10
20
30
40
50
REAR
FRONT = 0 AND REAR = SIZE – 1
QUEUE IS FULL
After deletion of 0th element (10), the Queue is
Q[1] = 20, Q [ 2 ] = 30, Q [ 3] = 40, Q [ 4 ] = 50 Q [ 0 ] is free
Insert new DATA (60) at Q [ 0 ]
INSERTION
Now Rear = 4, SIZE = 5.
REAR = ( REAR + 1 ) % SIZE
( 4 + 1 ) % 5 = 0
Q [ 0 ] = 60
Click Here
FRONT = -1
REAR = -1
1
4
FRONT
0
Q [0]
Q [1]
Q [2]
Q [3]
Q [4]
60
20
30
40
50
QUEUE IS FULL
( REAR + 1 ) % SIZE = FRONT
( 0 + 1 ) % 5 = 1
REAR

REAR = ( REAR + 1 ) % SIZE
REAR
void insert(int n)
{
if ( (front==0 && rear==size-1) || ( (rear+1) % size == front ))
{ cout << "nQueue is Full"; return; }
else
{
if(rear==-1) front = 0;
rear = (rear+1) % size;
Q[rear] = n;
}
}
main()
{
…….
insert ( 10 );
…….
insert ( 20 );
}
Algorithm of INSERTION
If (FRONT=0&&REAR=SIZE -1) OR (REAR+1)%SIZE=FRONT then print “Q is Full.”
Else
if REAR is in Initial state then FRONT become 0.
Increment REAR and Assign DATA at incremented Position of Queue
Insert when Queue is FULL

First Insert 
Latter Insert 

10
 20
 (FRONT=0 &&
OR (REAR + 1
Check if queue already filled or not.
1. If ( FRONT = 0 AND REAR = N – 1 or ( FRONT = REAR + 1 ) then
Print “OverFlow”
else
{
2. If FRONT == NULL then set FRONT = REAR = 0
3. Else if REAR = N – 1 then Set REAR = 0 else REAR = REAR + 1
}
4. Assign Q[REAR] = ITEM
5. end.
Insertions in Circular Queue.
Deletions in a Circular Queue. Algorithm
/* Check if Queue is empty or not */
1. If FRONT = NULL then print “Under Flow “ and return
else
{
2. Set temp = Q [ FRONT ]
3. If FRONT = REAR then FRONT = REAR = NULL
4. Else if FRONT = N – 1 then FRONT = 0 else FRONT = FRONT + 1
}
5. end.
Deletions in Circular Queue.
int del()
{
int x = -1;
If ( front == -1 ) return -1;
x = Q [ front ]; Q [ front ] = 0;
if ( front == rear ) { front = rear = -1; }
else front = (front+1) % 5;
return x;
}
DELETION
1. If FRONT = -1 then print “Queue is EMPTY” and return FALSE value.
2. Assign Q [ FRONT ] in ‘X’. X = Q [ FRONT ];
3. If FRONT and REAR are equal then FRONT = REAR = -1.
4. Increment FRONT by 1. FRONT = ( FRONT + 1 ) % SIZE.
5. Retuen X.
10
20 30
40
FRONT = 0 REAR = 3
Q [ 0 ]
Q [ 1 ] Q [ 2 ]
Q [ 3 ]
SHOW DELETION
IF FRONT == REAR then
FRONT = REAR = -1
QUEUE BECOMES EMPTY.

FRONT = ( FRONT + 1 ) % SIZE
# include <iostream.h>
# include <conio.h>
int *Q, front = -1, rear = -1, size;
main()
{ int ch, n;
cout << "nEnter the Size “;
cin >> size; Q = new int[size];
do
{
cout << "nMAIN MENUn1. Insert.
n2. Delete.n3. Show.n4. Exit";
cout << "nEnter Your Choice ?. ";
cin >> ch;
if (ch==1)
{
cout << "nEnter the No "; cin >> n;
insert(n);
}
else if (ch==2)
{
n = del();
if ( n == -1 )
cout << "nQueue is empty.";
else
cout<<"nThe Deleted No is"<<n;
}
}while (ch != 4); }
int del()
{
int x = -1;
If ( front == -1 ) return -1;
x = Q [ front ]; Q [ front ] = 0;
if ( front == rear ) { front = rear = -1; }
else front = (front+1) % size;
return x;
}
void insert(int n)
{
if ( (front==0 && rear==size-1) ||
(rear+1) % size ==front )
{ cout << "nQueue is Full"; return; }
else
{
if(rear==-1) front = 0;
rear = (rear+1) % size;
Q[rear] = n;
}
}
65526
65525
65524
65523
65522
65521
65520
65519
65518
65517
65516
65515
65514
65513
65512
65511
65510
65509
p 65524
a=15, b


Show Order 
6
5
5
0
0
6
5
5
0
1
6
5
5
0
2
6
5
5
0
3
6
5
5
0
4
6
5
5
0
5
6
5
5
0
6
6
5
5
0
7
6
5
5
0
8
6
5
5
0
9
6
5
5
1
0
6
5
5
1
1
6
5
5
1
2
6
5
5
1
3
6
5
5
1
4
6
5
5
1
5
6
5
5
1
6
6
5
5
1
7
6
5
5
1
8
6
5
5
1
9
6
5
5
2
0
6
5
5
2
1
6
5
5
2
2
6
5
5
2
3
6
5
5
2
4
6
5
5
2
5
6
5
5
2
6
6
5
5
2
7
 
p 65524
p
4
0
1
5
4
0
1
9
4
0
2
2
FREE A 3.14
10
8
7
9
4012
a
Click Me 
c


c
6


anil
6


Click Me

Show Order 
6
5
5
0
0
6
5
5
0
1
6
5
5
0
2
6
5
5
0
3
6
5
5
0
4
6
5
5
0
5
6
5
5
0
6
6
5
5
0
7
6
5
5
0
8
6
5
5
0
9
6
5
5
1
0
6
5
5
1
1
6
5
5
1
2
6
5
5
1
3
6
5
5
1
4
6
5
5
1
5
6
5
5
1
6
6
5
5
1
7
6
5
5
1
8
6
5
5
1
9
6
5
5
2
0
6
5
5
2
1
6
5
5
2
2
6
5
5
2
3
6
5
5
2
4
6
5
5
2
5
6
5
5
2
6
6
5
5
2
7
25

More Related Content

What's hot

Basics of Python
Basics of PythonBasics of Python
Basics of PythonEase3
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in pythonhydpy
 
Computer science solution - programming - big c plus plus
Computer science   solution - programming - big c plus plusComputer science   solution - programming - big c plus plus
Computer science solution - programming - big c plus plusPraveen Tyagi
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in pythonCeline George
 
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181Mahmoud Samir Fayed
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)shah alom
 
Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319Shaid Bin MD. Sifat
 

What's hot (14)

Basics of Python
Basics of PythonBasics of Python
Basics of Python
 
Datastructures in python
Datastructures in pythonDatastructures in python
Datastructures in python
 
Computer science solution - programming - big c plus plus
Computer science   solution - programming - big c plus plusComputer science   solution - programming - big c plus plus
Computer science solution - programming - big c plus plus
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
Pytho_tuples
Pytho_tuplesPytho_tuples
Pytho_tuples
 
Pytho lists
Pytho listsPytho lists
Pytho lists
 
Arrays in linux
Arrays in linuxArrays in linux
Arrays in linux
 
Python
PythonPython
Python
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181The Ring programming language version 1.5.2 book - Part 18 of 181
The Ring programming language version 1.5.2 book - Part 18 of 181
 
Oracle: PLSQL Introduction
Oracle: PLSQL IntroductionOracle: PLSQL Introduction
Oracle: PLSQL Introduction
 
Pytho dictionaries
Pytho dictionaries Pytho dictionaries
Pytho dictionaries
 
Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)Link list presentation slide(Daffodil international university)
Link list presentation slide(Daffodil international university)
 
Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319Finalpresentationfordatastructure 161207052319
Finalpresentationfordatastructure 161207052319
 

Similar to LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS

Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfKylaMaeGarcia1
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked ListThenmozhiK5
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and searchEstiak Khan
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notesDreamers6
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.pptAlliVinay1
 

Similar to LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS (20)

Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Lec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdfLec-4_Linked-List (1).pdf
Lec-4_Linked-List (1).pdf
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
DS Unit 2.ppt
DS Unit 2.pptDS Unit 2.ppt
DS Unit 2.ppt
 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 
Linked lists 1
Linked lists 1Linked lists 1
Linked lists 1
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Data Structures_Linked List
Data Structures_Linked ListData Structures_Linked List
Data Structures_Linked List
 
Deletion from single way linked list and search
Deletion from single way linked list and searchDeletion from single way linked list and search
Deletion from single way linked list and search
 
Module 3 Dara structure notes
Module 3 Dara structure notesModule 3 Dara structure notes
Module 3 Dara structure notes
 
LINKED LIST.pptx
LINKED LIST.pptxLINKED LIST.pptx
LINKED LIST.pptx
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
Linked list.docx
Linked list.docxLinked list.docx
Linked list.docx
 
Linked list
Linked list Linked list
Linked list
 
ds 4Linked lists.ppt
ds 4Linked lists.pptds 4Linked lists.ppt
ds 4Linked lists.ppt
 
linkrd_list.pdf
linkrd_list.pdflinkrd_list.pdf
linkrd_list.pdf
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 

More from Venugopalavarma Raja

Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and LibrariesVenugopalavarma Raja
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEFUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEVenugopalavarma Raja
 
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCEFUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCEVenugopalavarma Raja
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSVenugopalavarma Raja
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSVenugopalavarma Raja
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCEARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCEVenugopalavarma Raja
 
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCEPOINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCEVenugopalavarma Raja
 

More from Venugopalavarma Raja (13)

Python Modules, Packages and Libraries
Python Modules, Packages and LibrariesPython Modules, Packages and Libraries
Python Modules, Packages and Libraries
 
Python Modules and Libraries
Python Modules and LibrariesPython Modules and Libraries
Python Modules and Libraries
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEFUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
 
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCEFUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
FUNCTIONS IN PYTHON. CBSE +2 COMPUTER SCIENCE
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSFILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
FILE HANDLING IN C++. +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCEARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
ARRAYS IN C++ CBSE AND STATE +2 COMPUTER SCIENCE
 
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCEPOINTERS IN C++ CBSE +2 COMPUTER SCIENCE
POINTERS IN C++ CBSE +2 COMPUTER SCIENCE
 
Be happy Who am I
Be happy Who am IBe happy Who am I
Be happy Who am I
 
Be happy
Be happyBe happy
Be happy
 
Chess for beginers
Chess for beginersChess for beginers
Chess for beginers
 

Recently uploaded

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 

Recently uploaded (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 

LINKED LIST IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS

  • 1. INTRODUCTION. The term ‘list’ refers to a linear collection of data. One form of linear list are arrays. A linked list is a linear collection of data elements, called nodes pointing to the next node by means of a pointer. Each node is divided into 2 parts: the first part containing the information and the second part called ‘link’ or ‘next pointer’ containing the address of the next node in the list. A stack is a linear structure implemented in LIFO (Last In First Out) manner where insertions and deletions are restricted to occur at one end named stack top. LIFO means element last inserted would be the first one to be deleted. The stack is also a dynamic data structure as it can grow or shrink. A queue is also linear structure implemented in FIFO ( First I First Out ) manner where insertions can occur at the ‘rear’ end and ‘deletions’ occur only at ‘front’ end. FIFO means elements are deleted in the same order as they are inserted into. LINKED LIST, STACKS AND QUEUES
  • 2. A linked list consists of several nodes. Name Roll No Next Name Roll No Next Name Roll No Next struct student { char name[20]; int roll_NO; student *next; }; Address 1000 Address 1100 Address 1200 First Node Second Node Third Node Each Node has a pointer, which will point to the location where the next node being stored. Each node consists of Data Part and Link Part. ANIL 100 1100 BABU 101 1200 SUNIL 102 NULL Data Part Link Part  A structure having a member element that refers to the structure itself is called self-referential structure.
  • 3. Name Roll No Next Name Roll No Next Name Roll No Next Address 1000 Address 1100 Address 1200 First Node Second Node Third Node NULL ANIL 100 1100 struct student { char name[20]; int roll_NO; student *prev, *next; }; Data Part Link Part  The linked list are of two types. Singly linked list and doubly linled list. Singly linked list contain node with single pointer pointing to the next node whereas doubly linked list contain two pointer one pointing to previous node and other pointing to the next node in sequence. Address of Previous Node. Address of Next Node. 1000 BABU 101 1200 1100 HARI 102 NULL Prev Prev Prev 
  • 4. NEED FOR LINKED LIST. The linked list overcomes the drawback of arrays as in linked list number of elements need not to be predetermined. More memory can be allocated or released during the process as and when required, making insertions and deletions much easier and simpler. INFO Link INFO Link INFO Link INFO Link Node 1 Node 2 Node 3 Node 4 Node 1 Node 2 Node 3 Node 4 Prev INFO Next Prev INFO Next Prev INFO Next Prev INFO Next Singly linked list. Doubly linked list. Anil 100 67 78 63 208 1100 Babu 101 87 64 73 224 1200 Hari 102 87 64 73 224 NUL Address 1100 Address 1200 Node 1 Node 2 Node 3 Address 1000 
  • 5. Static allocation. This memory allocation technique reserves fixed amount of memory before actual processing take place, there for the number of elements to be stored must be predetermined. Such type of allocation is called static memory allocation. Declarations of variables, arrays are example of static memory allocations. int a,b,c; // Allocate 2 bytes each int a[10]; // Allocate 10 * 2 = 20 Bytes char name[] = “Anil Kumar “; // Allocate 10 bytes float x[10]; // Allocate 10 * 4 = 40 bytes. int a[3][3]; // Allocate 2 * ( 3 * 3 ) = 18. Memory Allocation ( Dynamic vs. Static ). The memory can be allocated in two manners: Dynamically and Statically. Dynamic memory allocation. This technique facilitates allocation of memory during the program execution itself, as and when required. This technique of allocation is called dynamic memory allocation. Dynamic memory allocation also facilitates release of memory. int *a = new int; // Allocate 2 bytes and the address will be assigned in ‘a’ int *a = new int(10); // Allocate 2 bytes and initialize with the value 10. int *a = new int [10]; // Allocate space for 10 integers. ( 10 * 2 = 20 bytes. )
  • 6. When maintaining a dynamic data structure, start pointer is very important. The start pointer is a special pointer which stores the very first address of a linked list. The next pointer of last node stores NULL value which means this is the last node. Singly linked list. Node 4 Node 1 Node 3 Node 2 Node 5 Address 1300 Address 1000 Address 1200 Address 1100 Address 1400 Boy NULLBad 1400 Betty 1100 is 1300 Kuttan 1200 struct node { char name[20]; node * next’ }*start, *temp; temp = start; while ( temp != NULL ) { cout << temp->name ; temp = temp->next ; } Start from 1000  temp = 1000 Start Betty temp = 1100 Kuttan temp = 1200  Is temp = 1300 Bad temp = 1400 Boy temp = NULL
  • 7. Representation of a Linked List in Memory. Simplest implementation of list requires two linear array, one called as INFO for storing information and the other known as LINK for storing address of next INFO. Also a separate variable START is required to store the beginning address of the List. The maintenance of linked list assumes the possibility of insertion and deletion of node using ‘new’ and ‘delete’ operators. A special list consisting of unused memory cells is maintained. This list is called free storage list or free pool. This list (AVAIL list) has its own pointer to the next free node. With every insertion number of free nodes in AVAIL is reduced and with every deletion this number is increased. The method of adding free nodes in free pool is called garbage collection. INFO Index DATA 0 E 1 T 2 3 Y 4 5 6 T 7 8 B 9 LINK LINK Index 6 0 3 1 9 2 NULL 3 7 4 NULL 5 1 6 5 7 0 8 4 9 Start from 8 Click Me   START From 8 AVAIL From 2  Show FREE POOL 
  • 8. Free-store allocation in C++. In C++, every program is provided with a pool of unallocated memory, it may utilize during execution. This memory is known as free storage memory. Free storage memory is allocated through ‘new’ operator, and returns a pointer pointing to the allocated memory. class Node { public: char name[20]; Node *next; }; void main() { } start temp name next Address 10001000 Node * start, * temp; // Click Me start = temp = new Node; // Click Me 1000 Create List  cin >> temp->name; // Click Me  temp->next = new Node; // Click Me  temp = temp->next; // Click Me  temp->next = NULL; // Click Me  cin >> temp->name; // Click Me  temp->next = NULL; // Click MeNULL NULL ANIL ANIL name next Address 20002000 2000 NULL NULL BABU BABU
  • 9. INSERTION. In a linked list new ITEM is either added in the beginning of the list or in the middle of the list or in the end of the list.To add an ITEM at the beginning of the list START is modified to point to the new node and the next of new node points to the previous first node. To add an ITEM in the end of the list the next pointer of the last node is made to point to the new node and the next of new ITEM is made NULL. Node 1 Node 2 Node 3 Node 4 Address 100 Address 200 Address 300 Address 400 JOY NULLANIL 200 HARI 300 BABU 400 X = new NODE; X->NEXT = START; START = X; Click Me START 100 Node 1 Node 2 Node 3 Node 4 Address 100 Address 200 Address 300 Address 400 JOY NULLANIL 200 HARI 300 BABU 400 Address 500 SUNIL 100 500 500 Address 500 SUNIL NULL LAST->NEXT = new NODE; LAST->NEXT->NEXT=NULL;Click Me 500500
  • 10. INSERTION. In a linked list new ITEM is either added in the beginning of the list or in the middle of the list or in the end of the list.To add an ITEM at the beginning of the list START is modified to point to the new node and the next of new node points to the previous first node. To add an ITEM in the end of the list the next pointer of the last node is made to point to the new node and the next of new ITEM is made NULL. start NULL START END X X = new NODE; // Click Me X->Next = START; // Click Me START = x; // Click Me name next ANIL Address 10001000 1000NULL 1000 1000 name next HARI Address 20002000 20001000 2000 2000 name next RAM Address 3000 3000 3000 2000 3000 3000 END->next = new NODE; // Click Me END = END->Next; // Click Me START = END = new NODE; // Click Me name next ANIL NULL Address 1000 name next BIJU NULL Address 2000 name next HARI NULL Address 3000END->Next = NULL; // Click Me  10001000 1000 1000 2000 2000 2000 3000 3000 3000
  • 11. OPERATIONS ON SINGLY LINKED LIST. 2. TRAVERSAL : Traversal means processing all data elements in a data structure. 3. DELETION : Deletion means removal of a data element from a data structure. 4. SEARCHING : Searching means search for a specified data element in a data structure. 1. INSERTION : Insertion means addition of new data element in a data structure. 5. REVERSAL 6. SPLITTING 7. CONCATINATION
  • 12. 1. Define a class or structure contains Information field and Link field. 2. Declare *START and *X of above class type and a character variable ‘ans’. 3. Assign NULL in START. START = NULL 4. Repeat following while ans = ‘Y’ (Yes) using do .. while loop. Allocate new node X. X = new NODE Read information of above node X. Cin >> X->info Assign START in the Link field of X (new node). X->LINK = START Assign X in START. START = X; Print a Caption “Do you want to Continue (Y/N) ?. “ Read answer ans. ans = getche() or cin >> ans; 5. Assign START in X. ( To traverse from first node ) 6. Repeat following while X != NULL Print information fields of X. ( Eg. cout << X->INFO ) Assign LINK field of X in X. X = X->LINK INSERT AT BEGINNING OF THE LIST. ALGORITHM
  • 13. 1. Ptr = START 2. NEWPTR = new node 3. If NEWPTR = NULL then print “No space available” 4. Else { 5. NEWPTR->INFO = ITEM. 6. NEWPTR->LINK = NULL 7. If START = NULL then START = NEWPTR 8. Else { NEWPTR->LINK = START START = NEWPTR } INSERT AT BEGINNING OF THE LIST. ALGORITHM
  • 14. # include <iostream.h> # include <conio.h> struct student { char name[20]; int roll; student *next; }; main() { student *start = NULL, *x; char ans; clrscr(); do { cout << "nnnDo You Want to Continue (Y/N) ?. "; ans = getche(); }while(ans=='y' || ans == 'Y'); x = start; cout << "nnnTraversal of List "; while(x!=NULL) { cout << "nn" << x->name << ", " << x->roll; x = x->next; } getch(); } name next ANIL Address 1000 START NULL X x = new student; // Click Me cout << "nnnPlease Enter Name & Roll No "; cin >> x->name >> x->roll;  x->next = start; // Click Me  start = x; // Click Me  1000 NULL 1000 name next ANIL Address 20002000 1000 2000 name next ANIL Address 30003000 2000 3000
  • 15. # include <iostream.h> # include <conio.h> main() { student *start = NULL, *x; char ans; clrscr(); do { x = new student; x->input(); x->next = start; start = x; cout << "nDo You Want to Continue (Y/N)?. "; ans = getche(); }while(ans=='y' || ans == 'Y'); x = start; cout << "nnnTraversal of List "; while(x!=NULL) { x->output(); x = x->next; } getch(); } class student { char name[20]; int roll; public: student *next; void input() { cout << "nEnter Name & Roll No "; cin >> name >> roll; } void output() { cout << "nn" << name << ", " << roll; } };
  • 16. # include <iostream.h> # include <conio.h> main() { student *start = NULL, *x; char ans; do { x = create_node(); insert_beg(x); cout << "nYou Want to Continue?."; ans = getche(); }while(ans=='y' || ans == 'Y'); cout << "nnnTraversal of List "; display(start); getch(); } struct student { char name[20]; int roll; public: student *next; }*start=NULL; student * create_node() { student *x = new student; cout << "nPlease Enter Name & Roll No "; cin >> x->name >> x->roll; return x; } void insert_beg(student *x) { x->next = start; start = x; } void display(student *x) { while(x!=NULL) { cout << "nn" << x->name << ", " << x->roll; x = x->next; } }
  • 17. INSERT AT THE END OF THE LIST. ALGORITHM 1. Define a class or structure ‘NODE’ contains Information Field and Link Field. 2. Declare 2 pointers *START = NULL and *LAST of type NODE and char ans. 3. Repeat following while ans = ‘Y’ using do .. While loop. if START == NULL then { Allocate new NODE and assign Address in START and LAST. E.g START = LAST = new NODE. } else { Allocate new NODE and assign Address in link field of LAST Node. E.g LAST->LINK = new NODE. Assign LAST->LINK in LAST. E.g LAST = LAST->LINK. } Read information of LAST NODE. E.g cin >> LAST->INFO. Assign NULL in the link field of LAST node. E.g LAST->LINK = NULL. 4. Repeat following while START != NULL. /* Traverse from START. */ Print Information Field of START. E.g cout << START->INFO. START = START->LINK.
  • 18. INSERTION AT END 1. Declare pointers START, PTR, NEWPTR, REAR 2. PTR = START 3. NEWPTR = new NODE 4. If newptr = NULL then print “No Space avilable.” and exit 5. Else NEWPTR->LINK = NULL 6. If START = NULL then START = REAR = NEWPTR 7. Else REAR->LINK = NEWPTR; and REAR = NEWPTR INSERT AT THE END OF THE LIST. ALGORITHM
  • 19. # include <iostream.h> # include <conio.h> main() { student *start = NULL, *x; char ans; do { x = create_node(); insert_end(x); cout << "nYou Want to Continue?."; ans = getche(); }while(ans=='y' || ans == 'Y'); cout << "nnnTraversal of List "; display(start); getch(); } struct student { char name[20]; int roll; public: student *next; }*start=NULL, *end = NULL; student * create_node() { student *x = new student; cout << "nPlease Enter Name & Roll No "; cin >> x->name >> x->roll; x->next = NULL; return x; } void insert_end(student *x) { if(start == NULL) { start = end = x; } else { end->next = x; end = x; } } void display(student *x) { while(x!=NULL) { cout << "nn" << x->name << ", " << x->roll; x = x->next; } }
  • 20. Deletion. Deletion of node from a linked list involves (i) Search for ITEM in the list for availability. (ii) if available, make its previous node point to its next node. Node 1 Node 2 Node 3 Address 100 Address 200 Address 300 ANIL 200 HARI 300 BABU 400 Address 500 SUNIL NULL Node 5 Address 500 Node 4 Address 400 JOY 500 FIND JOY Click Me DELETE JOY Click Me   OY 500 Case 1. If the node to be deleted happens to be the first node, then START is made to point the second node in sequence and deleted node is added in the free-store. DELNODE = START; START = DELNODE->NEXT; DELETE DELNODE; In deletion of a node, there are 3 posibilities. Case 3. If the node is at the end of the list, then its previous node X will point to NULL. DELNODE = X->NEXT; X->NEXT = NULL; DELETE DELNODE; Case 2. If the node happens to be in the middle of the list, its previous pointer is saved in X and if the node’s pointer is PTR then X->LINK = PTR->LINK; DELNODE = X->NEXT; X->NEXT = DELNODE->NEXT; DELETE DELNODE; X becomes 300. Node to be Delete Y = X->next(400) TO DELETE ‘Y’ X->next = Y->next Or X->next = X->next->next delete Y X becomes 300. Node to be Delete Y = X->next(400) TO DELETE ‘Y’ X->next = Y->next Or X->next = X->next->next delete Y
  • 21. /* First of all initialize pointers */ 1. if START = NULL then print “Under flow” 2. Else PTR = START; START = PTR->LINK; delete PTR 3. END DELETION FROM THE BEGINNING OF LIST. ALGORITHM
  • 22. # include <iostream.h> # include <conio.h> main() { student *x; int ch; clrscr(); do { cout << "nnn1. Insert Node.nn2. Delete First Node.nn3. Display.nn4. Exit "; cin >> ch; if(ch==1) create_node(); else if(ch==2) delete_first(); else if(ch==3) display(start); }while(ch!=4); } struct student { char name[20]; int roll; student *next; }*start=NULL; void create_node() { student *x = new student; cout << "nEnter Name & Roll No "; cin >> x->name >> x->roll; x->next = start; start = x; } void delete_first() { student *delnode = start; start = start->next; delete delnode; } void display(student *x) { while(x!=NULL) { cout << "nn" << x->name << ", " << x->roll; x = x->next; } }
  • 23. Traversal of a linked list means processing all the nodes of the list one by one. Following algorithm traverses a list to print all the elements of it. /* Initialize counters */ 1. PTR = START 2. Repeat steps 3 and 4 while PTR != NULL 3. Print PTR->INFO 4. PTR = PTR->LINK 5. end. TRAVERSAL. ALGORITHM
  • 24. STACKS. Logically, a Stack is a LIFO structure and physically it can be implemented as an array or as a linked list. A Stack implemented as an array inherits all the properties of an array and if implemented as a linked list, all characteristics of a linked list are possed by it. But whatever way a stack may be implemented, insertions and deletions occur at the top only. An insertion in a Stack called PUSHING and a deletion from a Stack is called POPPING. 3 2 1 Top Show Pushing  6 Show Poping  5 4 Array for storing stack elements. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] Top Show Pushing  Show Poping 
  • 25. STACK ( ARRAY ). ALGORITHM 1. Receive Data as an Ergument. 2. If TOP == N – 1 then Print ‘Stack if Full. ( Over Flow ) and return. 3. Else Increment TOP by 1. Assign the Data at TOPth position of the Array S. 1. If TOP == – 1 then Print ‘Stack is Empty. ( Under Flow ) and return FALSE. 2. Else Assign TOPth element in X. ( X = S [ TOP ] Decrement TOP by 1. Return value of X. PUSH POP
  • 26. /* Initialize top with invallid subscript value to show that it is empty. 1. Top = -1 2. Read Item /* Item which is to be pushed. */ 3. If ( top == N – 1) then print “Over flow.” 4. Else top = top + 1. /* Increment top to move to next empty position. 5. Stack [ top ] = Item 6. end. STACK ( ARRAY ) – PUSH. ALGORITHM
  • 27. Deletion of an data from stack removes the element at top most position Algorithm /* Firstly, check for underflow condition */ 1. If Top == -1 then print “Underflow” and exit 2. Else print Stack [ Top ] 3. Top = Top – 1 4. end. STACK ( ARRAY ) – POP. ALGORITHM
  • 28. STACK ( ARRAY ). ALGORITHM 1. Declare int array S[10], TOP = -1, No ‘N’ and menu choice ‘ch’ 2. Repeat following while ch != 4 a. Print Menu Options 1. Push, 2. Pop, 3. Exit. b. Input Choice (ch) ( 1 – 3 ) c. If (ch==1) then Input number. Invoke the function PUSH by giving N E.g PUSH(N) d. Else if ( ch == 2 ) then Invoke the function POP() and assign the returned value in ‘N’ if ( N == -1 ) then print “Stack is Empty.” else Print Poped Number ‘N’.
  • 29. # include <iostream.h> # include <conio.h> S = new int[n]; do { cout << "nn1. PUSH.n2. POP.n3. Exit "; cout << "nnEnter Your Choice ( 1- 3 ) ?. "; cin >> ch; switch(ch) { case 1: cout << "nEnter a Number "; cin >> num; push(num); break; case 2: num = pop(); if ( num== -1 ) cout << "nnStack is Empty."; else cout << "nThe Poped No is " << num; break; } }while(ch!=3); } int *S, n, top=-1; void push(int num) { if (top==n-1) cout << "nStack is Full."; else *(S + ++top) = num; } int pop() { int x; if (top==-1) return -1; x = *(S + top--); return x; } main() { int ch, num; clrscr(); cout << "nEnter the Size of the Stack "; cin >> n;
  • 30. A linked list is a dynamic data structure where space requirements needed not be predetermined. The creation of a Stack ( as a linked list ) is same as the creation of a linked list. After getting a node for the ITEM to be inserted, TOP (pointer pointing to the top ) points to the newly inserted node. Algorithm – Pushing in a Linked Stack /* Get new node for the ITEM to be inserted. */ 1. NEWPTR = new NODE 2. NEWPTR->INFO = ITEM; NEWPTR->LINK = NULL; /* Add node at the Top */ 3. If top == NULL then TOP = NEWPTR; 4. Else NEWPTR->LINK = TOP; 5. TOP = NEWPTR; 6. end. STACK ( using LINKED LIST ).
  • 31. Algorithm – Popping from a Linked Stack. 1. If TOP == NULL then Print “Stack Empty.“ 2. Else Print TOP->INFO 3. TOP = TOP->LINK 4. End. STACK ( using LINKED LIST ). ALGORITHM of POP
  • 32. struct student { int num; student *next; }*TOP=NULL; main() { int ch, n; clrscr(); do { cout << "n1. PUSH.n2. POP.n3. Exit "; cout << "nEnter Your Choice ( 1- 3 ) ?. "; cin >> ch; switch(ch) { case 1: cout << "nnEnter a Number "; cin >> n; push(n); break; case 2: n = pop(); if ( n==-1) cout << "nStack is Empty."; else cout << "nThe Poped No is " << n; break; } }while(ch!=3); } int pop() { int x; if ( TOP == NULL ) return -1; student *delnode = TOP; x = TOP->num; TOP = TOP->next; delete delnode; return x; } void push(int n) { student *x = new student; x->num = n; x->next = TOP; TOP = x; } # include <iostream.h> # include <conio.h>
  • 33. Application of STACK. Stacks are basically applied where LIFO scheme is required. A simple example of stack is reversal of a given line. We can accomplish this task by pushing each character on to a stack as it read. When the line is finished, characters are then popped off the stack, and they will be come off in the reverse order. RShow Pushing  Show Poping E V E R S A L O F L I N E When the operator is placed in between operands, such notation is called ‘infix’. Example of infix notation. A + B 10 + 20 Answer 30 When the operator is placed before its operand, such notation is called ‘prefix’. Example of pre-fix notation. + A B + 10 20 Answer 30 When the operator is placed after its operand, such notation is called ‘postfix’. Example of post-fix notation. A B + 10 20 + Answer 30 POLISH STRING refers to the notation in which the operator symbol is placed either before its operand ( pre–fix ) or after its operand ( post–fix ).
  • 34. Infix Pre-fix Post-Fix A + B + A B A B + (A – C) * B * - A C B A C – B * A + ( B * C ) + A * B C A B C * + ( A + B ) / ( C – D ) / + A B – C D A B + C D – / (A+ ( B*C )) / (C – (D * B )) / + A * B C – C * D B A B C * + C D B * - / Conversion of expressions. While evaluating an infix expression, there is an evaluation order. 1. Brackets or Parenthesis 2. Exponentiation. 3. Multiplication or Devision 4. Addition or Subtraction. The Operators with same priority are evaluated from left to right. The steps to convert an infix expression to post-fix expression. 1. Determine the actual evaluation order by inserting Brackets. 2. Convert the expression in the inner most braces into post fix notation by putting the operator after the operands. 3. Repeat Steps 2 until entire expression is converted into post-fix notation. Show Post fix  A + B+ B
  • 35. Steps Expression on each steps Example 10.1 1 2 3 4 Convert the expression in the inner most braces into post fix notation by putting the operator after the operands. ( ( A + B ) * C ) / D ( ( A B + ) * C ) / D ( ( A B + C * ) ) / D A B + C * D / Click Here  Click Here  Click Here  Steps ( ( A + B ) * C / D + E  F ) / G 1 2 3 4 5 6 7 ( ( ( ( A + B ) * C ) / D ) + ( E  F ) ) / G Click Here  Click Here  Click Here  Click Here  ( ( ( ( A B + ) * C ) / D ) + ( E  F ) ) / G ( ( ( A B + C * ) / D ) + ( E  F ) ) / G ( ( A B + C * D / ) + ( E  F ) ) / G ( ( A B + C * D / ) + ( E F  ) ) / G ( A B + C * D / E F  + ) / G A B + C * D / E F  + G / Click Here  Click Here  Click Here  Show Post fix A + B+ B
  • 36. Steps A * ( B + ( C + D ) * ( E + F ) / G ) * H 1 2 3 4 5 6 7 ( A * ( B + ( ( C + D ) * ( E + F ) ) / G ) ) * H Click Here  Click Here  Click Here  Click Here  ( A * ( B + ( ( C D + ) * ( E F + ) ) / G ) ) * H ( A * ( B + ( C D + E F + * ) / G ) ) * H ( A * ( B + ( C D + E F + * G / ) ) ) * H ( A * ( B C D + E F + * G / + ) ) * H ( A B C D + E F + * G / + * ) * H A B C D + E F + * G / + * H * Click Here  Click Here  Click Here  Steps A + [ ( B + C ) + ( D + E ) * F ] / G 1 2 3 4 5 6 7 A + [ ( ( B + C ) + ( ( D + E ) * F ) ) / G ] Click Here  Click Here  Click Here  Click Here  A + [ ( ( B + C ) + ( ( D E + ) * F ) ) / G ] A + [ ( ( B + C ) + ( D E + F * ) ) / G ] A + [ ( ( B C + ) + ( D E + F * ) ) / G ] A + [ ( B C + D E + F * + ) / G ] A + [ B C + D E + F * + G / ] A B C + D E + F * + G / + Click Here  Click Here  Click Here 
  • 37. Steps NOT A OR NOT B AND NOT C 1 2 3 4 5 6 ( NOT A ) OR ( ( NOT B ) AND ( NOT C ) ) Click Here  Click Here  Click Here  Click Here  ( NOT A ) OR ( ( B NOT ) AND ( NOT C ) ) ( NOT A ) OR ( ( B NOT ) AND ( C NOT ) ) ( NOT A ) OR ( B NOT C NOT AND ) ( A NOT ) OR ( B NOT C NOT AND ) A NOT B NOT C NOT AND OR Click Here  Click Here  Convert NOT A OR NOT B NOT C. ( Priority Order NOT AND OR )
  • 38. Suppose X is an infix expression. Finds the equivalent Post fix expression Y 1. Push ‘(’ onto stack and add ‘)’ to the end of X 2. Scan X from left and repeat steps 3 to 6 for each element of X until stack is empty. 3. If an Operand is encountered add it to Stack. 4. If a left parenthesis is encountered push it on Stack. 5. If an operator is encountered, then Repeatedly pop from STACK and add to Y each operator ( on the top of STACK) which has same precedence as or higher than Operator. Add operator to STACK. 6. If a right parenthesis is encountered then Repeatedly pop from STACK and add to Y each operator ( on the top of STACK ) until a left parenthesis is encountered. Remove the left parenthesis. ( Do not add the left parenthesis to Y ) 7. end. INFIX TO POSTFIX CONVERSSION.
  • 39. Convert X : A + ( B * C – ( D / E  F ) * G ) * H Symbol Stack Post-fix Expression Y A + ( B * C – ( D / E  F ) * G ) * H START  A A + + A + ( A + ( A B + ( * A B + ( * A B C + ( – A B C * * + ( – ( A B C * + ( – ( A B C * D + ( – ( / A B C * D + ( – ( / A B C * D E + ( – ( /  A B C * D E + ( – ( /  A B C * D E F + ( – A B C * D E F  / + ( – * A B C * D E F  / + ( – * A B C * D E F  / G + A B C * D E F  / G * – + * A B C * D E F  / G * – + * A B C * D E F  / G * – H A B C * D E F  / G * – H * +
  • 40. # include <iostream.h> # include <ctype.h> char S[50]; int size = 50, top = -1; main() { char infix[50], post[50]; int i, j; cout << "n Enter Infix Expression "; cin >> infix; for(i=j=0; infix[i] != NULL; i++) { if ( isalpha(infix[i]) ) post[j++] = infix[i]; else if ( infix[i] == '(' ) push ( '(' ); else if ( infix[i] == ')' ) { while ( topele() != '(' && topele() != NULL ) { post[j++] = pop(); } pop(); } else if (infix[i]=='^‘ || infix[i] == '*' || infix[i]=='/‘ || infix[i] == '+' || infix[i] == '-' ) { while ( presce(topele()) >= presce(infix[i]) && topelement() != NULL ) { post[j++] = pop(); } push(infix[i]); } } while ( topele() != NULL ) post[j++] = pop(); post[j] = NULL; cout << "nnPost-fix expression is " << post; } char topele() { return ( top == -1 ) ? NULL : S[top]; } char pop() { char x; if ( top == -1 ) return NULL; x = S[top]; top--; return x; } void push(char opr) { if ( top == size - 1 ) return; else S[++top] = opr; } int prece (char opr) { if (opr == '^') return 10; else if (opr == '*' || opr == '/' ) return 9; else if (opr == '+' || opr == '-' ) return 8; return 0; }
  • 41. An infix expression is difficult for the machine to know and keep track of precedence of operators. On the other hand a postfix expression itself determines the precedence of operators ( as the placement of operators in a post fix expression depends upon its precedence ). Therefore for the machine it is easier to carry out a postfix expression than an infix expression. Evaluation of Postfix expression. As postfix expression is without parenthesis and can be evaluated as two operands and an operator at a time. Evaluation rule of a postfix expression states : While reading the expression from left to right, push the element in the stack if it is an operand. Pop the two operands from stack, if the element is an operator ( except NOT operator ). In the case of NOT operator, POP one operand from the stack and then evaluate it ( two operands and an operator ). Push back the result of the expression. Repeat it till end of the expression. For a binary operator, two operands are popped from stack and for a unary operator, one operand is popped. Then the result is calculated using operand(s) and the operator, and pushed back into the stack.
  • 42. Evaluation of Postfix Expression. Algorithm /* Reading of expression takes place from left to right */ 1. Read the element. 2. If the element is operand then push the element in STACK 3. If the element is operator then 4. Pop two operands from stack ( POP one operand in case of unary operators ) 5. Evaluate the expression formed by the 2 operands and the operator. 6. Push the result of the expression in the STACK. 7. If no more elements then POP the result else go to step 1. 8. end. EVALUATION OF POSTFIX EXPRESSION.
  • 43. Evaluate the postfix expression A B + C * D / if A = 2, B = 3, C = 4 and D = 5 Element Action Taken Intermediate Calculation. Stack       A ( 2 ) PUSH 2 None 2 B ( 3 ) PUSH 3 None 2 3 + POP 3 & 2 3 + 2 = 5 5 C ( 4 ) PUSH 4 None 5 4 * POP 4 & 5 5 * 4 = 20 20 D ( 5 ) PUSH 5 None 20 5 / POP 5 & 20 20 / 5 = 4 4 Evaluate the following expression in postfix form using a stack and show the contents of the stack after execution of each operation. Read each elements from left to right, If it is an operand then PUSH it in Stack If it is an operator then POP 2 values from the stack. Perform Operation and Push back the Result Click all these buttons
  • 44. Evaluate the expression 5 6 2 + * 12 4 / – Element Action Taken Intermediate Calculation. Stack END RESULT 37       5 PUSH 5 None 5 6 PUSH 6 None 5 6 2 Push 2 3 + 2 = 5 5 6 2 + POP 2 AND 6 6 + 2 = 8 5 8 * POP 8 & 5 5 * 8 = 40 40 12 PUSH 12 None 40 12 4 PUSH 4 None 40 12 4 / POP 4 AND 12 12 / 4 = 3 40 3 - POP 3 AND 40 40 - 3 = 37 37
  • 45. TRUE FALSE TRUE NOT FALSE TRUE OR NOT AND OR AND. Element Action Taken Intermediate Calculation. Stack           TRUE PUSH ‘TRUE’ None TRUE FALSE PUSH ‘FALSE’ None TRUE FALSE TRUE PUSH ‘TRUE’ None TRUE FALSE TRUE NOT POP ‘TRUE’ FALSE TRUE FALSE FALSE FALSE PUSH ‘FALSE’ None TRUE FALSE FALSE FALSE TRUE PUSH ‘TRUE’ None TRUE FALSE FALSE FALSE TRUE OR POP ‘TRUE & FALSE’ TRUE TRUE FALSE FALSE TRUE NOT POP ‘TRUE’ FALSE TRUE FALSE FALSE FALSE AND POP ‘FALSE & FALSE’ FALSE TRUE FALSE FALSE OR POP ‘FALSE & FALSE’ FALSE TRUE FALSE AND POP ‘TRUE & FALSE’ FALSE FALSE TRUTH TABLE FALSE AND FALSE = FALSE FALSE AND TRUE = FALSE TRUE AND FALSE = FALSE TRUE AND TRUE = TRUE
  • 46. void main() { char post[50]; int i, a, b, res; cout << "nEnter Post-fix Expression "; cin.getline(post,49); cout << "nGiven Expression is " << post; # include <iostream.h> # include <conio.h> # include <ctype.h> # include <math.h> char S[50]; int size, top = -1; void push(char opr) { if ( top == size - 1 ) return; else S[++top] = opr; } int pop() { char x; if ( top == -1 ) return NULL; x = S[top]; top--; return x-48; } for(i=0; post[i] != NULL; i++) { if ( post[i] >= '0' && post[i] <= '9' ) push(post[i]); else if (post[i] == '^' || post[i] == '/' || post[i] == '*' || post[i] == '+' || post[i] == '-') { b = pop(); a = pop(); if (post[i] == '^') res = pow(a,b); else if (post[i] == '*' ) res = a*b; else if (post[i] == '/' ) res = a/b; else if (post[i] == '+' ) res = a+b; else res = a - b; push(res+48); } } res = pop(); cout << "nnnResult is " << res; getch(); }
  • 47. QUEUE. Logically a Queue is a FIFO structure and physically it can be implemented either as an array or as a linked list. Whatever way a Queue is implemented, insertions take place at the ‘rear’ end and the deletions at the ‘front’ end. Queue as an Array. When a Queue is created as an array, its number of elements is declared before processing. The beginning of the array becomes its ‘front’ end and end of the array becomes its ‘rear’ end. ‘Font’ stores the index of the first element in the queue and ‘rear’ stores the index of last element in the Queue. The number of elements in the Queue at any time can be calculated from the values of the ‘front’ and the ‘rear’ If ‘front’ = 0 then no. of elements = 0 else no. of elements = front – rear + 1 QUEUE
  • 48. Insertion in Array – Queue 1. If rear = NULL then rear = front = 0 and set ITEM in 0th index of Array QUEUE. 2. Else if rear = N – 1 then print “QUEUE IS FULL, OVERFLOW” 3. Else QUEUE [ rear + 1 ] = ITEM and rear = rear + 1. 4. end. Deletion in Array Queue. 1. If front = NULL then print Queue is empty. 2. Else ITEM = Queue [front] and if front = rear then front = rear = NULL else front = front + 1 3. end. QUEUE
  • 49. # include <iostream.h> # include <conio.h> int *Q, size, front = -1, rear = -1; void main() { int ch, n; cout << "nnnPlease Enter the Size "; cin >> size; Q = new int[size]; do { cout << "nMAIN MENUnnn1. Insert. nn2. Delete.nn3. Quit"; cout << "nEnter your Choice (1 - 3) ?. "; cin >> ch; if (ch==1) { cout << "nEnter a No "; cin >> n; insert(n); } else if (ch==2) { n = del(); if (n == -1) cout << "nQueue is Empty."; else cout << "nThe Deleted No is " << n; } }while(ch != 3); } int del() { int x; if ( front == -1 ) return -1; else { x = Q[front]; if (front == rear) front = rear = -1; else front++; return x; } } void insert( int n ) { if ( rear == -1 ) { front = rear = 0; Q[rear] = n; } else if ( rear == size - 1) cout << "nnQueue is Full."; else Q[++rear] = n; }
  • 50. INSERTION IN A LINKED QUEUE. Linked Queues are the queues having link among its elements. Two pointers are mentioned to store ‘front’ position and ‘rear’ position. Insertion in a Linked Queue. Insertion in a linked queue also take place only at the rear end, the rear gets modified with every insert. Algorithm /* Allocate space for ITEM to be inserted. */ 1. NEWPTR = new NODE 2. NEWPTR->INFO = ITEM; NEWPTR->LINK = NULL /*INSERT IN THE QUEUE */ 3. IF REAR = NULL then front = rear = NEWPTR 4. Else rear->link = NEWPTR; rear = NEWPTR; 5. end
  • 51. Deletion in a linked Queue. Deletions in a linked queue take place from the front end. Therefore the front gets modified with every delete. Algorithm Deletion from a linked Queue. 1. If front = NULL then 2. Print Queue is empty. 3. Else { 4. ITEM = front->INFO 5. If front = rear then 6. Front = rear = NULL 7. Else 8. Front = front->link } 9. end`
  • 52. # include <iostream.h> # include <conio.h> void main() { int ch, n; do { cout << "nMAIN MENUn 1. Insert. nn 2. Delete.nn3. Exit"; cout << "nEnter Choice ( 1 - 3 ) ?. "; cin >> ch; if (ch == 1) { cout << "nnEnter the No "; cin >> n; insert(n); } else if (ch==2) { n = del(); if (n==-1) cout << "nnQueue is empty."; else cout << "nThe Deleted No is " << n; } }while(ch != 3); } int del() { int item=-1; node *x; if(front==NULL) return -1; item = front->num; front= front->next; x = front; delete x; return item; void insert(int item) { if (rear == NULL) front = rear = new node; else { rear->next = new node; rear = rear->next; } rear->num = item; rear->next= NULL; } struct node { int num; node *next; } *front=NULL, *rear=NULL;
  • 53. Variations in QUEUE. Two popular variations of queues are Circular Queues and Dequeue (Double ended Queues). Circular Queues are the queues implemented in circular form rather than a strait line. Circular Queues overcome the problem of unutilized space in linear queues implemented as arrays Dequeue ( double ended queues ) are the refined queues in which elements can be added or removed at either end but not in the middle. There are two variations of dequeue Input restricted Dequeue and Output restricted Dequeue. An input restricted Dequeue which allows insertions at only one end but allows deletions at both end of the list. An output restricted dequeue is a dequeue which allows deletions at only one end of the list but allows insertions at both ends of the list.
  • 54. Circular Queues are the queues implemented in circular form rather than a strait line. Circular Queues overcome the problem of unutilized space in linear queues implemented as arrays INSERTION Advance Rear. Assign Data at Advanced Position. Click Here FRONT = -1 REAR = -1 0 0 FRONT 1234 Q [0] Q [1] Q [2] Q [3] Q [4] 10 20 30 40 50 REAR FRONT = 0 AND REAR = SIZE – 1 QUEUE IS FULL
  • 55. After deletion of 0th element (10), the Queue is Q[1] = 20, Q [ 2 ] = 30, Q [ 3] = 40, Q [ 4 ] = 50 Q [ 0 ] is free Insert new DATA (60) at Q [ 0 ] INSERTION Now Rear = 4, SIZE = 5. REAR = ( REAR + 1 ) % SIZE ( 4 + 1 ) % 5 = 0 Q [ 0 ] = 60 Click Here FRONT = -1 REAR = -1 1 4 FRONT 0 Q [0] Q [1] Q [2] Q [3] Q [4] 60 20 30 40 50 QUEUE IS FULL ( REAR + 1 ) % SIZE = FRONT ( 0 + 1 ) % 5 = 1 REAR  REAR = ( REAR + 1 ) % SIZE REAR
  • 56. void insert(int n) { if ( (front==0 && rear==size-1) || ( (rear+1) % size == front )) { cout << "nQueue is Full"; return; } else { if(rear==-1) front = 0; rear = (rear+1) % size; Q[rear] = n; } } main() { ……. insert ( 10 ); ……. insert ( 20 ); } Algorithm of INSERTION If (FRONT=0&&REAR=SIZE -1) OR (REAR+1)%SIZE=FRONT then print “Q is Full.” Else if REAR is in Initial state then FRONT become 0. Increment REAR and Assign DATA at incremented Position of Queue Insert when Queue is FULL  First Insert  Latter Insert   10  20  (FRONT=0 && OR (REAR + 1
  • 57. Check if queue already filled or not. 1. If ( FRONT = 0 AND REAR = N – 1 or ( FRONT = REAR + 1 ) then Print “OverFlow” else { 2. If FRONT == NULL then set FRONT = REAR = 0 3. Else if REAR = N – 1 then Set REAR = 0 else REAR = REAR + 1 } 4. Assign Q[REAR] = ITEM 5. end. Insertions in Circular Queue.
  • 58. Deletions in a Circular Queue. Algorithm /* Check if Queue is empty or not */ 1. If FRONT = NULL then print “Under Flow “ and return else { 2. Set temp = Q [ FRONT ] 3. If FRONT = REAR then FRONT = REAR = NULL 4. Else if FRONT = N – 1 then FRONT = 0 else FRONT = FRONT + 1 } 5. end. Deletions in Circular Queue.
  • 59. int del() { int x = -1; If ( front == -1 ) return -1; x = Q [ front ]; Q [ front ] = 0; if ( front == rear ) { front = rear = -1; } else front = (front+1) % 5; return x; } DELETION 1. If FRONT = -1 then print “Queue is EMPTY” and return FALSE value. 2. Assign Q [ FRONT ] in ‘X’. X = Q [ FRONT ]; 3. If FRONT and REAR are equal then FRONT = REAR = -1. 4. Increment FRONT by 1. FRONT = ( FRONT + 1 ) % SIZE. 5. Retuen X. 10 20 30 40 FRONT = 0 REAR = 3 Q [ 0 ] Q [ 1 ] Q [ 2 ] Q [ 3 ] SHOW DELETION IF FRONT == REAR then FRONT = REAR = -1 QUEUE BECOMES EMPTY.  FRONT = ( FRONT + 1 ) % SIZE
  • 60. # include <iostream.h> # include <conio.h> int *Q, front = -1, rear = -1, size; main() { int ch, n; cout << "nEnter the Size “; cin >> size; Q = new int[size]; do { cout << "nMAIN MENUn1. Insert. n2. Delete.n3. Show.n4. Exit"; cout << "nEnter Your Choice ?. "; cin >> ch; if (ch==1) { cout << "nEnter the No "; cin >> n; insert(n); } else if (ch==2) { n = del(); if ( n == -1 ) cout << "nQueue is empty."; else cout<<"nThe Deleted No is"<<n; } }while (ch != 4); } int del() { int x = -1; If ( front == -1 ) return -1; x = Q [ front ]; Q [ front ] = 0; if ( front == rear ) { front = rear = -1; } else front = (front+1) % size; return x; } void insert(int n) { if ( (front==0 && rear==size-1) || (rear+1) % size ==front ) { cout << "nQueue is Full"; return; } else { if(rear==-1) front = 0; rear = (rear+1) % size; Q[rear] = n; } }
  • 61.
  • 62. 65526 65525 65524 65523 65522 65521 65520 65519 65518 65517 65516 65515 65514 65513 65512 65511 65510 65509 p 65524 a=15, b   Show Order  6 5 5 0 0 6 5 5 0 1 6 5 5 0 2 6 5 5 0 3 6 5 5 0 4 6 5 5 0 5 6 5 5 0 6 6 5 5 0 7 6 5 5 0 8 6 5 5 0 9 6 5 5 1 0 6 5 5 1 1 6 5 5 1 2 6 5 5 1 3 6 5 5 1 4 6 5 5 1 5 6 5 5 1 6 6 5 5 1 7 6 5 5 1 8 6 5 5 1 9 6 5 5 2 0 6 5 5 2 1 6 5 5 2 2 6 5 5 2 3 6 5 5 2 4 6 5 5 2 5 6 5 5 2 6 6 5 5 2 7   p 65524 p 4 0 1 5 4 0 1 9 4 0 2 2 FREE A 3.14 10 8 7 9 4012
  • 63. a Click Me  c   c 6   anil 6   Click Me  Show Order  6 5 5 0 0 6 5 5 0 1 6 5 5 0 2 6 5 5 0 3 6 5 5 0 4 6 5 5 0 5 6 5 5 0 6 6 5 5 0 7 6 5 5 0 8 6 5 5 0 9 6 5 5 1 0 6 5 5 1 1 6 5 5 1 2 6 5 5 1 3 6 5 5 1 4 6 5 5 1 5 6 5 5 1 6 6 5 5 1 7 6 5 5 1 8 6 5 5 1 9 6 5 5 2 0 6 5 5 2 1 6 5 5 2 2 6 5 5 2 3 6 5 5 2 4 6 5 5 2 5 6 5 5 2 6 6 5 5 2 7 25