+ All Categories
Home > Documents > CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides ›...

CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides ›...

Date post: 03-Jul-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
51
CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0) Teacher: Sourangshu Bha@acharya [email protected] h@p://cse.iitkgp.ac.in/~sourangshu/ Department of Computer Science and Engineering Indian InsJtute of Technology Kharagpur
Transcript
Page 1: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

CS11001/CS11002ProgrammingandDataStructures

(PDS)(Theory:3-0-0)

Teacher:SourangshuBha@[email protected]

h@p://cse.iitkgp.ac.in/~sourangshu/

DepartmentofComputerScienceandEngineeringIndianInsJtuteofTechnologyKharagpur

Page 2: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

ConceptualIdea

List implementation

and the related functions

Insert

Delete

Traverse

Page 3: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

AbstractDataTypes(ADT)

Page 4: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

ListisanAbstractDataType

•  Aclassofobjectswhoselogicalbehaviorisdefinedbyasetofvaluesandasetofopera=ons.

•  Whatisanabstractdatatype(ADT)?–  Itisadatatypedefinedbytheuser.–  Itisdefinedbyit’sbehavior(seman=cs)–  Typicallymorecomplexthansimpledatatypeslikeint,float,etc.

•  Whyabstract?–  Becausedetailsoftheimplementa=onarehidden.– Whenyoudosomeopera=ononthelist,sayinsertanelement,youjustcallafunc=on.

–  Detailsofhowthelistisimplementedorhowtheinsertfunc=oniswriHenisnolongerrequired.

Page 5: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Example1::Complexnumbers struct cplx { float re; float im; } typedef struct cplx complex; complex *add (complex a, complex b); complex *sub (complex a, complex b); complex *mul (complex a, complex b); complex *div (complex a, complex b); complex *read(); void print (complex a);

Structure definition

Function prototypes

Page 6: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Complex Number

add

print

mul

sub

read

div

ADT

Page 7: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Example2::SetmanipulaJon struct node { int element; struct node *next; } typedef struct node set; set *union (set a, set b); set *intersect (set a, set b); set *minus (set a, set b); void insert (set a, int x); void delete (set a, int x); int size (set a);

Structure definition

Function prototypes

Page 8: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Set

union

size

minus

intersect

delete

insert

ADT

Page 9: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:Last-in-first-out(LIFO)

Page 10: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACKUSINGARRAY

PUSH

40

30

90

top

top

top

14top23top80top

Whatdoweneed?1.Anarraytostoretheelements(ofmaximumsize).2.Anintegervariable(actasarrayindex)toindicatethestacktop.

#define MAXSIZE 100 struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK;

Increment top (array index)

Page 11: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACKUSINGARRAY

POP

40

30

90top

14top23top80top

#define MAXSIZE 100 struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK;

Decrement top (array index)

Page 12: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACKusingarray

#include <stdio.h> #define MAXSIZE 100

struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK;

int main() { STACK A, B; create(&A); create(&B); push(&A,10); push(&A,20);

push(&A,30); push(&B,100); push(&B,5);

printf(“%d %d”, pop(&A),pop(&B));

push (&A, pop(&B));

if (isempty(&B)) printf (“\n B is empty”); return 0;

}

Page 13: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:OverflowandUnderflow

40

30

90

14

23

80

top

#define MAXSIZE 100 struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK;

Push(incrementtop)whenstacktopisatMAXSIZE

Overflow

Pop(decrementtop)whenstacktopisatzeroindex.

Underflow

Overflow

Underflow

top

Page 14: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:isEmpty()andisFull()#define MAXSIZE 100 struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK;

int isEmpty (stack *s) { if(s->top == -1) return 1; else return 0; }

int isFull (stack *s) { if(s->top==(MAXSIZE–1)) return 1; else return 0; }

Page 15: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:push()andpop()#define MAXSIZE 100 struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK;

int push (stack *s, int x) { if(isFull(s)) return 1;

else { s->top++; s->st[s->top]=x;

return 0; }

}

int pop (stack *s) { if(isEmpty(s)) return -99999; else { x=s->top; s->top--;

return x; }

}

Page 16: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

StackCreaJon

void create (stack *s) { s->top = -1; /* s->top points to last element pushed in; initially -1 */ }

Page 17: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Stack:LinkedListStructure

top

PUSH

NULL

struct stack { int value; struct stack *next; }; typedef struct stack STACK;

STACK *top;

Page 18: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Stack:LinkedListStructure

top

POP

NULL

struct stack { int value; struct stack *next; }; typedef struct stack STACK;

STACK *top;

Page 19: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

DeclaraJon

