(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
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.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
(1 - 2) Abstract Data Types D & D Chapter 12 Instructor - Andrew S. O’Fallon CptS 122 (June 10, 2015) Washington State University.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Introduction to Data Structures Systems Programming.
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
(1 - 1) Introduction to C Data Structures & Abstract Data Types Instructor - Andrew S. O’Fallon CptS 122 (August 26, 2015) Washington State University.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
Introduction to Data Structures Systems Programming Concepts.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
Programming Practice 3 - Dynamic Data Structure
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
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.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Introduction to Classes in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Operator Overloading D & D Chapter 10 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 12 – Data Structures
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
UNIT – I Linked Lists.
12 C Data Structures.
Data Structures and Algorithms
Stacks and Queues.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Introduction to Data Structures
(2-1) Data Structures & The Basics of a Linked List I
Linked List Sudeshna Sarkar.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
(5 - 1) Object-Oriented Programming (OOP) and C++
(2-1) Data Structures & The Basics of a Linked List I
Chapter 16-2 Linked Structures
18.5 Linked Queues Like a stack, a queue can be implemented using pointers and nodes Allows dynamic sizing, avoids issue of wrapping indices NULL front.
Review & Lab assignments
Data Structures and Algorithms
(5 - 1) Object-Oriented Programming (OOP) and C++
ECE 103 Engineering Programming Chapter 63 Queue Implementation
General List.
(1 - 2) Introduction to C Data Structures & Abstract Data Types
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
(4 – 2) Introduction to Classes in C++
Module 13 Dynamic Memory.
Presentation transcript:

(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University

A. O’Fallon, J. Hagemeister 2 Definition of Linked List A finite sequence of nodes, where each node may be only accessed sequentially (through links or pointers), starting from the first node It is also defined as a linear collection of self- referential structures connected by pointers

A. O’Fallon, J. Hagemeister 3 Conventions An uppercase first character of a function name indicates that we are referencing the List ADT operation A lowercase first character of a function indicates our implementation

A. O’Fallon, J. Hagemeister 4 Struct Node For these examples, we’ll use the following definition for Node: typedef struct node { char data; // self-referential struct node *pNext; } Node;

A. O’Fallon, J. Hagemeister 5 Initializing a List (1) InitList (L) Procedure to initialize the list L to empty Our implementation: void initList (Node **pList) { // Recall: we must dereference a // pointer to retain changes *pList = NULL; }

A. O’Fallon, J. Hagemeister 6 Initializing a List (2) The initList() function is elementary and is not always implemented We may instead initialize the pointer to the start of the list with NULL within main() int main (void) { Node *pList = NULL; … }

A. O’Fallon, J. Hagemeister 7 Checking for Empty List (1) ListIsEmpty (L) -> b: Boolean function to return TRUE if L is empty Our implementation: int isEmpty (Node *pList) { int status = 0; // False initially if (pList == NULL) // The list is empty { status = 1; // True } return status; }

A. O’Fallon, J. Hagemeister 8 Checking for Empty List (2) Note: we could substitute the int return type with an enumerated type such as Boolean typedef enum boolean { FALSE, TRUE } Boolean;

A. O’Fallon, J. Hagemeister 9 Checking for Empty List (3) Our implementation with Boolean defined: Boolean isEmpty (Node *pList) { Boolean status = FALSE; if (pList == NULL) { status = TRUE; } return status; }

A. O’Fallon, J. Hagemeister 10 Printing Data in List (1) Our implementation: void printListIterative (Node *pList) { printf (“X -> “); while (pList != NULL) { printf (“%c -> “, pList -> data); // Get to the next item pList = pList -> pNext; } printf (“NULL\n”); }

A. O’Fallon, J. Hagemeister 11 Printing Data in List (2) Another possible implementation using isEmpty(): void printListIterative (Node *pList) { printf (“X -> “); while (!isEmpty (pList)) { printf (“%c -> “, pList -> data); // Get to the next item pList = pList -> pNext; } printf (“NULL\n”); }

A. O’Fallon, J. Hagemeister 12 Printing Data in List (3) We can determine the end of the list by searching for the NULL pointer If the list is initially empty, no problem, the while() loop will not execute

A. O’Fallon, J. Hagemeister 13 Inserting Data at Front of List InsertFront (L,e): Procedure to insert a node with information e into L as the first node in the List; in case L is empty, make a node containing e the only node in L and the current node

A. O’Fallon, J. Hagemeister 14 Inserting Data at Front of List w/o Error Checking (1) Our implementation: void insertFront (Node **pList, char newData) { Node *pMem = NULL; pMem = (Node *) malloc (sizeof (Node)); // Initialize the dynamic memory pMem -> data = newData; pMem -> pNext = NULL; // Insert the new node into front of list pMem -> pNext = *pList; *pList = pMem; }

A. O’Fallon, J. Hagemeister 15 Inserting Data at Front of List w/o Error Checking (2) Let’s define a new function which handles the dynamic allocation and initialization of a node: Node * makeNode (char newData) { Node *pMem = NULL; pMem = (Node *) malloc (sizeof (Node)); // Initialize the dynamic memory pMem -> data = newData; pMem -> pNext = NULL; return pMem; }

A. O’Fallon, J. Hagemeister 16 Inserting Data at Front of List w/o Error Checking (3) Now we can reorganize our code and take advantage of the new function: void insertFront (Node **pList, char newData) { Node *pMem = NULL; pMem = makeNode (newData); // Insert the new node into front of list pMem -> pNext = *pList; *pList = pMem; }

A. O’Fallon, J. Hagemeister 17 Inserting Data at Front of List w/ Error Checking (1) Let’s modify our code so that we can check for dynamic memory allocation errors We’ll start with makeNode(): Node * makeNode (char newData) { Node *pMem = NULL; pMem = (Node *) malloc (sizeof (Node)); if (pMem != NULL) { // Initialize the dynamic memory pMem -> data = newData; pMem -> pNext = NULL; } // Otherwise no memory is available; could use else, but // it’s not necessary return pMem; }

A. O’Fallon, J. Hagemeister 18 Inserting Data at Front of List w/ Error Checking (2) Now let’s add some error checking to insertFront(): void insertFront (Node **pList, char newData) { Node *pMem = NULL; pMem = makeNode (newData); if (pMem != NULL) // Memory was available { // Insert the new node into front of list pMem -> pNext = *pList; *pList = pMem; } else // Can’t allocate anymore dynamic memory { printf (“WARNING: No memory is available for data insertion!\n”) }

A. O’Fallon, J. Hagemeister 19 Closing Thoughts Can you build a driver program to test these functions? Is it possible to return a Boolean for insertFront() to indicate a memory allocation error, where TRUE means error and FALSE means no error? insertFront() will be seen again with a Stack data structure…

A. O’Fallon, J. Hagemeister 20 Next Lecture… Continue our discussion and implementation of linked lists

A. O’Fallon, J. Hagemeister 21 References P.J. Deitel & H.M. Deitel, C: How to Program (7th ed.), Prentice Hall, 2013 J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (7 th Ed.), Addison- Wesley, 2013

A. O’Fallon, J. Hagemeister 22 Collaborators Jack Hagemeister