abstract data types - department of computer science and ...€¦ · abstract data types cmsc 104...

25
Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang

Upload: others

Post on 02-May-2020

26 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Jason Tang

Page 2: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

Topics

• Software Patterns• Linked List• Queue• Stack

Page 3: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 4: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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)

Page 5: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 6: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

...

Page 7: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 8: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; };

Page 9: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 10: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; }

Page 11: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 12: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; } }

Page 13: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 14: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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;

Page 15: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; } }

Page 16: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 17: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; } }

Page 18: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; }

Page 19: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; } }

Page 20: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 21: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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

Page 22: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; }

Page 23: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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");

Page 24: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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; }

Page 25: Abstract Data Types - Department of Computer Science and ...€¦ · Abstract Data Types CMSC 104 Spring 2014, Section 02, Lecture 25 Jason Tang. Topics • Software Patterns •

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