CSC172 Data Structures Linked Lists (C version)

Slides:



Advertisements
Similar presentations
CSEB324 Data Structures & Algorithms
Advertisements

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
CS Data Structures Chapter 4 Lists.
Memory Layout C and Data Structures Baojian Hua
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Variables and Objects, pointers and addresses: Chapter 3, Slide 1 variables and data objects are data containers with names the value of the variable is.
Dynamic Memory Allocation The process of allocating memory at run time is known as dynamic memory allocation. C does not Inherently have this facility,
Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined at run-time.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
POINTERS IN C Pointer Basics, Pointer Arithmetic, Pointer to arrays and Pointer in functions.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Day 03 Introduction to C.
Computer Science 210 Computer Organization
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
ENEE150 Discussion 07 Section 0101 Adam Wang.
UNIT-3 LINKED LIST.
Linked lists.
Day 03 Introduction to C.
Programming Languages and Paradigms
CSCI 3333 Data Structures Linked Lists.
Programmazione I a.a. 2017/2018.
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Circular Buffers, Linked Lists
Chapter 18: Linked Lists.
Computer Science 210 Computer Organization
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
Understanding Program Address Space
Programming Abstractions
Chapter 17: Linked Lists.
C Programming Lecture-8 Pointers and Memory Management
Chapter 9: Pointers and String
Linked lists.
Module 13 Dynamic Memory.
Dynamic Data Structures
Presentation transcript:

CSC172 Data Structures Linked Lists (C version) Koomen 140916

... Virtual Memory Map (e.g., Unix) 0 1 2 3 4 5 6 7 8 9 65,531 … 65,535 A contiguous sequence of bytes, each one with a unique address

Virtual Memory Map ii ... 0 4 8 12 16 20 24 28 32 36 … 4,294,967,292 A contiguous sequence of words of k bytes each Each word is large enough to hold an integer or address up to 28k - 1

... Virtual Memory Map iii program code static data global vars, constants execution stack local vars dynamic data “heap”

Static data int K; void main() { } Static data is located in fixed positions during the entire run of the program, but positions may differ from one run to the next. 16,768 ≡ 0x4180 ≡ K

Static data int K; void main() { K = 17; } 17 0x4180 ≡ K

Static data int K; … int *P; void main() { K = 17; } 0x4180 ≡ K 17 0x6820 ≡ P

Static data int K; … int *P; void main() { K = 17; P = &K; } 0x4180 ≡ K 17 A variable whose contents is the address of some memory location is called A POINTER 0x4180 0x6820 ≡ P

Static data char myCharData[] = { 'Y', 'o', '!', '\0' }; … char *myString; void main() { myString = myCharData; printf(“%s\n”, myString); } 'Y' 0x3392 ≡ myCharData 'o' '!' '\0' 0x3392 0x5620 ≡ myString

