welcome to linked list, stack & queue by vinay alexander pgt computer science) kv jhagrakhand

41
WELCOME TO Linked List, Stack & Queue By VINAY ALEXANDER PGT COMPUTER SCIENCE) KV JHAGRAKHAND

Upload: cullen-wiswell

Post on 16-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

WELCOME TO

Linked List, Stack & QueueBy

VINAY ALEXANDER

PGT COMPUTER SCIENCE)

KV JHAGRAKHAND

Linked-ListA link-list is a linear collection of data elements,

called nodes pointing to the next nodes by means of pointers.

A linked list consists of a series of nodes which are A linked list consists of a series of nodes which are not necessarily adjacent in memory.not necessarily adjacent in memory.

LinkLink

Data Elements

• A stack is a linear structure implemented in LIFO manner where insertions and deletions are restricted to occur only at one end – stack’s top. The stack is also a dynamic data structure as it can grow or shrink.

• A Queue is also a linear dynamic structure implemented in FIFO manner where insertions can occur at the “rear” end, and deletions occur only at “front” end.

• Memory Allocation (Dynamics Vs Static)

• Each data element, stored in the memory ,is given some memory. this process of giving memory is called memory allocation.

• Static memory: This memory allocation technique reserves fixed amount of memory before actual processing takes place, therefore the number of elements to be stored must be predetermined. Example: Array

• Dynamic Memory Allocation: An allocation of memory during the program execution itself, as and when required.Example: Link List

Array vs. Linked List

• An array is a static data structure. It cannot be created nor destroyed during program execution. So there is a lot of memory wastage.

• Size of an array should be known in advance.

• In array, insertion and deletion require lot of shifting.

All these problems are eliminated in linked All these problems are eliminated in linked listlist

4646 4747 4848 4949 5050

3636 3737 3838 3939 4040

2626 2727 2828 2929 3030

1616 1717 1818 1919 2020

66 77 88 99 1010

4141 4242 4343 4444 4545

3131 3232 3333 3434 3535

2121 2222 2323 2424 2525

1111 1212 1313 1414 1515

11 22 33 44 55

(45) (32)

(34) (39) (20)

(11)

(6) (25)

(9)

Example of a Linked ListExample of a Linked List

00000000

00000011

00000022

00000033

00000044

00000055

00000066

00000077

00000088

00000099

000000AA

000000BB

000000CC

000000DD

000000EE

000F000F

00100100

00100111

00100122

00100133

00100144

00100155

00100166

00100177

00100188

00100199

001001AA

001001BB

001001CC

001001DD

001001EE

001F001F

MemoryMemory

Linked list inside the Computer MemoryLinked list inside the Computer Memory

00000000

00000011

00000022

00000033

00000044

00000055

00000066

00000077

00000088

00000099

000000AA

000000BB

000000CC

000000DD

000000EE

000F000F

MemoryMemory

00100100

00100111

00100122

00100133

00100144

00100155

00100166

00100177

00100188

00100199

001001AA

001001BB

001001CC

001001DD

001001EE

001F001F

0002

Linked list inside the Computer MemoryLinked list inside the Computer Memory

00000000

00000011

00000022

00000033

00000044

00000055

00000066

00000077

00000088

00000099

000000AA

000000BB

000000CC

000000DD

000000EE

000F000F

MemoryMemory

00100100

00100111

00100122

00100133

00100144

00100155

00100166

00100177

00100188

00100199

001001AA

001001BB

001001CC

001001DD

001001EE

001F001F

00070002

Linked list inside the Computer MemoryLinked list inside the Computer Memory

00000000

00000011

00000022

00000033

00000044

00000055

00000066

00000077

00000088

00000099

000000AA

000000BB

000000CC

000000DD

000000EE

000F000F

MemoryMemory

00100100

00100111

00100122

00100133

00100144

00100155

00100166

00100177

00100188

00100199

001001AA

001001BB

001001CC

001001DD

001001EE

001F001F

0007

0018

0002

Linked list inside the Computer MemoryLinked list inside the Computer Memory

Singly, Doubly and Circular Linked List

• Free Store Allocation in c++

• In C++ ,Every program is provided with a pool of unallocated memory that it may utilize during execution. this memory is known as free store memory. Free store memory is allocated by applying operator new to a type specifier and which returns a pointer to the allocated memory.

Free Store Allocation in c++struct Node

{ char info;

Node *next;

};

Node *ptr;

ptr = new Node;

To Refered to info part ,we may write ptr->info;

To Refered to next pointer ,we may write ptr->next;

When a node is deleted, it is done as

delete ptr;

Basic Operations on singly linked list: New ITEM is either added in beginning of the list or

in the middle of the list or in the end of the list.

T o add an ITEM in the beginning of the list, START is modified to point to the new node of ITEM and the next pointer of the new node(i.e , ITEM node) points to the previous first node.