#define MAXSIZE 100 struct stack { int st[MAXSIZE]; int top; }; typedef struct stack STACK; STACK s;

struct stack { int value; struct stack *next; }; typedef struct stack STACK; STACK *top;

ARRAY LINKED LIST

Page 20: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:push()

void push (STACK **top, int element) { STACK *new;

new = (stack *) malloc(sizeof(stack)); if (new == NULL) { printf (“\n Memory allocation problem.”); exit(-1); }

new->value = element; new->next = *top; *top = new; }

LINKED LIST

Page 21: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

int pop (STACK **top) { int t; STACK *p;

if (*top == NULL) { printf (“\n Stack is empty”); exit(-1); } else { t = (*top)->value; p = *top; *top = (*top)->next; free (p); return t; } }

LINKED LIST

STACK:pop()

Page 22: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:imEmpty()

int isempty (stack *top) { if (top == NULL) return (1); else return (0); }

LINKED LIST

isFull()…?

Thereisunderflow.But,thereisnooverflow(assumingmemoryisavailablefordynamicalloca=on).

Page 23: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK:Last-In-First-Out(LIFO)

Assume::stackcontainsintegerelementsvoid push (STACK *s, int element); /*Insertanelementinthestack*/int pop (STACK *s); /*Removeandreturnthetopelement*/void create (STACK *s); /*Createanewstack*/int isempty (STACK *s); /*Checkifstackisempty*/int isfull (STACK *s); /*Checkifstackisfull*/

Page 24: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

STACK

push

create

pop

isfull

isempty

ADT

Page 25: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

ApplicaJonsofStacks

•  Directapplica=ons–  Page-visitedhistoryinaWebbrowser–  Undosequenceinatexteditor–  ChainofmethodcallsintheJavaVirtualMachine–  ValidateXML

•  Indirectapplica=ons–  Auxiliarydatastructureforalgorithms–  Componentofotherdatastructures

Page 26: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

InfixtoPos]ixInfix Pos]ix

A+B AB+A+B*C ABC*+(A+B)*C AB+C*A+B*C+D ABC*+D+(A+B)*(C+D) AB+CD+*A*B+C*D AB*CD*+

A+B*CàA+(B*C)àA(B*C)+àABC*+A+B*C+DàA+(B*C)+DàA(B*C)+D+àABC*+D+

Page 27: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

InfixtoPos]ixConversionRequiresoperatorprecedenceinforma=onOperands:Addtoposeixexpression.Closeparenthesis:popstacksymbolsun=lanopenparenthesisappears.Operators:Popallstacksymbolsun=lasymboloflowerprecedenceappears.Thenpushtheoperator.

Endofinput:Popallremainingstacksymbolsandaddtotheexpression.

Page 28: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

InfixtoPos]ixRules

Currentsymbol

OperatorStack

Pos]ixstring

1 A A

2 * * A

3 ( *( A

4 B *( AB

5 + *(+ AB

6 C *(+ ABC

7 * *(+* ABC

8 D *(+* ABCD

9 ) * ABCD*+

10 + + ABCD*+*

11 E + ABCD*+*E

12 ABCD*+*E+

Expression:A*(B+C*D)+EbecomesABCD*+*E+

Pos]ixnotaJonisalsocalledasReversePolishNotaJon(RPN)

Page 29: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

InfixtoPos]ixRulesstackscharch,elementwhile(tokensareavailable){ch=read(token);if(chisoperand){ printch;}else{ while(priority(ch)<=priority(topmoststack)){ element=pop(s); print(element); }push(s,ch);}}while(!empty(s)){

element=pop(s);print(element);

}

Page 30: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Homework

ImplementInfixtoPoseixconversionprograminCusingstack.Youmayuse

arrayorlinkedlistforyourstack.

Page 31: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

EvaluaJngPos]ixExpression

1)  Createastacktostoreoperands(orvalues).

2)  Scanthegivenexpressionanddofollowingforeveryscannedelement.

a)Iftheelementisanumber,pushitintothestackb)Iftheelementisaoperator,popoperandsfortheoperatorfromstack.Evaluatetheoperatorand push

theresultbacktothestack

3)  Whentheexpressionisended,thenumberinthestackisthefinalanswer

Page 32: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

EvaluaJngPos]ixExpression

InfixExpression:2*3–4/5PoseixExpression:23*45/-

23*45/-

3

2

EvaluateExpression

Page 33: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

EvaluaJngPos]ixExpression

InfixExpression:2*3–4/5PoseixExpression:23*45/-

23*45/-

6

4

5

6

0.8

5.2

EvaluateExpression

Page 34: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

