Linked Stacks and Linked Queues Overview Linked Stack

  • Slides: 25
Download presentation
Linked Stacks and Linked Queues

Linked Stacks and Linked Queues

Overview • Linked Stack. – Push – Pop • Linked Queue. – Append –

Overview • Linked Stack. – Push – Pop • Linked Queue. – Append – Serve 2

Linked Stack Top of the Stack NULL pointer 3

Linked Stack Top of the Stack NULL pointer 3

#ifndef LINKEDSTACKH #define LINKEDSTACKH #include <stdbool. h> #include "node. h" struct Linked. Stack. Rec

#ifndef LINKEDSTACKH #define LINKEDSTACKH #include <stdbool. h> #include "node. h" struct Linked. Stack. Rec { Node* top. Ptr; }; typedef struct Linked. Stack. Rec Stack; void intialize. Stack(Stack* stack. Ptr); bool stack. Empty(const Stack* stack. Ptr); bool stack. Full(const Stack* stack. Ptr); void push(Stack* stack. Ptr, float item); float pop(Stack* stack. Ptr); #endif 4

Initialize Stack void initialize. Stack(Stack* stack. Ptr) { stack. Ptr->top. Ptr = NULL; }

Initialize Stack void initialize. Stack(Stack* stack. Ptr) { stack. Ptr->top. Ptr = NULL; } 55

Push Top 6

Push Top 6

Push • create a new node for the item • link the new node

Push • create a new node for the item • link the new node to the current top node • make the new node the new top 7

Push void push(Stack* stack. Ptr, float item) { Node* new. Node. Ptr = make.

Push void push(Stack* stack. Ptr, float item) { Node* new. Node. Ptr = make. Node(item); new. Node. Ptr->next. Ptr = stack. Ptr->top. Ptr; stack. Ptr->top. Ptr = new. Node. Ptr; } 88

Pop Top 9

Pop Top 9

Pop • • • check if the stack is empty remember the item in

Pop • • • check if the stack is empty remember the item in the top node remember address of current top node make the next node the new top free the old top node return the item 10

Pop float pop(Stack* stack. Ptr) { float item; Node* old. Node. Ptr = stack.

Pop float pop(Stack* stack. Ptr) { float item; Node* old. Node. Ptr = stack. Ptr->top. Ptr; if (stack. Empty(stack. Ptr)) { fprintf(stderr, “Stack is emptyn”); exit(1); } else { item = old. Node. Ptr->value; stack. Ptr->top. Ptr = old. Node. Ptr->next. Ptr; free(old. Node. Ptr); } return item; } 11 11

Linked Queue Front Rear 12

Linked Queue Front Rear 12

#ifndef LINKEDQUEUEH #define LINKEDQUEUEH #include <stdbool. h> #include "node. h" struct Linked. Queue. Rec

#ifndef LINKEDQUEUEH #define LINKEDQUEUEH #include <stdbool. h> #include "node. h" struct Linked. Queue. Rec { int count; Node* front. Ptr; Node* rear. Ptr; }; typedef struct Linked. Queue. Rec Queue; void intialize. Queue(Queue* queue. Ptr); bool queue. Empty(const Queue* queue. Ptr); bool queue. Full(const Queue* queue. Ptr); void append(Queue* queue. Ptr, float item); float serve(Queue* queue. Ptr); #endif 13

Initialize Queue void initialize. Queue(Queue* queue. Ptr) { queue. Ptr->front. Ptr = NULL; queue.

Initialize Queue void initialize. Queue(Queue* queue. Ptr) { queue. Ptr->front. Ptr = NULL; queue. Ptr->rear. Ptr = NULL; queue. Ptr->count = 0; } 14 14

Append Front Rear 15

Append Front Rear 15

Append Front Rear 16

Append Front Rear 16

Append • create a new node for the item • if the queue is

Append • create a new node for the item • if the queue is empty: – the new node becomes both front and rear of the queue • otherwise: – make a link from the current rear to the new node – the new node becomes the new rear • increment the count 17

void append(Queue* queue. Ptr, float item) { Node* new. Node. Ptr = make. Node(item);

void append(Queue* queue. Ptr, float item) { Node* new. Node. Ptr = make. Node(item); if (queue. Empty(queue. Ptr)) { queue. Ptr->front. Ptr = new. Node. Ptr; queue. Ptr->rear. Ptr = new. Node. Ptr; queue. Ptr->count = 1; } else { queue. Ptr->rear. Ptr->next. Ptr = new. Node. Ptr; queue. Ptr->rear. Ptr = new. Node. Ptr; queue. Ptr->count++; } } 18 18

Serve Front Rear 19

Serve Front Rear 19

Serve Front Rear 20

Serve Front Rear 20

Serve • • check that the queue is not empty remember the item in

Serve • • check that the queue is not empty remember the item in the front node remember the address of the front node make the next node the new front free the old front node decrement count if queue is now empty, set rear to NULL return the item 21

float serve(Queue* queue. Ptr) { float item; Node* old. Node. Ptr = queue. Ptr->front.

float serve(Queue* queue. Ptr) { float item; Node* old. Node. Ptr = queue. Ptr->front. Ptr; if (queue. Empty(queue. Ptr)) { fprintf(stderr, “Queue is emptyn”); exit(1); } else { item = old. Node. Ptr->value; queue. Ptr->front. Ptr = old. Node. Ptr->next. Ptr; queue. Ptr->count--; free(old. Node. Ptr); if (queue. Ptr->count == 0) { queue. Ptr->rear. Ptr = NULL; } } return item; } 22 22

#include <stdio. h> #include “linkedqueue. h” main() { Queue float the. Queue; item; initialize.

#include <stdio. h> #include “linkedqueue. h” main() { Queue float the. Queue; item; initialize. Queue(&the. Queue); while (scanf(“%f”, &item) != EOF) { append(&the. Queue, item); } while (!queue. Empty(&the. Queue)) { item = serve(&the. Queue); printf(“%fn”, item); } } 23 23

Revision • Linked Stack. – Initialize, Pop, Push. • Linked Queue. – Initialize, Append,

Revision • Linked Stack. – Initialize, Pop, Push. • Linked Queue. – Initialize, Append, Serve. 24

Next Linked Lists 25

Next Linked Lists 25