{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect28 - DS CS 11002 Computer Sc Engg IIT Kharagpur $ 1...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1 & $ % Stack & Queue on Self-Referencing Structures Lect 28 Goutam Biswas
Image of page 1

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2 & $ % Representation of Stack struct stack { int data ; struct stack *next ; }; typedef struct stackNode node, *stack ; Lect 28 Goutam Biswas
Image of page 2
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3 & $ % 10 20 36 15 stack s Dynamically Created Data Area Lect 28 Goutam Biswas
Image of page 3

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4 & $ % Empty Stack stack s Empty Stack Lect 28 Goutam Biswas
Image of page 4
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5 & $ % Push 10 20 36 15 stack s 25 Push 25 Lect 28 Goutam Biswas
Image of page 5

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6 & $ % Pop 10 36 15 stack s 20 Pop Lect 28 Goutam Biswas
Image of page 6
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7 & $ % Interface File: stackSR.h #ifndef _STACKSR_H #define _STACKSR_H #include <stdio.h> #include <stdlib.h> #define ERROR 1 #define OK 0 struct stack { // stackSR.h int data ; struct stack *next ; } ; Lect 28 Goutam Biswas
Image of page 7

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8 & $ % typedef struct stack node, *stack ; #define INIT(s) ((s)=NULL) #define ISEMPTY(s) ((s) == NULL) int push(stack * , int) ; int pop(stack *) ; int top(stack, int *) ; #endif Lect 28 Goutam Biswas
Image of page 8
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9 & $ % Note We macro define INIT and ISEMPTY as the code is very simple. We also could have done inline function . Lect 28 Goutam Biswas
Image of page 9

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10 & $ % Implementation File: stackSR.c #include "stackSR.h" int push(stack *s, int n) { // stackSR.c stack temp ; temp=(stack)malloc(sizeof(node)) ; if(temp == NULL) { printf("The STACK is full\n"); return ERROR ; } temp->data=n; temp->next=*s ; *s = temp ; Lect 28 Goutam Biswas
Image of page 10
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11 & $ % return OK ; } int pop(stack *s) { stack temp ; if(ISEMPTY(*s)) { printf("The STACK is empty\n"); return ERROR ; } temp=*s; *s=(*s)->next ; free(temp) ; return OK ; } Lect 28 Goutam Biswas
Image of page 11

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

View Full Document Right Arrow Icon
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12 & $ % int top(stack s , int *val) { if(ISEMPTY(s)) { printf("The STACK is empty\n") ; return ERROR ; } *val = s -> data ; return OK ; } Lect 28 Goutam Biswas
Image of page 12
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 13 & $ % User Program: testStackSR.c #include <stdio.h> #include "stackSR.h" int main() // testStackSR.c { stack s ; int x , err , val ; char c ; INIT(s); printf(" ’U’ for push (U 15)\n ’O’ for pop\n ’T’ for to printf(" ’E’ for exit :\n"); Lect 28 Goutam Biswas
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern