Linked Lists in C and C++ CS-2303, C-Term 20101 Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.

Slides:



Advertisements
Similar presentations
Information Security of Embedded Systems : Design of Secure Systems Prof. Dr. Holger Schlingloff Institut für Informatik und Fraunhofer FIRST.
Advertisements

Under the Hood of Polymorphism CS-2303, C-Term Under the Hood of Polymorphism CS-2303 System Programming Concepts (Slides include materials from.
Pointers.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Stacks, Queues, and Linked Lists
Linked Lists.
Linear Lists – Linked List Representation
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
A useful testing technique and more…
Hash Tables and Constant Access Time CS-2303, C-Term Hash Tables and Constant Access Time CS-2303 System Programming Concepts (Slides include materials.
Tutorial 1 Creating a Database
Data Structure Lecture-5
Collaboration Works! 10/20/20101 Planning Research Institutional Effectiveness.
Quick Training Guide New SpringerLink, August 2010.
Tutorial 8 Sharing, Integrating, and Analyzing Data
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) CS-2303 System Programming Concepts (Slides include materials from The C Programming.
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).
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.
Lists and Trees (continued) CS-2301, B-Term Lists and Trees (continued) CS-2301, System Programming for Non-Majors (Slides include materials from.
More on Data Structures in C CS-2301 B-term More on Lists and Trees Introduction to Hash Tables CS-2301, System Programming for Non-majors (Slides.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Hash TablesCS-2301, B-Term Hash Tables and Constant Access Time CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming.
More on Data Structures in C CS-2301 D-term More on Data Structures in C CS-2301 System Programming D-term 2009 (Slides include materials from The.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
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 — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
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.
Subject Name : Data Structure Using C Title : Linked Lists
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Introduction to Linked Lists
Data Structure Interview Question and Answers
Data Structures and Algorithms
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
LINKED LISTS CSCD Linked Lists.
Introduction to Linked Lists
Programming Assignment #4 Binary Trees in C++
Review & Lab assignments
Data Structures and Algorithms
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Introduction to C++ Linear Linked Lists
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
BY PROF. IRSHAD AHMAD LONE.
Linked Lists.
Presentation transcript:

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 Language, 2 nd edition, by Kernighan and Ritchie and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel)

Linked Lists in C and C++ CS-2303, C-Term Common Data Structures in C and C++ Linked lists – D&D §12.4–12.6 (not in K&R) One-way Doubly-linked Circular Trees – D&D §12.7, K&R §6.5 Binary Multiple branches Hash Tables – K&R §6.6 (not in D&D) Combine arrays and linked list Especially for searching for objects by value

Linked Lists in C and C++ CS-2303, C-Term Definitions 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 Singly- or doubly-linked Stack, queue, circular list Tree A data structure in which each element is dynamically allocated and in which each element has more than one potential successor Defines a partial order Note: elements are usually the same type (but not always).

Linked Lists in C and C++ CS-2303, C-Term Linked List struct listItem { type payload; struct listItem *next; }; payload next payload next payload next payload next Note: payload may be multiple members.

Linked Lists in C and C++ CS-2303, C-Term Linked List (continued) Items of list are usually same type Generally obtained from malloc() Each item points to next item Last item points to null Need head to point to first item! Payload of item may be almost anything A single member or multiple members Any type of object whose size is known at compile time Including struct, union, char * or other pointers Also arrays of fixed size at compile time (see p. 214)

Linked Lists in C and C++ CS-2303, C-Term Usage of Linked Lists Not massive amounts of data Linear search is okay Sorting not necessary or sometimes not possible Need to add and delete data on the fly Even from middle of list Items often need to be added to or deleted from the ends

Linked Lists in C and C++ CS-2303, C-Term Linked List (continued) struct listItem { type payload; struct listItem *next; }; struct listItem *head; payload next payload next payload next payload next

Linked Lists in C and C++ CS-2303, C-Term 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

Linked Lists in C and C++ CS-2303, C-Term 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

Linked Lists in C and C++ CS-2303, C-Term 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

Linked Lists in C and C++ CS-2303, C-Term 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 to do if we cannot guarantee that p and q are non-NULL?

Linked Lists in C and C++ CS-2303, C-Term 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

Linked Lists in C and C++ CS-2303, C-Term 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

Linked Lists in C and C++ CS-2303, C-Term 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 This is needed in PA#3 –Insert item after earlier event times and before later ones –Need to search the list

Linked Lists in C and C++ CS-2303, C-Term Doubly-Linked List struct listItem { type payload; listItem *prev; listItem *next; }; struct listItem *head, *tail; prevnext payload prevnext payload prevnext payload prevnext payload In-class exercise:– how to add a new item q after a list item p

Linked Lists in C and C++ CS-2303, C-Term Other Kinds of List Structures Queue FIFO (First In, First Out) Items added at end Items removed from beginning Stack LIFO (Last In, First Out) Items added at beginning, removed from beginning Circular list Last item points to first item Head may point to first or last item Items added to end, removed from beginning

Linked Lists in C and C++ CS-2303, C-Term Circular List listItem *addAfter (listItem *p, listItem *tail){ if (p && tail) { p -> next = tail -> next; tail = p; } else if (p) { tail p -> next = p; } return tail; } payload next payload next payload next payload next struct listItem *tail; Optional:– struct listItem *head;

Linked Lists in C and C++ CS-2303, C-Term Questions?