Linked Lists... An introduction to creating dynamic data structures.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Pointers.
Linked Lists Mohammed Almashat CS /03/2006.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
Dynamic memory allocation
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
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.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Introduction to Data Structures Systems Programming.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Introduction to Data Structures Systems Programming Concepts.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
CS261 Data Structures Linked Lists - Introduction.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Pointers and Linked Lists
Chapter 12 – Data Structures
Pointers and Linked Lists
UNIT – I Linked Lists.
UNIT-3 LINKED LIST.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Popping Items Off a Stack Lesson xx
Dynamic Memory Allocation
Memory Allocation CS 217.
Review & Lab assignments
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
(1 - 2) Introduction to C Data Structures & Abstract Data Types
Dynamic Data Structures
Presentation transcript:

Linked Lists... An introduction to creating dynamic data structures

Post-conditions To extend understanding of pointers by using them to create dynamic data structures Understand when to use a Linked List Be able to create a Linked List in C –understand internals –be able to program them!

Outline Linked Lists introduction Characteristics of Linked Lists Programming Linked Lists

Linked Lists definition Abstract common use of “list” –TODO: Read task description Design test data Create makefile Collect other files needed Begin working on code Type in first version Test it …

Linked Lists and pointers The word “list” suggests an ordered collection The word “linked” hints at pointers to where the next element in the list is located –This is different from arrays, which use contiguous memory locations –Linked lists may use multiple links to link elements according to different interpretations of “order”. –We will only consider “singly linked lists” for now.

Linked Lists memory diagrams A list of the numbers [0, 12, -4, 22] An array of the numbers [0, 12, -4, 22] Where are arrays stored? –Global/static/extern/const? –Stack? –Heap? Where are linked lists stored?

Linked Lists: Internals The dynamic nature of the linked list data structure means we must allocate and free memory on demand Some languages do this for you. Not so for C - you have to do it explicitly using malloc/calloc/realloc/free.

Linked Lists: Internals Here is a high-level schematic of a linked list. A pointer enables us to reference its 1st element. Element 0 Element 1 Element 2 Pointer

Things to consider in test design Three distinctive parts of the list - what are they? Element 0 Element 1 Element 2 Pointer

Linked Lists: Internals Each element in our singly-linked list has two components: –the data component anything you want... –the link component a pointer, of course! Data Link

Linked Lists: Internals We now expose the internals of the previous schematic. Element 0 Element 1 Element 2 Pointer Data 0 Pointer Data 1 Pointer Data 2 Pointer NULL Q: How might a Linked List be terminated? A: By our “old friend”, the NULL pointer...

Linked Lists: Internals Adding an element to the front of the list. Element 1 Element 2 Pointer Element 0 Data Pointer Data 0 Pointer Data 1 Pointer NULL

Linked Lists: Internals Adding an element to the front of the list. Element 1 Element 2 Element 0 Data 0 Pointer Data 2 Pointer NULL Data 1 Pointer

Linked Lists: Internals New links are forged by simple assignment operations. Element 1 Element 2 Element 0 Data 0 Pointer Data 2 Pointer NULL Data 1 Pointer Q: What happens if we change the order? A: Ooops!

Linked Lists: Internals Using “boxes and arrows” to show what we are doing with pointers is very helpful… But it is easy to lose sight of one simple thing: One pointer can only point to one thing! Every time we “draw an arrow” we effectively erase any existing arrow coming from that box. In the previous example we “lost” the linked list because we neglected this!

Element 1 Data Pointer Linked Lists: Internals Adding an element elsewhere. Element 0 Element 2 Pointer Data 0 Pointer Data 1 Pointer NULL

Element 1 Data 1 Pointer Linked Lists: Internals Adding an element elsewhere. Element 0 Element 2 Data 0 Pointer Data 2 Pointer NULL

Characteristics of Linked Lists What is the worst-case situation for altering the n-th element of: –an array? –a linked list? Altering an element anywhere in the array has the same cost. Arrays elements are referred to by their address offset and have O(1) cost. Altering an element to the end of the linked list is more costly as the list has to be traversed. The cost is therefore O(n).

Characteristics of Linked Lists Advantages of linked lists: Dynamic nature of the data structure Flexible structure –singly linked lists –doubly linked –circular lists –etc., etc.

Characteristics of Linked Lists Disadvantages of linked lists: Linear worst-case cost of access The absence of a Linked List implementation in standard C libraries

Programming Linked Lists The dynamic nature of the data structure means we must allocate and free memory – malloc() to allocate memory when we add an element – free() to de-allocate memory when we remove an element

Programming Linked Lists The link element we will use for the example: typedef struct List List; struct List { char *content; List *next; } Could be anything, of course!! Note the self-referential nature of the pointer; it points to one of these structures. char * List * ”Hello”

Programming Linked Lists Adding an element to the front of the list. List *addToFront(List *listp, List *newp) { newp->next = listp; return newp; } Draw memory diagrams for: empty list; list with one element; list with three elements.

List *addToFront(List *listp, List *newp) { newp->next = listp; return newp; } char * List * List *listp char * List * List *newp char * NULL Return this pointer

Programming Linked Lists Adding an element to the end of the list. List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ;/* null statement */ p->next = newp; return listp; }

Programming Linked Lists Draw pictures of case of empty list *listp List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ;/* null statement */ p->next = newp; return listp; }

NULL char * NULL List *newp List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; p->next = newp; return listp; } Return this pointer

Programming Linked Lists Draw pictures of case of list *listp containing 2 elements List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ;/* null statement */ p->next = newp; return listp; }

char * List * List *listp char * NULL List *newp char * NULL List *addToEnd(List *listp, List *newp) { List *p; if (listp == NULL) return newp; for (p = listp; p->next != NULL; p = p->next) ; p->next = newp; return listp; } List *p List *

Programming Linked Lists De-allocating a complete list void freeAll(List *listp) { List *p; for ( ; listp != NULL; listp=p) { p = listp->next; free(listp->content);/* the string */ free(listp); }

Programming Linked Lists Draw the memory diagram for a 5 element list. void freeAll(List *listp) { List *p; for ( ; listp != NULL; listp=p) { p = listp->next; free(listp->content);/* the string */ free(listp); }

char * List * char * NULL char * List * void freeAll(List *listp) { List *p; for ( ; listp != NULL; listp=p) { p = listp->next; free(listp->content); free(listp); } List *p NULL List *listp NULL

Programming Linked Lists Write a function that deletes the first element in a list. List * deleteFirst(List *listp) { List *p; if (listp != NULL) { p = listp; listp = listp->next; free(p); } return listp; }

Programming Linked Lists Write a function that counts the number of elements in a list. int count(List *listp) { int cnt = 0; for ( ; listp != NULL; cnt++) listp = listp->next; return cnt; }

Post-conditions To extend understanding of pointers by using them to create dynamic data structures Understand when to use a Linked List Be able to create a Linked List in C understand internals be able to program them!