Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.

Similar presentations


Presentation on theme: "Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue."— Presentation transcript:

1 Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue ADT and its applications

2 3.1 Abstract Data Type Modular program Advantages: (1) debugging (2)team work (3) easy to modify

3 3.1 Abstract Data Type Abstract data type a set of operations mathematical abstractions just as integer, boolean Basic idea write once, use many Any changes are transparent to the other modules

4 3.2 List ADT Review on linked list Common errors memory allocation segmentation violation check for NULL pointer free memory

5 3.2 List ADT Doubly linked list struct node {int Number; struct node *left; struct node *right; }

6 3.2.1 List ADT: Example1 Single-variable Polynomials typedef struct {int CoeffArray[MaxDegree+1]; int HighPower } *polynomial

7 3.2.1 List ADT: Example 1 Initialize a polynomial void ZeroPolynomial(Polynomial Poly) {int j; for (j = 0; j < MaxDegree; j++) poly->CoeffArray[j] = 0; Poly->HighPower = 0; }

8 3.2.1 List ADT: Example 1 Add two polynomials void AddPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolySum) {int j; ZeroPolynomial(PolySum); PolySum->HighPower = Max(Poly1-> HighPower, Poly2 -> HighPower); for (j=PolySum->HighPower; j>=0; j--) PolySum->CoeffArray[j] = Poly1-> CoeffArray [j] + Poly2-> CoeffArray[j]; }

9 3.2.1 List ADT: Example 1 Multiply two polynomials void MultPolynomial(const Polynomial Poly1, const Polynomial Poly2, Polynomial PolyProd) {int j,k; ZeroPolynomial(PolyProd); PolySum->HighPower = Poly1-> HighPower+ Poly2 -> HighPower; for (j=0; j=Poly1->HighPower; j++) for (k=0; k=Poly2->HighPower; k++) PolyProd->CoeffArray[j+k] += Poly1-> CoeffArray [j]* Poly2-> CoeffArray[k]; }

10 3.2.1 List ADT: Example 1 Multiply two polynomials: limitation Consider the following situation P 1 (X) = 10 X X P 2 (X) = 3X X X +5 Most of the time is spent multiplying zeros

11 3.2.1 List ADT: Example 1 Multiply two polynomials: better structure struct node { int Coefficient; int Exponent; struct node *Next; }

12 3.2.1 List ADT: Example 1 Linked list representation of the previous structure

13 3.2.2 List ADT: Example 2 Multilists A university with 40,000 students and 2,500 subjects needs to generate 2 reports 1. Lists of registration for each class 2. Classes that each student registered. Implementation: construct 2D array (40Kx2K5) = 100M entries if each student takes 3 subjects => only 120K entries (~0.1% of 100M) => waste of resources.

14 3.2.2 List ADT: Example 2 Multilists

15 3.3 Stack ADT Stack model

16 3.3 Stack ADT LIFO structure Access from the top Basic Operations –empty (s) –pop (s) –push (s, i) –top (s)

17 3.3.1Implementation of stack using Linked List struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; struct Node {ElementType Element; PtrToNode Next; }

18 3.3.1Implementation of stack using Linked List int IsEmpty(Stack S); Stack CreatStack(void); void DisposeStack(Stack S); void MakeEmpty(Stack S); void Push(ElementType X, Stack S); void Pop(Stack S); ElementType Top(Stack S);

19 3.3.1Implementation of stack using Linked List Test for an empty stack int Isempty(Stack S) { return (S Next == NULL) }

20 3.3.1Implementation of stack using Linked List Create an empty stack stack CreatStack(void) {Stack S; S=malloc(sizeof(struct Node)); if (S==NULL) FatalError(Out of space!!!); MakeEmpty(S) return(S); } void MakeEmpty(Stack S) { if (S==NULL) printf(Error message!); else while (!IsEmpty(S)) pop(S) }

21 3.3.1Implementation of stack using Linked List Push onto a stack void Push(ElementType X, Stack S) {PtrtoNode TmpCell; TmpCell = malloc(sizeof(struct Node)); if (TmpCell == NULL) fatalError(message); else {TmpCell Element = X; TmpCell Next = S Next; S Next = TmpCell; } }

22 3.3.1Implementation of stack using Linked List Return top element in a stack ElementType Top(Stack S) {if (!IsEmpty(S)) return (S Next Element); Error(Empty stack); return 0; }

