Sunteți pe pagina 1din 55

Data Structures

Linked Lists
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
Linked Lists
Stack
Queue

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists
Stack
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists
Stack
Queue

Data Types and Data Structures

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

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists
Stack
Queue

Data Types and Data Structures

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

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists
Stack
Queue

Data Types and Data Structures

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
Linked lists
Stack
Queue

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists
Stack
Queue

Data Types and Data Structures

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
Linked lists
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
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Linked lists

Sequential or contiguous design

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


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

Linked lists

Sequential or contiguous design

Linked design

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


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

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Dynamic memory allocation: sizeof, malloc, free

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
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
Linked Lists Background
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
Linked Lists Background
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
Linked Lists Background
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
Linked Lists Background
Stack C implementation
Queue

Declaring and initializing a linked list

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Declaring and initializing a linked list

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


typedef struct Node {
int data;
struct Node *link;
} Node;

typedef struct Linked_List {


Node *head;
} Linked_List;

void initialize_list(Linked_List *list) {


list->head = NULL;
}

int is_list_empty(Linked_List *list) {


return (list->head == NULL);
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Inserting into a linked list

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
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 *runner = list->head;
Node *node = (Node *) malloc (sizeof(Node));
if (node == NULL) {
printf("Sorry, the computer can no longer allocate memory.\n");
} else {
node->data = x;
node->link=NULL;
if (is_list_empty(list)) {
list->head = node;
} else {
while (runner->link != NULL) {
runner = runner->link;
}
runner->link = node;
}
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Print contents of a linked list

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Print contents of a linked list

void print_list(Linked_List *list) {


Node *runner = list->head;
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);
runner = runner->link;
}
printf("\n");
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Destroy a linked list

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Destroy a linked list

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


void destroy_list(Linked_List *list) {
Node *runner = list->head;
if (is_list_empty(list)) {
printf("List is empty.\n");;
} else {
while (list->head != NULL) {
list->head = list->head->link;
free(runner);
runner = list->head;
}
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Sample main function

main() {
Linked_List list;
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
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Stack

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


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

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Declaring and initializing a stack

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Declaring and initializing a stack

typedef struct Stack_Node {


int data;
struct Stack_Node *link;
} Stack_Node;

typedef struct Stack {


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
Linked Lists Background
Stack C implementation
Queue

Inserting into a stack

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Inserting into a stack

/* 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;
node->link=NULL;
if (is_stack_empty(stack)) {
stack->top = node;
} else {
node->link = stack->top;
stack->top = node;
}
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Deleting from a stack

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Deleting from a stack

/* 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 {
stack->top = stack->top->link;
x = node->data;
free(node);
printf("%d was deleted from the stack.\n", x);
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Print contents of a stack

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Print contents of a stack

/* 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);
runner = runner->link;
}
printf("\n");
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
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
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Queue

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


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

Queue

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


the rear.

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


JMC Yap CS 11 - Intro to Data Structures
Data Structures
Linked Lists Background
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
Linked Lists Background
Stack C implementation
Queue

Topics

1 Data Structures

2 Linked Lists
Background
C implementation

3 Stack
Background
C implementation

4 Queue
Background
C implementation

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Declaring and initializing a queue

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Declaring and initializing a queue

typedef struct Queue_Node {


int data;
struct Queue_Node *link;
} Queue_Node;

typedef struct Queue {


Queue_Node *front;
Queue_Node *rear;
} Queue;

void initialize_queue(Queue *queue) {


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

int is_queue_empty(Queue *queue) {


return (???);
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Inserting into a queue

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Inserting into a 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;
node->link=NULL;
if (is_queue_empty(queue)) {
queue->front = node;
queue->rear = node;
} else {
queue->rear->link = node;
queue->rear = node;
}
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Deleting from a queue

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Deleting from a 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 {
queue->front = queue->front->link;
x = node->data;
free(node);
printf("%d was deleted from the queue.\n", x);
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Print contents of a queue

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
Stack C implementation
Queue

Print contents of a 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);
runner = runner->link;
}
printf("\n");
}
}

JMC Yap CS 11 - Intro to Data Structures


Data Structures
Linked Lists Background
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
Linked Lists
Stack
Queue

END OF LESSON 9

JMC Yap CS 11 - Intro to Data Structures

S-ar putea să vă placă și