Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
Lists and TreesCS-2301 D-term Data Structures — Lists and Trees CS-2301 System Programming D-term 2009 (Slides include materials from The C Programming.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Lecture 9  Arrays  Declaration  Initialization  Applications  Pointers  Declaration  The & and * operators  NULL pointer  Initialization  Readings:
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Pointers Applications
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Data Structures Using C++ 2E
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked List by Chapter 5 Linked List by
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.
Review 1 List Data Structure List operations List Implementation Array Linked List.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
Data Structure & Algorithms
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Introduction to Linked Lists
Linked Lists Chapter 6 Section 6.4 – 6.6
Java Primer 1: Types, Classes and Operators
UNIT-3 LINKED LIST.
Pointers and References
Introduction to Linked Lists
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Pointers.
Linked Lists in C and C++
Chapter 16 Linked Structures
Presentation transcript:

Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably performed several operations on arrays, such as sorting, inserting, deleting, and searching. If data is not sorted, then searching for an item in the array can be very time-consuming, especially with large arrays. Once the data is sorted, we can use a binary search and improve the search algorithm; however, insertion and deletion become time-consuming, especially with large arrays, because these operations require data movement. With an array of fixed size, new items can be added only if there is room. Thus, there are limitations when organizing data in an array.

Introduction to Linked Lists Linked List a data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship. elements are allocated separately and only when needed elements are usually the same type (but not always) each element of the structure is typically called a node Example of a singly-linked list with a head and tail pointer: - indicates that the pointer is NULL

Introduction to Linked Lists Advantages over arrays –We can increase the size of the linked list one item at a time dynamically. –Overcomes limitations with Arrays: Inserting/Deleting is time-consuming in an array, especially large arrays, because these operations require data movement. With an array of fixed size, new items can be added only if there is room.

Introduction to Linked Lists Types of Linked Lists A singly linked list as previously described A doubly linked list - a list that has two references, one to the next node and another to the previous node

Singly Linked List struct listItem { type payload; struct listItem *next; }; struct listItem *head; payload next payload next payload next payload next

Adding an Item to a List struct listItem *p, *q; Add an item pointed to by q after item pointed to by p –Neither p nor q is NULL payload next payload next payload next payload next payload next

Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next

Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next

Adding an Item to a List listItem *addAfter(listItem *p, listItem *q){ q -> next = p -> next; p -> next = q; return p; } payload next payload next payload next payload next payload next Question: What should we do if we cannot guarantee that p and q are non-NULL?

Adding an Item to a List (continued) listItem *addAfter(listItem *p, listItem *q){ if (p && q) { q -> next = p -> next; p -> next = q; } return p; } payload next payload next payload next payload next payload next Note test for non-null p and q

What about Adding an Item before another Item? struct listItem *p; Add an item before item pointed to by p ( p != NULL ) payload next payload next payload next payload next payload next

What about Adding an Item before another Item? Answer:– – Need to search list from beginning to find previous item – Add new item after previous item

Linked Lists (continued) We want to develop a list management system that allows us to maintain lists of any type of data (integers, floats, structures, etc.). We will use void pointers to accomplish this.

void Pointers and type casting A void pointer (i.e., void *) is a generic pointer that can hold the address of any pointer type. Consider the following segment of code on the right: The void pointer ptr can be set to point to any of the objects, and any type pointer can be set to ptr Example: int x; float y; typedef struct data_type { int f1; int f2; } data_t; data_t d1; data_t *dptr; void *ptr; ptr = &x; ptr = &y; ptr = &d1; ptr = dptr; dptr = ptr;

void Pointers and type casting Although a void * can be set to point to any type, you should not assign a pointer to a given type to a pointer to another type. Consider the code on the right int x = 55; float y = 83.7; int *iPtr; int *fPtr = &y; iPtr = fPtr; you will get the following message : “…warning: assignment from incompatible pointer type” and generally unexpected behavior.

void Pointers and type casting You can never dereference a void pointer. i.e., void * pointers can never be directly used to access the memory to which they point because the size and type of the location are unknown. Example: int x; float y; typedef struct data_type { int f1; int f2; } data_t; data_t d1; data_t *dptr; void *ptr; … x = *ptr; …warning: dereferencing 'void *' pointer …error: invalid use of void expression

void Pointers and type casting There are two ways to avoid this problem: 1.type casting: You can “tell” the compiler the type of the data that “ptr” is pointing to, i.e.: x = *(int *)ptr; This is telling the compiler to treat ptr as a pointer to something of type “int”, and then copy the integer contents it is "pointing to" to x. 2.Define a separate integer pointer and copy ptr to it; the other technique is to simply define another pointer, i.e.: int *intPtr; intPtr = ptr; x = *intPtr;

void Pointers and type casting Defining a few extra pointers of the appropriate type may result in more readable code and will likely be less error- prone (but either approach is okay). Here is another example using the structure on the right. Assume we want to set the “f2” field in “d1”. Note: ptr is pointing to d1 Example: int x; float y; typedef struct data_type { int f1; int f2; } data_t; data_t d1; data_t *dptr; void *ptr = &d1; /* set f2 field in d1 */ ((data_t *)ptr)->f1 = 6; /* or */ dptr = ptr; dptr->f1 = 6;

Linked List (continued) A linked list is a very useful data structure and many of the techniques used to manipulate linked lists are applicable to other data structures. We will use linked lists to organize scene objects, and lights in the raytracer. In lab, you will develop a list management module to be used with the raytracer.

Linked List (continued) struct list_type { node_t *head; node_t *tail; }; struct node_type { struct node_t *head; void *objPtr; }; struct node_type { struct node_t *head; void *objPtr; }; object1object2 The characteristics of the lists used by the raytracer include the following: 1.Newly created structures are always added to the end of the appropriate list. 2.Individual structures are never deleted from the list 3.Lists are always processed sequentially from beginning to end 4.We desire a single generic mechanism that can manage lists of the two different structure types

List data structures The node_t structure The following example creates a new type name, node_t, which is 100% equivalent to struct node_type /** List node **/ typedef struct node_type { struct node_type *next; /* Pointer to next node */ void *objPtr; /* Pointer to associated object */ } node_t; There is a single instance of the node_t structure for each element in each list. Note we must use the "official" name struct node_type when declaring next because node_t is not known to be a type definition until 3 lines later! Each node contains two pointers: –One is to the next node_t in the list. –The other points to the actual data being managed by the list

List data structures The node_t structure /** List node **/ typedef struct node_type { struct node_type *next; /* Pointer to next node */ void *objPtr; /* Pointer to associated object */ } node_t; The ojbPtr pointer is declared to be of type void *. As stated earlier, –A void * pointer is a pointer to something of unknown or generic type. –In the raytracer, depending on the list being processed, the objPtr might be a sobj_t or a light_t –void * pointers can be freely assigned to other pointers –void * pointers can never be directly used to access the memory to which they point because the size and type of the location are unknown.

List data structures /** List structures **/ typedef struct list_type { node_t *head; /* Pointer to front of list */ node_t *tail; /* Pointer to end of list */ } list_t; There is a single instance of the list_t structure for each list. We will use two lists in the ray tracer: one for visible scene objects and one for lights.

list_t Functions newList() – a function used to create a new list. In a true O-O language, each class has a constructor method that is automatically invoked when a new instance of the class is created. The newList() function serves this role here. Its mission is to: 1 - malloc() a new list_t structure. 2 - set the head and tail elements of the structure to NULL. 3 - return a pointer to the list_t to the caller. list_t *newList() { }

list_t Functions l_add() - must add the element pointed to by objPtr to the list structure pointed to by list. Its mission is to: 1.malloc() a new instance of node_t, 2.add it to the end of the list, 3.ensure the next pointer of the new node is NULL and 4.ensure the next pointer of the node_t that used to be at the end of the list points to the new node_t Two cases must be distinguished: 1 - the list is empty (list-> head == NULL) 2 - the list is not empty (list-> head != NULL) void l_add(list_t *list, void *objPtr) { }

Processing a list – the iterator_t object The list_t functions described above provide a means to add nodes to the list, but there are no functions to process the nodes of a list (i.e. retrieve the nodes). Both Java and C++ provide another class known as the iterator class that is associated with a list and can be used to sequentially retrieve elements of a list. We’ll develop our own version of an iterator.

Processing a list – the iterator_t object /** Iterator **/ typedef struct list_iterator { list_t *list; /* List iterator is associated with */ node_t *position; /* Current position in list */ } Iterator_t; The “list” field points to the list_t the iterator is associated with, and “position” points to the node that would be retrieved next.

list_t Functions iterator_t *newIterator(list_t *list); l_begin() – sets the position pointer to the head pointer. If the list is empty this will cause the position pointer to be set to NULL. /* Reset position to front of list */ void l_begin(iterator_t *iter) { }

list_t Functions l_next() – returns the address of the data pointed to by the node to which the position pointer points. The function should advance the position pointer so that it points to the next node in the list. If the position pointer is presently pointing at the last node in the list, then this call will and should set the current pointer to NULL. void *l_next(iterator_t *iter) { } The l_hasnext() function should be called BEFORE l_next() is invoked to make sure the current pointer points to a valid node! The call to assert() will abort the program if l_next() is invoked in an improper state.

list_t Functions l_hasnext() – tests for the end of the list. Returns 1 if not at the end of the list; otherwise returns 0. int l_hasnext(iterator_t *iter) { } The l_hasnext() function should be called BEFORE l_next() is invoked to make sure the current pointer points to a valid node!