T o add an ITEM in the end of the list, nest pointer of the last node is made to point to the new ITEM’s node and the next pointer of the new ITEM/s node is made NULL.

IF you try to insert a node ,when there is no memory available ,it is called “OVERFLOW”

Insertion

Beginning

MID

END

// The algorithm for insert in the beginning of the list is given below.

1. ptr= START // START denotes the first node of the list

2. NEWPTR=new node //allocate memory for new node

3. If NEWPTR=NULL // if sufficient memory is not available

4.Print “ no space available”

5. Else { NEWPTRINFO=ITEM //put information in new node

6. NEWPTRLINK=NULL // initialize the pointer

7.If START=NULL then

8. START=NEWPTR

9. else { Save=START // Save Start’s value-Save is also pointer

10. START=NEWPTR // Assign NEWPTR to START11. NEWPTRLINK=Save// Make NEWPTR point to previous START

}

}

12. END

Insertion in Link List#include<iostream.h>

#include<process.h> // for exit( )

Struct Node

{ int info;

Node *next;

} *start, *newptr, *save, *ptr, *rear;

Node * create_new_node(int);

Void insert_beg(Node *);

Void insert_end(Node *);

Void display(Node *);

Void main( )

{start = NULL; int inf;

cout<<“Enter info for new node”;

cin>>inf;

cout<<“create new node !!”;

newptr= create_new_node(inf);

if(newptr!=NULL)

cout<<“Success”;

else

{ cout<<“Cannot create”; exit(1);}

cout<<“insert new node in the beginning of list”;

insert_beg(newptr);

//Or

insert_end(newptr);

display(start);

}

Insertion in Link ListNode * create_new_node( int n)

{ ptr = new ptr;

ptr->info=n;

ptr->next=NULL;

return ptr;

}

void insert_beg(Node *np)

{ if( start= =NULL) start=np;

else { save=start;

start=np;

np->next=save;}

}

void display( Node *np){ while(np!=NULL) {

cout<<np->info<<“->”;np=np->next;

} cout<<“\n”;}

void insert_end(Node *np){ if( start= =NULL) start=rear=np; else { rear->next=np;

rear=np;}}

//Insertion in the end of list

1.declare pointer START,PTR,NEWPTR ,REAR

2.PTR=START

3. NEWPTR=new Node

4. If NEWPTR=NULL

7Print” No Space available”;

8Exit

9Else

{

10. NEWPTRLINK=NULL

}

11. If START=NULL then

12 START=NEWPR

13 REAR=NEWPTR

}

14. REARLINK=NEWPTR

16 REAR=NEWPTR

16END.

// Deletion from the Beginning of list

//First of all initialize pointers

1.If START= NULL THEN

2. Print “ UNDERFLOW”

Else

{

3. ptr=START

4.START=ptrLINK

5. Delete ptr

}

7.END

Deletion in Link List#include<iostream.h>

#include<process.h>

struct Node

{ int info;

Node *next;

} *start, *newptr, *save, *ptr, *rear;

Node * create_new_node(int);

void insert(Node *);

void display(Node *);

void delnode(Node *);

void main( )

{start = rear = NULL; int inf;

cout<<“Enter info for new node”;

cin>>inf;

cout<<“create new node !!”;

newptr= create_new_node(inf);

if(newptr!=NULL)

cout<<“Success”;

else

{ cout<<“Cannot create”; exit(1);}

cout<<“insert new node in the beginning of list”;

insert(newptr);

display(start);

delnode( );

}

Deletion in Link ListNode * create_new_node( int n)

{ ptr = new ptr;

ptr->info=n;

ptr->next=NULL;

return ptr;

}

void insert(Node *np)

{ if( start= =NULL)

start=rear=np;

else

{

rear->next=np;

rear=np;}

}

void display( Node *np){ while(np!=NULL) {

cout<<np->info<<“->”;np=np->next;

} cout<<“!!!\n”;}

void delnode( ){ if( start= =NULL) cout<<“Underflow”; else { ptr=start;

start=start->next;delete ptr;}

}

Traversal: Traversal of a linked list means processing all the nodes of the list one by one.

Algorithms:

//Initialize counter

1.ptr=start

2. repeat steps 3 and 4 until ptr=null

3. Print ptrINFO

4. ptr=ptrLINK

5. END

Traversal in Link List#include<iostream.h>

#include<process.h>

struct Node

{ int info;

Node *next;

} *start, *newptr, *save, *ptr, *rear;

Node * create_new_node(int);

void insert(Node *);

void traverse(Node *);

void main( )

{start = rear = NULL; int inf;

cout<<“Enter info for new node”;

cin>>inf;

cout<<“create new node !!”;

newptr= create_new_node(inf);

if(newptr!=NULL)

cout<<“Success”;

else

{ cout<<“Cannot create”; exit(1);}

insert(newptr);

traverse(start);

}