EvaluaJngPos]ixExpression

InfixExpression:2*3–4/5PoseixExpression:23*45/-

23*45/-

6

0.8

5.2

EvaluateExpression

EvaluatedExpression(Stacktopelement)=5.2

Page 35: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

Homework

WriteaCprogramtoevaluateposeixexpressionusingstack.Youmayusearray

orlinkedlistforyourstack.

Page 36: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE:First-in-first-out(FIFO)

Page 37: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUEUSINGARRAYWhatdoweneed?

1.Anarraytostoretheelements(ofmaximumsize).2.Twointegervariables(actasarrayindex)toindicatefrontandrear.

#define MAXSIZE 100 struct queue { int que[MAXSIZE]; int front,rear; }; typedef struct queue QUEUE;

FrontRear

76543210

Page 38: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

ENQUEUE

#define MAXSIZE 100 struct queue { int que[MAXSIZE]; int front,rear; }; typedef struct queue QUEUE;

Increment front (array index)

FrontRear Front

76543210

Page 39: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

DEQUEUE

#define MAXSIZE 100 struct queue { int que[MAXSIZE]; int front,rear; }; typedef struct queue QUEUE;

Increment rear (array index)

FrontRear Rear

76543210

Page 40: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

ProblemWithArrayImplementaJon

front rear rear

ENQUEUE

front

DEQUEUE

Effective queuing storage area of array gets reduced.

Use of circular array indexing

0 N

•  Thesizeofthequeuedependsonthenumberandorderofenqueueanddequeue.

•  Itmaybesitua=onwherememoryisavailablebutenqueueisnotpossible.

Page 41: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUEUSINGLINKEDLIST•  Createalinkedlisttowhichitemswouldbeaddedtooneendanddeletedfromtheotherend.

•  Twopointerswillbemaintained:–  Onepoin=ngtothebeginningofthelist(pointfromwhereelementswillbedeleted).

–  Anotherpoin=ngtotheendofthelist(pointwherenewelementswillbeinserted).

Front

Rear

DELETION INSERTION

Page 42: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE:InserJonintoaLinkedList

front rear

ENQUEUE

Page 43: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE:DeleJonfromaLinkedList

front rear

DEQUEUE

Page 44: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE::First-In-First-Out(FIFO)

Assume::queuecontainsintegerelementsvoid enqueue (QUEUE *q, int element); /*Insertanelementinthequeue*/int dequeue (QUEUE *q); /*Removeanelementfromthequeue*/queue *create(); /*Createanewqueue*/int isempty (QUEUE *q); /*Checkifqueueisempty*/int size (QUEUE *q); /*Returntheno.ofelementsinqueue*/int peek (QUEUE *q); /*dequeuewithoutremovingelement*/

Page 45: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE

enqueue

create

dequeue

size

isempty

ADT

Page 46: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUEusingLinkedList

structqnode{intval;structqnode*next;};structqueue{structqnode*qfront,*qrear;};typedefstructqueueQUEUE;

Page 47: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE::First-In-First-Out(FIFO)

Assume::queuecontainsintegerelementsvoid enqueue (QUEUE *q,int element) { struct qnode *q1; q1=(struct qnode *)malloc(sizeof(struct qnode));

q1->val= element; q1->next=q->qfront; q->qfront=q1; }

Page 48: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE::First-In-First-Out(FIFO)

Assume::queuecontainsintegerelementsint size (queue *q) { queue *q1; int count=0; q1=q; while(q1!=NULL) { q1=q1->next; count++; } return count; }

Page 49: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE::First-In-First-Out(FIFO)

Assume::queuecontainsintegerelementsint peek (queue *q) { queue *q1; q1=q; while(q1->next!=NULL) q1=q1->next; return (q1->val); }

ImplementthisusingQUEUEdatastructure.

Page 50: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

QUEUE::First-In-First-Out(FIFO)

Assume::queuecontainsintegerelementsint dequeue (queue *q) { int val; queue *q1,*prev; q1=q; while(q1->next!=NULL) { prev=q1; q1=q1->next; } val=q1->val; prev->next=NULL; free(q1); return (val);

} ImplementthisusingQUEUEdatastructure.

Page 51: CS11001/CS11002 Programming and Data Structures (PDS ...cse.iitkgp.ac.in › ... › slides › Presentation11-upload.pdf · 2. Two integer variables (act as array index) to indicate

ApplicaJonsofQueues

•  Directapplica=ons– Wai=nglists.–  Accesstosharedresources(e.g.,printer).– Mul=programming.

•  Indirectapplica=ons–  Auxiliarydatastructureforalgorithms–  Componentofotherdatastructures


Recommended