You are on page 1of 55

# Data Structures

Stack
Queue

## Introduction to Data Structures

CS 11 - Introduction to Computer Programming I

## Jan Michael C. Yap

janmichaelyap@gmail.com
Department of Computer Science
University of the Philippines Diliman

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack
Queue

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

Data Structures
Stack
Queue

## A data type is a kind of data that variables may hold in a

programming language plus the operations automatically
provided
E.g. character, integer, and floating point

Data Structures
Stack
Queue

## A data type is a kind of data that variables may hold in a

programming language plus the operations automatically
provided
E.g. character, integer, and floating point
An abstract data type (ADT) is a set of data elements and
the set of operations defined on the data elements

Data Structures
Stack
Queue

## A data type is a kind of data that variables may hold in a

programming language plus the operations automatically
provided
E.g. character, integer, and floating point
An abstract data type (ADT) is a set of data elements and
the set of operations defined on the data elements
Stack
Queue

Data Structures
Stack
Queue

## A data type is a kind of data that variables may hold in a

programming language plus the operations automatically
provided
E.g. character, integer, and floating point
An abstract data type (ADT) is a set of data elements and
the set of operations defined on the data elements
Stack
Queue
A data structure is an implementation or realization of an
ADT in terms of language data types or other data structures
such that operations on the data structure are expressible in
terms of directly executable procedures

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## E.P. Quiwa, ”Data Structures”, Alexan Publishing

JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

## E.P. Quiwa, ”Data Structures”, Alexan Publishing

JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Dynamic memory allocation: sizeof, malloc, free

sizeof operator: gets the required memory size (in bytes) to
represent data
sizeof(<data type>)
sizeof <expression>

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Dynamic memory allocation: sizeof, malloc, free

sizeof operator: gets the required memory size (in bytes) to
represent data
sizeof(<data type>)
sizeof <expression>
malloc function: allocate memory with the specified amount
(in bytes) ;
(<data type> *) malloc(<size in bytes>)

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Dynamic memory allocation: sizeof, malloc, free

sizeof operator: gets the required memory size (in bytes) to
represent data
sizeof(<data type>)
sizeof <expression>
malloc function: allocate memory with the specified amount
(in bytes) ;
(<data type> *) malloc(<size in bytes>)
The (<data type> *) part is called explicit type casting.
More particularly, we are type casting the allocated memory
into a pointer with type <data type>

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Dynamic memory allocation: sizeof, malloc, free

sizeof operator: gets the required memory size (in bytes) to
represent data
sizeof(<data type>)
sizeof <expression>
malloc function: allocate memory with the specified amount
(in bytes) ;
(<data type> *) malloc(<size in bytes>)
The (<data type> *) part is called explicit type casting.
More particularly, we are type casting the allocated memory
into a pointer with type <data type>
free function: deallocates memory given to a particular
(pointer) variable
free(<pointer variable>)
JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* The linked list we are implementing is a singly-linked list, to be exact. */

typedef struct Node {
int data;
} Node;

}

}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Inserting into a linked list

/* Assume that we can only insert at the "right" end of the list. */
void insert_into_list(Linked_List *list, int x) {
Node *node = (Node *) malloc (sizeof(Node));
if (node == NULL) {
printf("Sorry, the computer can no longer allocate memory.\n");
} else {
node->data = x;
if (is_list_empty(list)) {
} else {
}
}
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Print contents of a linked list

if (is_list_empty(list)) {
printf("List is empty.\n");;
} else {
printf("The contents of the list are:\n ");
while (runner != NULL) {
printf("%d ", runner->data);
}
printf("\n");
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* IMPORTANT! Since we allocated memory, we must also deallocate memory. */

if (is_list_empty(list)) {
printf("List is empty.\n");;
} else {
free(runner);
}
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Sample main function

main() {
initialize_list(&list);
insert_into_list(&list, 1);
insert_into_list(&list, 3);
insert_into_list(&list, 5);
insert_into_list(&list, 7);
print_list(&list);
destroy_list(&list);
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Stack

## E.P. Quiwa, ”Data Structures”, Alexan Publishing

JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

int data;
} Stack_Node;

Stack_Node *top;
} Stack;

## void initialize_stack(Stack *stack) {

stack->top = NULL;
}