Static data char myCharData[] = { 'Y', 'o', '!', '\0' }; … char *myString; void main() { myString = myCharData; // myString = &myCharData[0]; printf(“%s\n”, myString); } 0x3392 ≡ myCharData 01011001 01101111 00100001 00000000 0x3392 0x5620 ≡ myString

Static data char *myString = “Yo!”; void main() { printf(“%s\n”, myString); } 0x3392 'Y' 'o' '!' 00000000 0x3392 0x5620 ≡ myString

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp means “fetch the value that cp points to” '!' 00000000 *cp ≡ cp[0] &cp[0] ≡ &*cp ≡ cp 0x3392 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3392 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3393 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3393 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3394 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3394 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3395 0x5620 ≡ cp

Static data char *cp; void main() { cp = "Yo!"; while (*cp) printf("%c", *cp++); printf("\n"); } 0x3392 'Y' 'o' *cp++ means “fetch the value that cp points to, and then, as a side effect, change cp to point to the next location” → POINTER ARITHMETIC *(cp+k) ≡ cp[k] , NOT ≡ *cp+k !!! ++*cp ≡ ? *++cp ≡ ? '!' 00000000 0x3395 0x5620 ≡ cp

Static data struct ListNode { int data; struct ListNode *next; } void main() { 0x4860

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; void main() { n1.data = 1963; n2.data = 2014; 1963 0x4860 ≡ n1 2014 0x4868 ≡ n2

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; void main() { n1.data = 1963; n2.data = 2014; n1.next = &n2; n2.next = null; 1963 0x4860 ≡ n1 #0x4868 2014 0x4868 ≡ n2 #0x0

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { n1.data = 1963; n1.next = &n2; n2.data = 2014; n2.next = null; 1963 0x4860 ≡ n1 #0x4868 2014 0x4868 ≡ n2 #0x0 0x6220 ≡ ptr

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { n1.data = 1963; n1.next = &n2; n2.data = 2014; n2.next = null; ptr = &n1; 1963 0x4860 ≡ n1 #0x4868 2014 0x4868 ≡ n2 #0x0 #0x4860 0x6220 ≡ ptr

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { n1.data = 1963; n1.next = &n2; n2.data = 2014; n2.next = null; ptr = &n1; while (ptr) { printf(“%d\n”, ptr->data); ptr = ptr->next; 1963 0x4860 ≡ n1 #0x4868 2014 0x4868 ≡ n2 #0x0 #0x4860 0x6220 ≡ ptr

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { n1.data = 1963; n1.next = &n2; n2.data = 2014; n2.next = null; ptr = &n1; while (ptr) { printf(“%d\n”, ptr->data); ptr = ptr->next; 1963 0x4860 ≡ n1 #0x4868 2014 0x4868 ≡ n2 #0x0 #0x4868 0x6220 ≡ ptr

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { n1.data = 1963; n1.next = &n2; n2.data = 2014; n2.next = null; ptr = &n1; while (ptr) { printf(“%d\n”, ptr->data); ptr = ptr->next; 1963 0x4860 ≡ n1 #0x4868 2014 0x4868 ≡ n2 #0x0 #0x0 0x6220 ≡ ptr

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { 0x4880 ≡ n1 0x4888 ≡ n2 ptr->data means Go to the struct whose address is the contents of the ptr variable, and fetch the value of the 'data' component Same as (*ptr).data 0x0 0x5330 ≡ ptr

Static data struct ListNode { int data; struct ListNode *next; } struct ListNode n1, n2; struct ListNode *ptr; void main() { 0x4880 ≡ n1 0x4888 ≡ n2 ptr = ptr->next means Go to the struct pointed to by the ptr variable, fetch the value of the 'next' component, and store that value in the ptr variable. Same as ptr = (*ptr).next 0x0 0x5330 ≡ ptr

Normally we prefer a more abstract representation of a linked list: ptr 1963 1963 2014

Dynamic data struct ListNode { int data; struct ListNode *next; } struct ListNode *head; void main() { int size = sizeof(struct ListNode); void *rawptr = malloc(size); head = (struct ListNode *) rawptr; head->data = 2015; head->next = null; … free(head); 0xFFF8 0x1234 ≡ head Static data 2015 0xFFF8 0x0 Dynamic data (aka “heap”)

A Linked List abstraction in C typedef int ListData; typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; LinkedList *newLinkedList() { LinkedList *LL = (LinkedList *) malloc( sizeof( LinkedList ) ); LL->head = null; return LL; }

A Linked List abstraction in C - insert() typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; ListNode *newNode( ListData data, ListNode *next ) { ListNode *nodePtr = (ListNode *) malloc( sizeof(ListNode) ); nodePtr->data = data; nodePtr->next = next; return nodePtr; } void LL_insert( LinkedList *LL, ListData data ) LL->head = newNode( data, LL->head );

A Linked List abstraction in C - length() typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; int LL_length( LinkedList *LL ) { int cnt = 0; ListNode *nodePtr = LL->head; while ( nodePtr ) cnt++, nodePtr = nodePtr->next; return cnt; }

A Linked List abstraction in C - empty() typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; void LL_empty( LinkedList *LL ) { ListNode *this, *next; for ( this = LL->head; this != null; this = next) { next = this->next; free(this); } LL->head = null;

A Linked List abstraction in C - get() typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; ListData LL_get( LinkedList *LL, int index ) { ListNode *nodePtr = LL->head; while ( index-- > 0 && nodePtr ) nodePtr = nodePtr->next; if( nodePtr ) return nodePtr->data; else error( “out of bounds” ); }

A Linked List abstraction in C - indexOf() typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; int LL_indexOf( LinkedList *LL, ListData data ) { int index = 0; ListNode *tail = LL->head; while ( tail && tail->data != data ) index++, tail = tail->next; return tail ? index : -1; }

A Linked List abstraction in C - remove() typedef struct LLNode { typedef struct { ListData data; ListNode *head; struct LLNode *next; } LinkedList; } ListNode; void LL_remove( LinkedList *LL, int index ) { LL->head = LN_remove( LL->head, index ); } ListNode *LN_remove( ListNode *this, int index ) ListNode *next; if ( this == null ) return null; if ( index == 0 ) { next = this->next; free(this); return next; this->next = LN_remove( this->next, index-1 ); return this;

A Linked List abstraction in C - append() typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head; } LinkedList; void LL_append( LinkedList *LL, ListData data ) { ListNode *this = LL->head, *tail; if ( this == null ) LL_insert( LL, data ); else { do { tail = this; } while ( this = this->next ); tail->next = newNode( data, null );

A Linked List abstraction in C – append() fast typedef struct LLNode { ListData data; struct LLNode *next; } ListNode; typedef struct { ListNode *head, *tail; } LinkedList; void LL_append( LinkedList *LL, ListData data ) { if ( LL->head == null ) LL_insert( LL, data ); else { LL->tail = LL->tail->next = newNode( data, null ); }