Data Structures and Algorithms for Engineers

Similar documents
Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers

Cpt S 122 Data Structures. Data Structures

Pointers, Dynamic Data, and Reference Types

PROGRAMMAZIONE I A.A. 2017/2018

Algorithms and Data Structures CS-CO-412

Data Structures and Algorithms for Engineers

Data Structure with C. List

Class Information ANNOUCEMENTS

BBM 201 DATA STRUCTURES

Dynamic Allocation of Memory

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

CMSC 341 Lecture 7 Lists

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

Linked List. April 2, 2007 Programming and Data Structure 1

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Quiz 0 Review Session. October 13th, 2014

CA341 - Comparative Programming Languages

CS61C Midterm Review on C & Memory Management

Introduction to Linked Lists

ECE 15B COMPUTER ORGANIZATION

Recitation #11 Malloc Lab. November 7th, 2017

Linked Lists Structures

JTSK Programming in C II C-Lab II. Lecture 3 & 4

CS349/SE382 A1 C Programming Tutorial

Chapter 17: Linked Lists

Programming in C/C Lecture 2

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Dynamic Allocation of Memory

Low-Level C Programming. Memory map Pointers Arrays Structures

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Data Structure Series

Lectures 13 & 14. memory management

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

ECE 2400 Computer Systems Programming Fall 2017 Topic 4: C Pointers

Short Notes of CS201

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 241 Data Organization Binary Trees

Self-referential Structures and Linked List. Programming and Data Structure 1

o Code, executable, and process o Main memory vs. virtual memory

CS201 - Introduction to Programming Glossary By

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

ADT: Design & Implementation

Dynamic Memory Allocation

Linked List using a Sentinel

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

CS201- Introduction to Programming Current Quizzes

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

Data Structures and Algorithms for Engineers

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Lecture 2, September 4

CSCI 171 Chapter Outlines

However, in C we can group related variables together into something called a struct.

CP2 Revision. theme: dynamic datatypes & data structures

Solution for Data Structure

C++ for Java Programmers

CS24 Week 2 Lecture 1

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Ashish Gupta, Data JUET, Guna

CA31-1K DIS. Pointers. TA: You Lu

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Data Structures and Algorithms for Engineers

Programs in memory. The layout of memory is roughly:

Lecture Notes on Memory Management

Dynamic Memory Management! Goals of this Lecture!

Run-time Environments - 3

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

CS61C : Machine Structures

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Dynamic Memory Allocation and Linked Lists

Chapter 17: Linked Lists

Limitations of the stack

Dynamic Memory Management

Motivation was to facilitate development of systems software, especially OS development.

Lecture 2: C Programm

struct node{ int info; struct node *left, *right; }; typedef struct node nodeptr; A Linear Doubly Linked List

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Common Misunderstandings from Exam 1 Material

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Run Time Environment

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Lecture 8 Data Structures

CS 61c: Great Ideas in Computer Architecture

Memory management. Johan Montelius KTH

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

CS61, Fall 2012 Section 2 Notes

Dynamic memory. EECS 211 Winter 2019

Lecture 4: Elementary Data Structures Steven Skiena

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

Dynamic Data Structures (II)

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Transcription:

04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University Africa

Lecture 9 Containers and Dictionaries Containers Dictionaries List ADT Array implementation Linked list implementation Data Structures and Algorithms for Engineers 2 Carnegie Mellon University Africa

Aside: Linked Lists Using Pointers Data Structures and Algorithms for Engineers 3 Carnegie Mellon University Africa

Why Pointer-Based Implementation? Linked lists are used avoid excessive data movement with insertions and deletions Elements are not necessarily stored in contiguous memory locations Makes efficient use of memory space Allocate space when needed Deallocate space when finished & return it to the free store Failure to deallocate space will cause memory leakage Data Structures and Algorithms for Engineers 4 Carnegie Mellon University Africa

Why Pointer-Based Implementation? Some guidelines when writing programs that dynamically allocate memory Use malloc or new to create data-structures of the appropriate size Remember to avoid memory leakage by always using free and delete to deallocate dynamically-created data-structures Check every call to malloc or new to see if it returned NULL (i.e. chec if the allocation was unsuccessful) Data Structures and Algorithms for Engineers 5 Carnegie Mellon University Africa

Why Pointer-Based Implementation? Some guidelines when writing programs that dynamically allocate memory You must expect free or delete to alter the contents of the memory that was freed or deleted Never access a data structure after it has been freed or deleted If malloc fails in a non-interactive program, make that a fatal error In an interactive program, it is better to abort the current command and return to the command reader loop Data Structures and Algorithms for Engineers 6 Carnegie Mellon University Africa