Traversal in Link ListNode * create_new_node( int n)

{ ptr = new ptr;

ptr->info=n;

ptr->next=NULL;

return ptr;

}

void insert(Node *np)

{ if( start= =NULL)

start=rear=np;

else

{

rear->next=np;

rear=np;}

}

void display( Node *np){ while(np!=NULL) {

cout<<np->info<<“->”;np=np->next;

} cout<<“!!!\n”;}

void traverse(Node *np){ while(np!= NULL) { cout<<np->info <<“->”; np=np->next; } cout<<“!!!\n”;}

StackStack refer to the lists stored and accessed in a special way Stack refer to the lists stored and accessed in a special way i.e. LIFO technique. In stack, insertion and deletions take i.e. LIFO technique. In stack, insertion and deletions take place only at one end called the top.place only at one end called the top.

Stack Operation

• Algorithm: Pushing in stack Array

• Top=-1

• Read Item

• If(top==N-1) Then

• { Print “Overflow: }

• Else

• {

• Top=top+1

• Stack[top]=item }

• END

Algorithm: Popping from Array Stack(Remove)

If top==-1

{

print “ Underflow”

Exit

}

Else

{

Print stack[top]

}

end

#include<iostream.h>#include<conio.h>#include<process.h>Int Push(int [ ] ,int &,int);Void display(int [ ],int);Const int size=50;Void main( ){ int Stack[size],Item,top=-1,res;Char ch=‘y’;Clrscr( );While(ch==‘y’||ch=‘Y’){ cout<<“\n enter ITEM for insertion:”;Cin>>Item;res=Push(Stack,top,Item)if(res= = -1){ cout<<“\n Overflow”;exit(1);}cout<<“\n the stack now is:;Display(Stack,top);

cout<<“ \n Want to insert more element?(y/n);cin>>ch;}}int Push(int Stack[ ],int & top,int ele){ if(top= =size-1) return -1; else top++;Stack(top]=ele;}Return 0;}void Display( int Stack[ ],int top){ cout<< Stack[top]<<“”<<endl;For(int i =top-1;i>=0;i- -)cout<< Stack[i]<<endl;}

• Conversion of infix to postfix expression:

• Evaluation order: • 1. Brackets or Parenthesis

• 2.Exponentiation(^)

• 3. Multiplication or Division

• 4. Addition or Substraction

• Example: Convert (A+B)*C/D into postfix

• Ans: Step 1: Determine the actual evaluation order by putting braces. ((A+B)*C)/D

• Step 2: Converting expression into innermost braces.

• ((AB+)*C)/D=(AB+C*)/D=AB+C*D/

Conversion from infix to prefix An expression where an operator precede the two

operands is a prefix expression

INFIX PREFIX

((A+(B-C)*D)^E+F) T1=B-C -BC

((A+T1*D)^E+F) T2=T1*D *T1D

((A+T2)^E+F) T3=A+T2 +AT2

(T3^E+F) T4=T3^E ^T3E

(T4+F) T5=T4+F +T4F

T5 +^+A*-BCDEF

Application of Stack :Polish Strings

PUSH A

PUSH B

ADD  

PUSH C

PUSH D

ADD  

MUL  

POP X

QUEUE: A queue is a special type of data structure where elements are inserted from one end is known as rear end and elements are deleted from the other end is known as front end.

The queue is also known as First in First out (FIFO).

Front Store the index of first element in the queue and rear stores the index of last element in the queue.

at beginning ,front=0 rear=-1

Insertion in the array queue:

Algorithm:1. If rear=NULL then

2. {

3. rear=front=0

4. Queue[0]=item

5. }

6. Else if rear=n-1 then

7. Print “queue is full, OVERFLOW”

8. Else {queue[rear+1]=item

9. Rear=rear+1

10.}

11.end

Deletion in a array queue:

Algorithm:1. If front=Null then

2. print “queue empty”

3. Else {

4. Item=queue[front]

5. If front=rear then

6. { front=rear=null

7. }

8. else front =rear+1

9. }

10.end

Linked Queues: Algorithm:1. //Allocate space for item to be inserted

2. NEWPTR=new Node

3. NEWPTRINFO=item;

4. NEWPTRLINK=NULL

5. if rear=NULL then

6. { front=NEWPTR

7. rear=NEWPTR

8. } else

9. { rear LINK=NEWPTR

10. rear= NEWPTR

11. }

12. END

DELETION in a Linked Queue

Algorithm:1. If front=null then

2. print “ Queue empty”

3. else {

4. item= front link

5. If front =rear then

6. { front=rear=null

7. }

8. Else

9. Front =front link

10. }

11.end

Circular Queue: A Circular Array allows the entire array to store the element s without shifting any data within the queue.

Doubled ended Queue: Deque are the refined queues in which elements can be added or removed at either end but not in the middle.