Linked Lists in C and C++

Similar documents
Binary Trees (and Big O notation)

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Programming Assignment #4 Binary Trees in C++

Containers and the Standard Template Library (STL)

Iterators. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Structures, Unions, and Typedefs

Procedural Programming & Fundamentals of Programming

Accessing Files in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Programming. Lists, Stacks, Queues

Arrays and Pointers in C & C++

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Classes, Constructors, etc., in C++

Introduction to Linked Lists

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS 241 Data Organization Binary Trees

Your first C and C++ programs

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Strings in C. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Algorithms, Data Structures, and Problem Solving

Ashish Gupta, Data JUET, Guna

Linked Lists

1 P age DS & OOPS / UNIT II

Derived Classes in C++

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CS32 Discussion Week 3

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Information Science 2

Solution for Data Structure

CS301 - Data Structures Glossary By

Linked List. April 2, 2007 Programming and Data Structure 1

cs Java: lecture #6

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Exception Handling in C++

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

ECE 2035 Programming HW/SW Systems Spring problems, 5 pages Exam Three 8 April Your Name (please print clearly)

A Deeper Look at Classes

Dynamic Data Structures

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Introduction to Programming in C Department of Computer Science and Engineering

C Multiple Choice Questions and answers MCQ with Ans.

CP2 Revision. theme: dynamic datatypes & data structures

Programming and Data Structure Solved. MCQs- Part 2

Singly linked lists in C.

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

III Data Structures. Dynamic sets

Logistics. Half of the people will be in Wu & Chen The other half in DRLB A1 Will post past midterms tonight (expect similar types of questions)

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Advanced C Programming and Introduction to Data Structures

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CS350 - Exam 1 (100 Points)

3. Fundamental Data Structures

Project 4: Implementing Malloc Introduction & Problem statement

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

DATA STRUCTURE UNIT I

Linked Lists and Abstract Data Structures A brief comparison

+ Abstract Data Types

Templates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts

Programming in C. Lecture Tiina Niklander. Faculty of Science

CS 231 Data Structures and Algorithms Fall DDL & Queue Lecture 20 October 22, Prof. Zadia Codabux

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

Linked Lists Structures

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Bharati Vidyapeeth s College Of Engineering for Women Pune-43 Department E & TC. SE- Unit Test I Subject-DS

15-213/18-213/15-513, Spring 2018 C Programming Lab: Assessing Your C Programming Skills

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

PROGRAMMAZIONE I A.A. 2017/2018

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Data Structure with C. List

The time and space are the two measure for efficiency of an algorithm.

Computer Science 9608 (Notes) Chapter: 4.1 Computational thinking and problem-solving

811312A Data Structures and Algorithms, , Exercise 1 Solutions

15-122: Principles of Imperative Computation, Spring Due: Thursday, March 10, 2016 by 22:00

1 Deletion in singly linked lists (cont d) 1 Other Functions. 1 Doubly Linked Lists. 1 Circular lists. 1 Linked lists vs. arrays

Data Structure Series

Fundamentals of Programming

CS PROGRAMMING & ATA STRUCTURES I. UNIT I Part - A

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Basic Data Structures (Version 7) Name:

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

However, in C we can group related variables together into something called a struct.

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

Jump Tables A jump table is essentially a list of places in the code to jump to. This can be thought of in C as an array of function pointers. In Asse

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

a) State the need of data structure. Write the operations performed using data structures.

Short Notes of CS201

! Mon, May 5, 2:00PM to 4:30PM. ! Closed book, closed notes, clean desk. ! Comprehensive (covers entire course) ! 30% of your final grade

5 Phases of Software Life Cycle

Cpt S 122 Data Structures. Data Structures

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

15-213/18-213/15-513, Fall 2017 C Programming Lab: Assessing Your C Programming Skills

Data Structure. Recitation III

Transcription:

Linked Lists in C and C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter Savitch, The C++ Programming Language, Special Edition, by Bjarne Stroustrup, and from C: How to Program, 5 th and 6 th editions, by Deitel and Deitel) CS-2303, A-Term 2010 Linked Lists 1

Common Data Structures in C and C++ Linked lists Nothing specific in K&R One-way Doubly-linked Circular Trees K&R 6.5 Binary Multiple branches Hash Tables K&R 6.6 Combine arrays and linked list Especially for searching for objects by value 2

Definitions Linked List Tree 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 Variations: stack, queue, circular list Note: elements are usually the same type (but not always). 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 3

Linked List struct listitem { type ; struct listitem *; }; 4

Linked List (continued) Items of list are usually same type Generally obtained from malloc() Each item points to 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) 5

Usage of Linked Lists Not massive numbers of items Linear search is okay Sorting not usually 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 6

Linked List (continued) struct listitem { type ; struct listitem *; }; struct listitem *head; 7

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 8

Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 9

Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 10

Adding an Item to a List Question: What to do if we cannot guarantee that p and q are non-null? listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 11

Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ if (p && q) { q -> = p -> ; p -> = q; } } return p; 12

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

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 14

Doubly-Linked List struct listitem { type ; listitem *prev; listitem *; }; struct listitem *head, *tail; prev prev prev prev 15

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 16

Circular List Optional: struct listitem *head; struct listitem *tail; listitem *addtotail (listitem *p, listitem *tail){ if (p && tail) { p -> = tail -> ; tail = tail -> = p; } else if (p) { tail = p -> = p; } return tail; } 17

Questions? 18