A Linked List A linked list is a list in which the order of the components is determined by an explicit link member in each node The nodes are structs each node contains a component member and also a link member that gives the location of the next node in the list Data Structures and Algorithms for Engineers 7 Carnegie Mellon University Africa

Pointer-Based (Dynamic) Linked List A pointer-based linked list is a dynamic linked list where nodes are linked together by pointers, and an external pointer (or head pointer) points to the first node in the list Data Structures and Algorithms for Engineers 8 Carnegie Mellon University Africa

Nodes can be located anywhere in memory The link member holds the memory address of (or a reference to) of the next node in the list Data Structures and Algorithms for Engineers 9 Carnegie Mellon University Africa

Declarations for a Dynamic Linked List // Type DECLARATIONS struct NodeType { char info; NodeType* next; typedef NodeType* NodePtr; // Variable DECLARATIONS NodePtr head; NodePtr ptr; Data Structures and Algorithms for Engineers 10 Carnegie Mellon University Africa

Pointer Dereferencing and Member Selection ptr ptr A 6000. info. next ptr is a pointer to a node *ptr ptr A 6000. info. next *ptr is the entire node pointed to by ptr ptr A 6000. info. next (*ptr).info ~ ptr->info (*ptr).next ~ ptr->next ptr A 6000. info. next ptr->info is a node member ptr->next is a node member Data Structures and Algorithms for Engineers 11 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 12 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 13 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 14 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 15 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 16 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 17 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 18 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 19 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 20 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 21 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 22 Carnegie Mellon University Africa

Traversing a Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr!= NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->next ; Data Structures and Algorithms for Engineers 23 Carnegie Mellon University Africa

Using Operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated. The dynamically allocated object exists until the delete operator destroys it. Data Structures and Algorithms for Engineers 24 Carnegie Mellon University Africa 24

Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 25 Carnegie Mellon University Africa

Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 26 Carnegie Mellon University Africa

Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 27 Carnegie Mellon University Africa

Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 28 Carnegie Mellon University Africa

Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 29 Carnegie Mellon University Africa

Inserting a Node at the Front of a List Data Structures and Algorithms for Engineers 30 Carnegie Mellon University Africa

Using Operator delete The object currently pointed to by the pointer is deallocated, and the pointer is considered undefined. The object s memory is returned to the free store. Data Structures and Algorithms for Engineers 31 Carnegie Mellon University Africa 31

Deleting the first node from a list Data Structures and Algorithms for Engineers 32 Carnegie Mellon University Africa

Deleting the first node from a list Data Structures and Algorithms for Engineers 33 Carnegie Mellon University Africa

Deleting the first node from a list link Data Structures and Algorithms for Engineers 34 Carnegie Mellon University Africa

Deleting the first node from a list Data Structures and Algorithms for Engineers 35 Carnegie Mellon University Africa

Deleting the first node from a list Data Structures and Algorithms for Engineers 36 Carnegie Mellon University Africa

End of Aside: Linked Lists Using Pointers Data Structures and Algorithms for Engineers 37 Carnegie Mellon University Africa

Header node element pointer NULL pointer List Data Structures and Algorithms for Engineers 38 Carnegie Mellon University Africa

Header node An empty list!!! List Data Structures and Algorithms for Engineers 39 Carnegie Mellon University Africa

List window To place the window at this position we provide a link to the previous node (this is why we need a header node) Data Structures and Algorithms for Engineers 40 Carnegie Mellon University Africa

List window To place the window at end of the list we provide a link to the last node Data Structures and Algorithms for Engineers 41 Carnegie Mellon University Africa

List window To insert a node at this window position we create the node and re-arrange the links Data Structures and Algorithms for Engineers 42 Carnegie Mellon University Africa

List window temp To insert a node at this window position we create the node and re-arrange the links Data Structures and Algorithms for Engineers 43 Carnegie Mellon University Africa

List window To delete a node at this window position we re-arrange the links and free the node Data Structures and Algorithms for Engineers 44 Carnegie Mellon University Africa

temp List window To delete a node at this window position we re-arrange the links and free the node Data Structures and Algorithms for Engineers 45 Carnegie Mellon University Africa

List window To delete a node at this window position we re-arrange the links and free the node Data Structures and Algorithms for Engineers 46 Carnegie Mellon University Africa

