Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104...

Preview:

Citation preview

Abstract Data TypesCMSC 104 Spring 2014, Section 02, Lecture 25

Jason Tang

Topics

• Software Patterns• Linked List• Queue• Stack

Taxonomy of Variables (Review)

• Recall that every variable refers to a unique memory address

Type

scalar

array

pointer

complex

Visibility

function local

global

Lifetime

duration of function

duration of program

Storage Class

static allocation

dynamic allocation

Software Patterns

• In real-life, certain problems appear frequently

• Examples of common software patterns:

• Do some operation on a rectangle (double for loop)

• Find some string within another string (tokenization)

Abstract data Types

• In Computer Science, some of these common software patterns have been given names

• One class of named patterns is the abstract data type (ADT)

• An ADT is a common pattern for declaring variable(s) and function(s) to solve a common problem

Linked List (Intuitive Definition)

Starting Line: California

Finish Line: Las Vegas

China

Road Block

Airport

Malaysia

Road Block

Detour

Airport

Sri Lanka

Detour

Road Block

Airport

Italy

Detour

Road Block

Airport

...

Linked List (Formal Definition)

• Abstract Data Type that consists of a group of nodes that together represent a sequence

• Is a vector data type (the array is another type of vector)

• Each node has:

• One or more data fields

• Pointer to the next node, or NULL if at end of list

Linked List Definition

• This particular linked list holds a string in each node

• Other linked lists hold one or more scalars/arrays/pointers/structures/etc.

• Final field is a pointer to next linked list node

struct llist { char *datum; struct llist *next; };

Graphical Representation

struct llist

datum=”China”

next=0x10000010

struct llist

datum=”Malaysia”

next=0x10000020

struct llist

datum=”Sri Lanka”

next=0x10000030

struct llist

datum=”Italy”

next=NULL

head of list

0x10000000 0x10000010 0x10000020 0x10000030

Traversing a Linked List#include <stdio.h> int main(void) { struct llist *l = NULL; l = append(l, "China"); l = append(l, "Malaysia"); l = append(l, "Sri Lanka"); l = append(l, "Italy"); while (l != NULL) { printf("%s\n", (*l).datum); l = (*l).next; } return 0; }

Traversing a Linked List#include <stdio.h> int main(void) { struct llist *l = NULL; l = append(l, "China"); l = append(l, "Malaysia"); l = append(l, "Sri Lanka"); l = append(l, "Italy"); while (l != NULL) { printf("%s\n", (*l).datum); l = (*l).next; } return 0; }

Will get to append() on next slide

This processes each node in the linked list, by printing

the datum them setting l to the next pointer

Appending to End of Linked List#include <stdlib.h> #include <string.h> struct llist *append(struct llist *l, char *s) { if (l == NULL) { l = malloc(sizeof(struct llist)); (*l).datum = strdup(s); (*l).next = NULL; return l; } else { (*l).next = append((*l).next, s); return l; } }

Appending to End of Linked List#include <stdlib.h> #include <string.h> struct llist *append(struct llist *l, char *s) { if (l == NULL) { l = malloc(sizeof(struct llist)); (*l).datum = strdup(s); (*l).next = NULL; return l; } else { (*l).next = append((*l).next, s); return l; } }

Linked lists are often implemented recursively

Full Linked List Program, Part 1#include <stdio.h> #include <stdlib.h> #include <string.h> struct llist { char *datum; struct llist *next; }; struct llist *append(struct llist *l, char *s); int main(void) { struct llist *l = NULL; l = append(l, "China"); l = append(l, "Malaysia"); l = append(l, "Sri Lanka"); l = append(l, "Italy"); while (l != NULL) { printf("%s\n", (*l).datum); l = (*l).next;

Full Linked List Program, Part 2 } return 0; }

struct llist *append(struct llist *l, char *s) { if (l == NULL) { l = malloc(sizeof(struct llist)); (*l).datum = strdup(s); (*l).next = NULL; return l; } else { (*l).next = append((*l).next, s); return l; } }

Queue

• Real-world examples: deli counter, check-out lines, escalator

• First-In-First-Out (FIFO) behavior

• Technically, an Abstract Data Type that holds a collection of items, without any specific capacity

• Two functions: enqueue and dequeue

• Often implemented via linked list

Queue’s enqueue()

struct llist *enqueue(struct llist *l, char *s) { if (l == NULL) { l = malloc(sizeof(struct llist)); (*l).datum = strdup(s); (*l).next = NULL; return l; } else { (*l).next = enqueue((*l).next, s); return l; } }

Full Queue Program, Part 1#include <stdio.h> #include <stdlib.h> #include <string.h> struct llist { char *datum; struct llist *next; }; struct llist *enqueue(struct llist *l, char *s); int main(void) { struct llist *l = NULL; l = enqueue(l, "Alice"); l = enqueue(l, "Bob"); l = enqueue(l, "Carol"); l = enqueue(l, "Dave"); while (l != NULL) { printf("%s\n", (*l).datum); l = (*l).next; }

Full Queue Program, Part 2 return 0; } struct llist *enqueue(struct llist *l, char *s) { if (l == NULL) { l = malloc(sizeof(struct llist)); (*l).datum = strdup(s); (*l).next = NULL; return l; } else { (*l).next = enqueue((*l).next, s); return l; } }

Stack

• Real-world examples: cars parked in a narrow driveway, batteries in a flashlight

• Last-In-First-Out (LIFO) behavior

• Technically, an Abstract Data Type that holds a collection of items, without any specific capacity

• Two functions: push and pop

• Often implemented via linked list

Stack, Graphically

FordHondToyot

Ferrar1. Toyota shows up, but Ferrari needs to leave soon

2. First pop the Ferrari

3. Next Push the Toyota

4. Then Push the Ferrari

FordHondFerrar Toyot

FordHondFerrar

Toyot

FordHondFerrar Toyot

Stack’s push()

struct llist *push(struct llist *l, char *s) { struct llist *l2 = malloc(sizeof(struct llist)); (*l2).datum = strdup(s); (*l2).next = l; return l2; }

Full Stack Program, Part 1#include <stdio.h> #include <stdlib.h> #include <string.h> struct llist { char *datum; struct llist *next; }; struct llist *push(struct llist *l, char *s); int main(void) { struct llist *l = NULL; l = push(l, "ford"); l = push(l, "honda"); l = push(l, "toyota"); l = push(l, "ferrari");

Full Stack Program, Part 2 while (l != NULL) { printf("%s\n", (*l).datum); l = (*l).next; } return 0; } struct llist *push(struct llist *l, char *s) { struct llist *l2 = malloc(sizeof(struct llist)); (*l2).datum = strdup(s); (*l2).next = l; return l2; }

Summary

• Abstract Data Types (ADTs) are solutions to common software problems

• ADTs are building blocks to more complex programs

• Linked List is the basis for many other ADTs:

• Queue

• Stack

Recommended