lec28-29 - TAC252ComputerProgramming TodaysAgenda...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
13/8/03 Sundar B. BITS, Pilani. 1 TA C252 Computer Programming Today’s Agenda Access Restricted Lists -  Stacks, Queues and their applications.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ADT Stack – Type Definition /* file: stack.h */ #include “element.h” struct _stacknode; typedef struct _stacknode *StackNode; struct _stacknode {  Element e;  StackNode next;  }; typedef struct { StackNode top; unsigned int size; Boolean error; } Stack;
Background image of page 2
ADT Stack - Implementation    /* file: stackOps.c */ #include “stack.h” #include “element.h” #include “stdlib.h” Stack createStack()  {   Stack s;   s.top = NULL;   s.size = 0;   s.error = FALSE;   return s; } Boolean isEmpty(Stack s)  {       return (top==NULL) ? TRUE : FALSE; } Element top(Stack s)  {       return s.top->e; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ADT Stack - Implementation    /* file: stackOps.c */ Stack push(Element e, Stack s)  {   Stack s1;             return s1;     // Can we change s and return it? }   // Why not pass address of s instead of returning? s1.top = (StackNode)malloc(sizeof(struct __stacknode)); if (s1.top == NULL) { s1.error = TRUE;   // “out of memory” error             return s1; } s1.top->e = e ;  // copy element s1.top->next = s.top; s1.size = s.size+1;
Background image of page 4
ADT Stack – Implementation    /* file: stackOps.c */ Stack pop(Stack s)  {   Stack s1;   StackNode temp;             return s1; } s1.top =  s.top->next ;  // skip element s1.size = s.size-1; temp =  s.top ;  free(temp);  // if the original Stack can be destroyed
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
13/8/03 Sundar B. BITS, Pilani. 6 Use of ADT Stack - Example Problem: Consider a string of parentheses: ()()(((()))) Is this a valid string? Define a string of parentheses to be valid if: Each Open ‘(‘ has a matching Close ‘)’ and the Open  always appears before the Close Valid instances: ()()() ((()))(()) Invalid instances: (() )()(
Background image of page 6
Use of ADT Stack - Example Design an algorithm to verify whether a  string of parentheses is valid. Idea Push each Open onto a stack; Pop an Open  for each Close. The string is valid  If the stack is empty at the end  The string is invalid: If stack is non-empty at the end (i.e when the  string is empty)  OR  If stack is empty before the end (i.e when the  string is non-empty)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
13/8/03 Sundar B. BITS, Pilani. 8 Use of ADT Stack - Example The parentheses problem does have an  alternative solution. Observation:  In a valid string count of Opens equals the  count of Closes.  But )(  is an invalid string with equal count of  Opens and Closes. In a valid string the count of Opens of any 
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

lec28-29 - TAC252ComputerProgramming TodaysAgenda...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online