23 3.3.1Implementation of stack using Linked List Pop from a stack void Pop(Stack S) {PtrToNode FirstCell; if (ISEmpty(S); Error (Empty stack); else {FirstCell = S Next; S Next =S Next Next; free(FirstCell);} }

24 3.3.2 Implementation of stack using Array Struct StackRecord { int Capacity; int TopOfStack; ElementType *Array; } #define STACKSIZE 100 Struct StackRecord { int top; ElementType Array[STACKSIZE]; }

25 3.3.2 Implementation of stack using Array Test for empty stack empty (stack *ps) { return (ps top == -1) }

26 3.3.2 Implementation using Array Pop top element from stack int pop (stack *ps) { int x if (empty (ps)) printf (%s\n, stack underflow!); else {x = ps array [ps top]; (ps top)--; return (x); } }

27 3.3.2 Implementation using Array Push an element onto the stack push (stack *ps, int x) { if (ps top == STACKSIZE – 1) printf (%s\n, stack overflow!); else { (ps top)++; ps items [ps top] = x; } }

28 3.3.3 Variable types of stack elements #define STACKSIZE 100 #define INTGR1 #define FLT2 #define STRING3 struct stackelement { int etype;/* element type */ union { int ival; float fval; char *pval; } element;/* end union */ }

29 3.3.3 Variable Types of Stack Elements To access the top element struct stack s; struct stackelement se; se = s.items [s.top]; switch (se.etype) { case INTGR: printf (%d\n, se.ival); break; case FLT: printf (%d\n, se.fval); break; case STRING: printf (%s\n, se.pval); }

30 3.3.4 Applications Balancing Symbols Make an empty stack. Read characters until end of file. If the character is an opening symbol, push it onto stack. If it is a closing symbol, then if the stack is empty, report an error. Otherwise, pop the stack. If the symbol popped is not the corresponding opening symbol, then report an error. At the end of file, if the stack is not empty, report an error.

31 3.3.4 Applications Postfix Expressions *1.06 = or postfix notation of (4.99* *1.06) is * * + Why do we need postfix notation?

32 3.3.4 Application: Postfix How does postfix work? e.g * *

33 3.3.4 Application : Postfix

34

35 3.3.4 Applications Converting infix expressions into postfix InfixA * B + C * D PostfixA B * C D * +

36 3.3.4 Application: infix postfix Stack -> Input Postfix StringA **A *BA B ++A B * +CA B * C + **A B * C + *DA B * C D pop stackA B * C D * +

37 3.3.4 Application: infix postfix opstk = the empty stack; while (not end of input) {symb = next input character; if (symb is an operand) add symb in the postfix string; else { while (!empty (opstk) && prcd (stack (opstk), symb)) { topsymb = pop (opstk); add topsymb to postfix string; } push (opstk, symb); }

38 3.3.4 Application: infix postfix /* output any remaining operators */ while (!empty (opstk)) { topsymb = pop (opstk); add topsymb to the postfix string; }

39 3.3.4 Application: infix postfix With parentheses Stack -> InputPostfix String( (AA ( ++A ( +BA B )A B + **A B + *CA B + C pop stackA B + C *

40 3.3.4 Application: infix postfix Please refer to infix2postfix.doc for implementation.

41 3.4 Queue ADT Ordered collections of data items –Delete item at front of the queue –Insert item at rear of the queue –A FIFO structure A B CD E front rear

42 3.4 Queue ADT Basic operations (1) insert (q, x) (2) remove (q, x) (3) empty (q)

43 3.4 Queue ADT Array implementation #define MAXQUEUE 100 struct queue { int items [MAXQUEUE]; int front, rear, length; } q;

44 3.4 Queue ADT void insert (q, x) { q.length++; q.rear++; q.items [q.rear] = x; }

45 3.4 Queue ADT void remove (q, x) { x = q.items [q.front]; q.front++; q.length--; }

46 3.4 Queue ADT Problem: May run out of rooms (1) Keep front always at 0 by shifting the contents up the queue, but the computer solution is inefficient (2) Use a circular queue (wrap around & use a length variable to keep track of the queue length)

47 3.4 Queue ADT insert (queue *pq, int x) {if ((pq length) == MAXQUEUE) /* check for overflow */ printf (Queue overflow); else { (pq length)++;/* make room for a new element */ if ((pq rear) == MAXQUEUE - 1) pq rear = 0; else (pq rear)++; pq items [pq rear] = x; /* check if queue is originally empty */ if ((pq length) == 1) pq front = pq rear; } }

48 3.4 Queue ADT void empty (queue *pq) { if ((pq length) == 0) return (TRUE); else return (FALSE); }

49 3.4 Queue ADT void remove (queue *pq) { if (empty (pq)) printf (Queue underflow); else if (pq front == MAXQUEUE - 1) { pq front = 0; return (pq items [MAXQUEUE - 1]); } else { (pq front)++; return (pq items [(pq front) - 1]); } }

50 3.4 Applications of Queues Print jobs Computer networks OS Real-life waiting lines


Download ppt "Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue."

Similar presentations


Ads by Google