## int is_stack_empty(Stack *stack) {

return (stack->top == NULL);
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* Insertion into a stack is termed as push. */

void push(Stack *stack, int x) {
Stack_Node *node = (Stack_Node *) malloc (sizeof(Stack_Node));
if (node == NULL) {
printf("Sorry, the computer can no longer allocate memory.\n");
} else {
node->data = x;
if (is_stack_empty(stack)) {
stack->top = node;
} else {
stack->top = node;
}
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* Deletion from a stack is termed as pop. */

void pop(Stack *stack) {
Stack_Node *node = stack->top;
int x;
if (is_stack_empty(stack)) {
printf("Stack is empty.\n");
} else {
x = node->data;
free(node);
printf("%d was deleted from the stack.\n", x);
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* We print the contents of the stack from topmost to bottommost element. */

void print_stack(Stack *stack) {
Stack_Node *runner = stack->top;
if (is_stack_empty(stack)) {
printf("Stack is empty.\n");
} else {
printf("The contents of the stack, from topmost to bottommost, are:\n ");
while (runner != NULL) {
printf("%d ", runner->data);
}
printf("\n");
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Sample main function

main() {
Stack stack;
initialize_stack(&stack);
push(&stack, -3);
push(&stack, -5);
push(&stack, 14);
push(&stack, 2);
print_stack(&stack);
pop(&stack);
print_stack(&stack);
pop(&stack);
print_stack(&stack);
pop(&stack);
print_stack(&stack);
pop(&stack);
print_stack(&stack);
pop(&stack);
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

Queue

## E.P. Quiwa, ”Data Structures”, Alexan Publishing

JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

Queue

the rear.

## E.P. Quiwa, ”Data Structures”, Alexan Publishing

JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

Queue

## The part of the queue where insertion is allowed is termed as

the rear.
The part of the queue where deletion is allowed is termed as
the front.

## E.P. Quiwa, ”Data Structures”, Alexan Publishing

JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack C implementation
Queue

Topics

1 Data Structures

Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

int data;
} Queue_Node;

## typedef struct Queue {

Queue_Node *front;
Queue_Node *rear;
} Queue;

## void initialize_queue(Queue *queue) {

queue->front = NULL;
queue->rear = NULL;
}

return (???);
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* Insertion into a queue is termed as enqueue. */

void enqueue(Queue *queue, int x) {
Queue_Node *node = (Queue_Node *) malloc (sizeof(Queue_Node));
if (node == NULL) {
printf("Sorry, the computer can no longer allocate memory.\n");
} else {
node->data = x;
if (is_queue_empty(queue)) {
queue->front = node;
queue->rear = node;
} else {
queue->rear = node;
}
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* Deletion from a queue is termed as dequeue. */

void dequeue(Queue *queue) {
Queue_Node *node = queue->front;
int x;
if (is_queue_empty(queue)) {
printf("Queue is empty.\n");
} else {
x = node->data;
free(node);
printf("%d was deleted from the queue.\n", x);
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## /* We print the contents of the queue from front to rear element. */

void print_queue(Queue *queue) {
Queue_Node *runner = queue->front;
if (is_queue_empty(queue)) {
printf("Queue is empty.\n");
} else {
printf("The contents of the queue, from front to rear, are:\n ");
while (runner != NULL) {
printf("%d ", runner->data);
}
printf("\n");
}
}

## JMC Yap CS 11 - Intro to Data Structures

Data Structures
Stack C implementation
Queue

## Sample main function

main() {
Queue queue;
initialize_queue(&queue);
enqueue(&queue, 22);
enqueue(&queue, 25);
enqueue(&queue, 33);
enqueue(&queue, -72);
enqueue(&queue, 5);
print_queue(&queue);
dequeue(&queue);
print_queue(&queue);
dequeue(&queue);
print_queue(&queue);
dequeue(&queue);
print_queue(&queue);
dequeue(&queue);
print_queue(&queue);
dequeue(&queue);
print_queue(&queue);
dequeue(&queue);
}
JMC Yap CS 11 - Intro to Data Structures
Data Structures
Stack
Queue

END OF LESSON 9