Stack &Queue on Self-Referencing Structuresrogers/pds_theory/lect28.pdf · Stack &Queue on...
Transcript of Stack &Queue on Self-Referencing Structuresrogers/pds_theory/lect28.pdf · Stack &Queue on...
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 1'
&
$
%
Stack & Queue on Self-Referencing Structures
Lect 28 Goutam Biswas
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
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3'
&
$
%
10
20
36
15
stack s
Dynamically CreatedData Area
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4'
&
$
%
Empty Stack
stack s
Empty Stack
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5'
&
$
%
Push
10
20
36
15
stack s25
Push 25
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6'
&
$
%
Pop
10
36
15
stack s
20
Pop
Lect 28 Goutam Biswas
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
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
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 9'
&
$
%
Note
We macro define INIT and ISEMPTY as thecode is very simple. We also could have doneinline function.
Lect 28 Goutam Biswas
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
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
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
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 top
printf(" ’E’ for exit :\n");
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14'
&
$
%
while((c = getchar()) != ’e’ && c != ’E’)
switch(c) {
case ’u’ :
case ’U’ :
scanf("%d",&x);
err = push(&s,x);
break;
case ’o’ :
case ’O’ :
err = pop(&s);
break;
case ’t’ :
case ’T’ :
err = top(s , &val) ;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15'
&
$
%
if(!err) printf("%d\n", val);
break;
case ’\n’ :
case ’\t’ :
case ’ ’ :
break;
default :
printf("Token Unknown\n");
}
return 0;
}
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16'
&
$
%
Representation of Queue
struct queue {
int data ;
struct queue *next ;
};
typedef struct {
struct queue *front, *rear ;
} queue ;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17'
&
$
%
Note
We use a dummy node. In an empty queue,both the front and the rear pointers points tothe dummy node.
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18'
&
$
%
front
rear
Empty Queue
queue s Dummy Node
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 19'
&
$
%
10
36
15
queue s
front
rear
Dummy Node
Dynamically Created Data Area
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 20'
&
$
%
queue s
front
rear
Dummy Node
36
15
10
30
Add 30
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 21'
&
$
%
queue s
front
rear 36
15
10
30
Dummy NodeDelete
New Dummy Node
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 22'
&
$
%
Interface File: queueSR.h
#ifndef _QUEUESR_H
#define _QUEUESR_H
#include <stdio.h>
#include <stdlib.h>
#define ERROR 1
#define OK 0
struct queue { // queueSR.h
int data ;
struct queue *next ;
};
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 23'
&
$
%
typedef struct queue node;
typedef struct {
struct queue *front, *rear ;
} queue ;
#define ISEMPTY(q) ((q).front == (q).rear)
void init(queue *) ;
int add(queue *, int) ;
int delete(queue *);
int front(queue, int *) ;
#endif
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 24'
&
$
%
Implementation File: queueSR.c
#include "queueSR.h"
void init(queue *qP) { // queueSR.c
node *temp ;
temp = (node *)malloc(sizeof(node)) ;
qP->front=qP->rear=temp ;
}
int add(queue *qP, int n) {
node *temp ;
temp=(node *)malloc(sizeof(node)) ;
if(temp == NULL) return ERROR ;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 25'
&
$
%
temp->data=n; qP->rear->next=temp ;
qP->rear = temp ;
return OK ;
}
int front(queue q, int *v) {
if(ISEMPTY(q)) return ERROR ;
*v= q.front->next->data ;
return OK ;
}
int delete(queue *qP) {
node *temp ;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 26'
&
$
%
if(ISEMPTY(*qP)) return ERROR ;
temp = qP->front ;
qP->front=qP->front->next ;
free(temp) ;
return OK ;
}
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 27'
&
$
%
User Program: testQueueSR.c
#include <stdio.h>
#include "queueSR.h"
int main() // testQueueSR.c
{
queue q ;
int x , err , val ;
char c;
init(&q);
printf(" ’A’ for add (A 15)\n") ;
printf(" ’D’ for delete\n ’F’ for front\n ’E’ for exit
while((c = getchar()) != ’e’ && c != ’E’)
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 28'
&
$
%
switch(c) {
case ’a’ :
case ’A’ :
scanf("%d",&x);
err = add(&q,x);
if(err) printf("The Queue is full\n") ;
break;
case ’d’ :
case ’D’ :
err = delete(&q);
if(err) printf("The Queue is empty\n") ;
break;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 29'
&
$
%
case ’f’ :
case ’F’ :
err = front(q , &val) ;
if(err) printf("The Queue is empty\n") ;
else printf("%d\n",val);
break;
case ’\n’ :
case ’\t’ :
case ’ ’ :
break;
default :
printf("Token Unknown\n");
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 30'
&
$
%
}
return 0;
}
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 31'
&
$
%
Union Type
So far all our data are of single type. But it ispossible to have data of any one of a fewdifferent types e.g. either of type int or of typedouble. The C language provides support tohave data of type union.
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 32'
&
$
%
Union Type: an Example
typedef union data {
int dataI;
double dataD;
} intDouble;
A variable of type union data or intDoublecan store either a value of type int or of typedouble. The allocated space is large enough toaccomodate the data of the largest size. Thefields are accessed like a structure.
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 33'
&
$
%
Note
It is necessary to attach a tag to remember thetype of the actual data present. We consider astack of type intDouble.
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 34'
&
$
%
Interface File: stackU.h
#ifndef _STACKU_H
#define _STACKU_H
#include <stdio.h>
#include <stdlib.h>
#define ERROR 1
#define OK 0
typedef struct { // stackU.h
union data {
int I;
double D;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 35'
&
$
%
} data ;
char type;
} intDouble;
struct stack {
intDouble data ;
struct stack *next ;
} ;
typedef struct stack node, *stack ;
#define INIT(s) ((s)=NULL)
#define ISEMPTY(s) ((s) == NULL)
int push(stack *, intDouble) ;
int pop(stack *) ;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 36'
&
$
%
int top(stack, intDouble *) ;
#endif
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 37'
&
$
%
Implementation File: stackU.c
#include "stackU.h"
int push(stack *s, intDouble n) { // stackU.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 ;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 38'
&
$
%
*s = temp ;
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
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 39'
&
$
%
}
int top(stack s, intDouble *val) {
if(ISEMPTY(s)) {
printf("The STACK is empty\n") ;
return ERROR ;
}
*val = s -> data;
return OK ;
}
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 40'
&
$
%
User Program: testStackU.c
#include <stdio.h>
#include "stackU.h"
int main() // testStackU.c
{
stack s ;
int err ;
intDouble d;
char c ;
INIT(s);
printf(" ’Ui’ for push int (Ui 15)\n ’Uf’ for push float
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 41'
&
$
%
printf(" ’O’ for pop\n ’T’ for top \n ’E’ for exit :\n");
while((c = getchar()) != ’e’ && c != ’E’)
switch(c) {
case ’u’ :
case ’U’ :
scanf("%c", &c);
if(c == ’i’ || c == ’I’) {
scanf("%d",&d.data.I);
d.type = ’i’;
}
else if(c == ’f’ || c == ’F’) {
scanf("%lf",&d.data.D);
d.type = ’f’;
}
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 42'
&
$
%
err = push(&s,d);
break;
case ’o’ :
case ’O’ :
err = pop(&s);
break;
case ’t’ :
case ’T’ :
err = top(s , &d) ;
if(!err) {
if(d.type == ’i’) printf("%d\n", d.data.I);
if(d.type == ’f’) printf("%f\n", d.data.D);
}
break;
Lect 28 Goutam Biswas
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 43'
&
$
%
case ’\n’ :
case ’\t’ :
case ’ ’ :
break;
default :
printf("Token Unknown\n");
}
return 0;
}
Lect 28 Goutam Biswas