Sei sulla pagina 1di 16

DATA STRUCTURES

PRESENTATION

SUBMITTED BY : REEMA CHAUHAN

ROLL NO. : 2047

SUBMITTED TO : Mr. Dhananjay


TOPICS :

• Implementation of doubly linked list


• Conversion of infix to postfix expression
using stack
• Algorithms for preorder & postorder
traversal
IMPLEMENTATION OF DOUBLY LINKED LIST

Double Linked list is a fundamental data structure used in


computer programming. It consists of a sequence of nodes
connected by two pointers in both directions. Each node is
having three parts the first part contains the address of
previous node and second part contains data and third
part contains address of next node. Different types of
operations can be performed on a doubly linked list :
Insertion,deletion,traversing,search and reverse etc.

START Prev A next Prev B next NULL

NULL
INSERTION CASES
• INSERTION AT THE BEGINNING OF THE LIST

void insert_begin(int i)
{
node *ptr=new node;
ptr->info=i;
ptr->prev=NULL;
if(start==NULL)
{
start=ptr;
start->next=NULL;
}
else
{
ptr->next=start;
start->prev=ptr;
start=ptr;
}
}
• I N S E RT I O N AT T H E E N D O F T H E L I S T

void insert_end(int i)
{
node *ptr=new node;
ptr->info=i;
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
start->prev=NULL;
}
else
{
node *temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=ptr;
ptr->prev=temp;
}
}
• INSERTION AT A SPECIFIED POSITION
void insert_middle(int i)
{
node *ptr=new node;
ptr->info=i;
int loc,k=0;
cout<<"enter element after which you wish to insert"<<endl;
cin>>loc;
node *temp=start;
while(temp!=NULL)
{
if(temp->info==loc)
{
temp->next->prev=ptr;
ptr->next=temp->next;
ptr->prev=temp;
temp->next=ptr;
k=1;
break;
}
else
temp=temp->next;
}
if(k==0)
cout<<"incorrect location";
DELETION CASES
• DELETION AT THE BEGINNING OF THE LIST

void delete_begin()
{
if(start==NULL)
cout<<"deletion not possible"<<endl;
else
{
start=start->next;
start->prev=NULL;
}
}
• D E L E T I O N AT T H E E N D O F T H E L I S T

void delete_end()
{
if(start==NULL)
cout<<"deletion not possible"<<endl;
else
{
node *temp=start;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
}
• DELETION FROM A SPECIFIED POSITION

void delete_middle()
{
int l,k=0;
cout<<"enter location after which you want to delete the element"<<endl;
cin>>l;
node *temp=start;
while(temp->next!=NULL)
{
if(temp->info==l)
{
k=1;
node *p=temp->next->next;
temp->next=p;
p->prev=temp;
break;
}
else
temp=temp->next;
}
if(k==0)
cout<<"incorrect location"<<endl;
}
ALGORITHM FOR CONVERSION OF INFIX TO
POSTFIX EXPRESSION USING STACK
Let Q be any infix expression and we have to convert it to postfix expression P.
For this the following procedure will be followed.
1. Push left parenthesis onto STACK and add right parenthesis at the end of Q.
2. Scan Q from left to right and repeat step 3 to 6 for each element of Q until
the STACK is empty.
3. If an operand is encountered add it to P.
4. If a left parenthesis is encountered push it onto the STACK.
5. If an operator is encountered, then
• Repeatedly pop from STACK and add to P each operator which has same
precedence as or higher precedence than the operator encountered.
• Push the encountered operator onto the STACK.
6. If a right parenthesis is encountered, then
• Repeatedly pop from the STACK and add to P each operator
until a left parenthesis is encountered.
• Remove the left parenthesis and do not add it to P.
7. Exit
EXAMPLE OF INFIX TO POSTFIX CONVERSION :
Infix Expression: A+((B+C)+(D+E)*F)/G
Scanned Symbols Stack Expression P
(
A ( A
+ (+ A
( (+( A
( (+(( A
B (+(( AB
+ (+((+ AB
C (+((+ ABC
) (+( ABC+
+ (+(+ ABC+
( (+(+( ABC+
D (+(+( ABC+D
+ (+(+(+ ABC+D
E (+(+(+ ABC+DE
) (+(+ ABC+DE+
* (+(+* ABC+DE+
F (+(+* ABC+DE+F
) (+ ABC+DE+F*+

/ (+/ ABC+DE+F*+

G (+/ ABC+DE+F*+G

) ABC+DE+F*+G/+

Expression P : A B C + D E + F * + G / +
ALGORITHMS FOR PREORDER AND
POSTORDER TRAVERSAL

• PREORDER TRAVERSAL :
In this traversal method, the root node is visited first, then the left
subtree and finally the right subtree.
Algorithm
void preorder( struct BinaryTreeNode *root )
{
if ( root! = NULL )
{
cout << root -> num;
preorder( root -> left );
preorder( root -> right );
}
}
EXAMPLE OF PREORDER TRAVERSAL :

ROOT We start from A, and


following pre-order
A
traversal, we first visit A
itself and then move to
its left subtree B. B is also
B C
traversed pre-order. The
process goes on until all
the nodes are visited. The
D E F G
output of pre-order
traversal of this tree will
be −
A→B→D→E→C
→F→G
• POSTORDER TRAVERSAL :
In this traversal method, the root node is visited last, hence
the name. First we traverse the left subtree, then the right
subtree and finally the root node.
Algorithm
void postorder( struct BinaryTreeNode *root )
{
if ( root! = NULL )
{
preorder( root -> left );
preorder( root -> right );
cout << root -> num;
}
}
EXAMPLE OF POSTORDER TRAVERSAL :

ROOT We start from A, and


following pre-order
A traversal, we first visit
the left subtree B. B is
also traversed post-
B C order. The process goes
on until all the nodes
are visited. The output
of post-order traversal
D E
of this tree will be −
F G
D→E→B→F→G
→ C →A

Potrebbero piacerti anche