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
Pointers.
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.
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.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Hash Tables and Constant Access Time CS-2303, C-Term Hash Tables and Constant Access Time CS-2303 System Programming Concepts (Slides include materials.
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.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Binary TreesCS-2303, C-Term Binary Trees (and Big “O” notation) 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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Review of Exam #2CS-2301, B-Term Review of Exam #2 CS-2301, System Programming for Non-Majors (Slides include materials from The C Programming Language,
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.
More on Operator Overloading CS-2303, C-Term More on Operator Overloading CS-2303 System Programming Concepts (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.
Accessing Files in CCS-2303, C-Term Accessing Files in C CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
Structures, Unions, and Typedefs CS-2303, C-Term Structures, Unions, and Typedefs CS-2303 System Programming Concepts (Slides include materials from.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
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.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
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.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
CS261 Data Structures Linked Lists - Introduction.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© 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)
Linked Lists and Generics Written by J.J. Shepherd.
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 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++
Arrays and Linked Lists
Review & Lab assignments
Data Structures and Algorithms
Linked Lists in C and C++
Binary Trees (and Big “O” notation)
Introduction to C++ Linear Linked Lists
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 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() In computing, malloc is a subroutine for performing dynamic memory allocation in the C and C++ programming languages, though its use in C++ has been largely superseded by operators new and new[]. malloc is part of the standard library for both languages and is declared in the stdlib.h header although it is also declared within the std namespace via the C++'s cstdlib header. Programs must properly manage dynamic memory allocated through the use of malloc to avoid memory leaks and memory corruption. 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 Linked Lists in C and C++ CS-2303, C-Term A Head BC

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

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?