type elementtype type LIST type Boolean type windowtype Data Structures and Algorithms for Engineers 47 Carnegie Mellon University Africa

/* linked-list implementation of LIST ADT */ #include <stdio.h> #include <math.h> #include <.h> #define FALSE 0 #define TRUE 1 typedef struct { int ; char *; ELEMENT_TYPE; Data Structures and Algorithms for Engineers 48 Carnegie Mellon University Africa

typedef struct node *NODE_TYPE; typedef struct node { ELEMENT_TYPE element; NODE_TYPE next; NODE; typedef NODE_TYPE LIST_TYPE; typedef NODE_TYPE WINDOW_TYPE; Data Structures and Algorithms for Engineers 49 Carnegie Mellon University Africa

typedef struct node *NODE_TYPE; /* alternative approach... */ /* but need to use sizeof(struct node) in malloc()*/ struct node { ELEMENT_TYPE element; NODE_TYPE next; ; typedef NODE_TYPE LIST_TYPE; typedef NODE_TYPE WINDOW_TYPE; Data Structures and Algorithms for Engineers 50 Carnegie Mellon University Africa

/*** position following last element in a list ***/ WINDOW_TYPE end(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { while (q->next!= NULL) { q = q->next; return(q); *list q Data Structures and Algorithms for Engineers 51 Carnegie Mellon University Africa

/*** position following last element in a list ***/ WINDOW_TYPE end(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { while (q->next!= NULL) { q = q->next; return(q); *list q Data Structures and Algorithms for Engineers 52 Carnegie Mellon University Africa

/*** position following last element in a list ***/ WINDOW_TYPE end(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { while (q->next!= NULL) { q = q->next; return(q); *list q Data Structures and Algorithms for Engineers 53 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list q Data Structures and Algorithms for Engineers 54 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 55 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 56 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 57 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 58 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 59 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 60 Carnegie Mellon University Africa

/*** empty a list ***/ WINDOW_TYPE empty(list_type *list) { WINDOW_TYPE p, q; if (*list!= NULL) { /* list exists: delete all nodes including header */ q = *list; while (q->next!= NULL) { p = q; q = q->next; free(p); free(q) *list p q Data Structures and Algorithms for Engineers 61 Carnegie Mellon University Africa

/* now, create a new empty one with a header node */ if ((q = (NODE_TYPE) malloc(sizeof(node))) == NULL) error( function empty: unable to allocate memory ); else { q->next = NULL; *list = q; return(end(list)); *list p q Data Structures and Algorithms for Engineers 62 Carnegie Mellon University Africa

/* now, create a new empty one with a header node */ if ((q = (NODE_TYPE) malloc(sizeof(node))) == NULL) error( function empty: unable to allocate memory ); else { q->next = NULL; *list = q; return(end(list)); *list p q Data Structures and Algorithms for Engineers 63 Carnegie Mellon University Africa

/* now, create a new empty one with a header node */ if ((q = (NODE_TYPE) malloc(sizeof(node))) == NULL) error( function empty: unable to allocate memory ); else { q->next = NULL; *list = q; return(end(list)); *list p q Data Structures and Algorithms for Engineers 64 Carnegie Mellon University Africa

/*** test to see if a list is empty ***/ int is_empty(list_type *list) { WINDOW_TYPE q; q = *list; if (q == NULL) { error( non-existent list ); else { if (q->next == NULL) { return(true); *list else return(false); q *list q Data Structures and Algorithms for Engineers 65 Carnegie Mellon University Africa

/*** position at first element in a list ***/ WINDOW_TYPE first(list_type *list) { if (is_empty(list) == FALSE) { return(*list); else return(end(list)); *list end(list) Data Structures and Algorithms for Engineers 66 Carnegie Mellon University Africa

/*** position at next element in a list ***/ WINDOW_TYPE next(window_type w, LIST_TYPE *list) { if (w == last(list)) { return(end(list)); else if (w == end(list)) { error( can t find next after end of list ); else { return(w->next); *list w W->next end(list) Data Structures and Algorithms for Engineers 67 Carnegie Mellon University Africa

/*** position at previous element in a list ***/ WINDOW_TYPE previous(window_type w, LIST_TYPE *list) { WINDOW_TYPE p, q; if (w!= first(list)) { p = first(list); while (p->next!= w) { p = p->next; if (p == NULL) break; /* trap this to ensure */ /* we don t dereference */ if (p!= NULL) /* a null pointer in the */ return(p); /* while condition */ *list p Data Structures and Algorithms for Engineers 68 Carnegie Mellon University Africa w

/*** position at previous element in a list ***/ WINDOW_TYPE previous(window_type w, LIST_TYPE *list) { WINDOW_TYPE p, q; if (w!= first(list)) { p = first(list); while (p->next!= w) { p = p->next; if (p == NULL) break; /* trap this to ensure */ /* we don t dereference */ if (p!= NULL) /* a null pointer in the */ return(p); /* while condition */ *list p Data Structures and Algorithms for Engineers 69 Carnegie Mellon University Africa w

else { error( can t find previous to a non-existent node ); else { error( can t find previous before first element of list ); return(w); Data Structures and Algorithms for Engineers 70 Carnegie Mellon University Africa

/*** position at last element in a list ***/ WINDOW_TYPE last(list_type *list) { WINDOW_TYPE p, q; if (*list == NULL) { error( non-existent list ); else { /* list exists: find last node */ Data Structures and Algorithms for Engineers 71 Carnegie Mellon University Africa

/* list exists: find last node */ if (is_empty(list)) { p = end(list); else { p = *list; q = p->next; while (q->next!= NULL) { p = q; q = q->next; return(p); *list p q Data Structures and Algorithms for Engineers 72 Carnegie Mellon University Africa

/* list exists: find last node */ if (is_empty(list)) { p = end(list); else { p = *list; q = p->next; while (q->next!= NULL) { p = q; q = q->next; return(p); *list Data Structures and Algorithms for Engineers 73 Carnegie Mellon University Africa p q

/* list exists: find last node */ if (is_empty(list)) { p = end(list); else { p = *list; q = p->next; while (q->next!= NULL) { p = q; q = q->next; return(p); *list p q Data Structures and Algorithms for Engineers 74 Carnegie Mellon University Africa

/*** insert an element in a list ***/ LIST_TYPE *insert(element_type e, WINDOW_TYPE w, LIST_TYPE *list) { WINDOW_TYPE temp; if (*list == NULL) { error( cannot insert in a non-existent list ); Data Structures and Algorithms for Engineers 75 Carnegie Mellon University Africa

else { /* insert it after w */ temp = w->next; if ((w->next = (NODE_TYPE) malloc(sizeof(node))) = NULL) error( function insert: unable to allocate memory ); else { w->next->element = e; w->next->next = temp; return(list); *list w temp Data Structures and Algorithms for Engineers 76 Carnegie Mellon University Africa

else { /* insert it after w */ temp = w->next; if ((w->next = (NODE_TYPE) malloc(sizeof(node))) = NULL) error( function insert: unable to allocate memory ); else { w->next->element = e; w->next->next = temp; return(list); *list w temp Data Structures and Algorithms for Engineers 77 Carnegie Mellon University Africa

else { /* insert it after w */ temp = w->next; if ((w->next = (NODE_TYPE) malloc(sizeof(node))) = NULL) error( function insert: unable to allocate memory ); else { w->next->element = e; w->next->next = temp; return(list); e. e. *list w temp Data Structures and Algorithms for Engineers 78 Carnegie Mellon University Africa

/*** delete an element from a list ***/ LIST_TYPE *delete(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot delete from a non-existent list ); else { p = w->next; /* node to be deleted */ w->next = w->next->next; /* rearrange the links */ free(p); /* delete the node */ return(list); *list w p Data Structures and Algorithms for Engineers 79 Carnegie Mellon University Africa

/*** delete an element from a list ***/ LIST_TYPE *delete(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot delete from a non-existent list ); else { p = w->next; /* node to be deleted */ w->next = w->next->next; /* rearrange the links */ free(p); /* delete the node */ return(list); *list w p Data Structures and Algorithms for Engineers 80 Carnegie Mellon University Africa

/*** delete an element from a list ***/ LIST_TYPE *delete(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot delete from a non-existent list ); else { p = w->next; /* node to be deleted */ w->next = w->next->next; /* rearrange the links */ free(p); /* delete the node */ return(list); *list w p Data Structures and Algorithms for Engineers 81 Carnegie Mellon University Africa

/*** retrieve an element from a list ***/ ELEMENT_TYPE retrieve(window_type w, LIST_TYPE *list) { WINDOW_TYPE p; if (*list == NULL) { error( cannot retrieve from a non-existent list ); else { return(w->next->element); *list w Data Structures and Algorithms for Engineers 82 Carnegie Mellon University Africa

/*** print all elements in a list ***/ int print(list_type *list) { WINDOW_TYPE w; ELEMENT_TYPE e; printf( Contents of list: \n ); w = first(list); while (w!= end(list)) { e = retrieve(window_type w, LIST_TYPE *list); printf( %d %s\n, e., e.); w = next(w, list); printf( ---\n ); return(0); Data Structures and Algorithms for Engineers 83 Carnegie Mellon University Africa

/*** error handler: print message passed as argument and take appropriate action ***/ int error(char *s); { printf( Error: %s\n, s); exit(0); Data Structures and Algorithms for Engineers 84 Carnegie Mellon University Africa

/*** assign values to an element ***/ int assign_element_values(element_type *e, int, char s[]) { e-> = (char *) malloc(sizeof(char) * (strlen(s)+1)); strcpy(e->, s); e-> = ; e Data Structures and Algorithms for Engineers 85 Carnegie Mellon University Africa

/*** assign values to an element ***/ int assign_element_values(element_type *e, int, char s[]) { e-> = (char *) malloc(sizeof(char) * (strlen(s)+1)); strcpy(e->, s); e-> = ; e Data Structures and Algorithms for Engineers 86 Carnegie Mellon University Africa

/*** initialize the list pointer to make sure ***/ /*** all subsequent checks on its value are valid ***/ void initialize_list(list_type *list) { *list = NULL; *list Data Structures and Algorithms for Engineers 87 Carnegie Mellon University Africa

/*** main driver routine ***/ WINDOW_TYPE w; ELEMEN_TYPE e; LIST_TYPE list; int i; initialize_list(&list); empty(&list); print(&list); assign_element_values(&e, 1, String A ); w = first(&list); insert(e, w, &list); print(&list); Data Structures and Algorithms for Engineers 88 Carnegie Mellon University Africa

assign_element_values(&e, 2, String B ); insert(e, w, &list); print(&list); assign_element_values(&e, 3, String C ); insert(e, last(&list), &list); print(&list); assign_element_values(&e, 4, String D ); w = next(last(&list), &list); insert(e, w, &list); print(&list); Data Structures and Algorithms for Engineers 89 Carnegie Mellon University Africa

w = previous(w, &list); delete(w, &list); print(&list); Data Structures and Algorithms for Engineers 90 Carnegie Mellon University Africa

Key points: All we changed was the implementation of the data-structure and the access routines But by keeping the interface to the access routines the same as before, these changes are transparent to the user And we didn t have to make any changes in the main function which was actually manipulating the list Data Structures and Algorithms for Engineers 91 Carnegie Mellon University Africa

Key points: In a real software system where perhaps hundreds (or thousands) of people are using these list primitives, this transparency is critical We couldn t have achieved it if we manipulated the data-structure directly Data Structures and Algorithms for Engineers 92 Carnegie Mellon University Africa

Possible problems with the implementation: we have to run the length of the list in order to find the end (i.e. end(l) is O(n)) there is a (small) overhead in using the pointers On the other hand, the list can now grow as large as necessary, without having to predefine the maximum size Data Structures and Algorithms for Engineers 93 Carnegie Mellon University Africa

List We can also have a doubly-linked list; this removes the need to have a header node and make finding the previous node more efficient Data Structures and Algorithms for Engineers 94 Carnegie Mellon University Africa

List Lists can also be circular Data Structures and Algorithms for Engineers 95 Carnegie Mellon University Africa

Comparison: Linked Lists vs. Arrays Relative advantages of linked lists Overflow on linked structures can never occur unless memory is actually full Insertions and deletions are simpler than for contiguous (array) lists With large records, moving pointers is easier and faster than moving the items themselves Data Structures and Algorithms for Engineers 96 Carnegie Mellon University Africa

Comparison: Linked Lists vs. Arrays Relative advantages of arrays Linked structures require extra space for storing pointer fields Linked lists do not allow efficient random access to items Arrays allow better memory locality and cache performance than random pointer jumping Dynamic memory allocation provides us with flexibility on how and where to use limited storage resources Data Structures and Algorithms for Engineers 97 Carnegie Mellon University Africa

Comparison: Linked Lists vs. Arrays Both lists and arrays can be thought of a recursive objects: Lists: chopping off the first element of a linked list leaves a smaller linked list Lists are recursive objects Splitting the first k elements off an n element array give two smaller arrays, of size k and n-k, respectively Arrays are recursive objects This shows us that lists are amenable to efficient (recursive) divide-andconquer algorithms, such as binary search and quicksort Data Structures and Algorithms for Engineers 98 Carnegie Mellon University Africa