1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Chapter 17 vector and Free Store Bjarne Stroustrup
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Chapter 3: Linked List Tutor: Angie Hui
Business Transaction Management Software for Application Coordination 1 Business Processes and Coordination.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
ZMQS ZMQS
BT Wholesale October Creating your own telephone network WHOLESALE CALLS LINE ASSOCIATED.
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
Report Card P Only 4 files are exported in SAMS, but there are at least 7 tables could be exported in WebSAMS. Report Card P contains 4 functions: Extract,
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Linked Lists.
Linked Lists Chapter 4.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
ITEC200 Week04 Lists and the Collection Interface.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Data Structures Using C++
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
LIST PROCESSING.
John Hurley Cal State LA
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
ABC Technology Project
CSCI2100B Linked List Jeffrey
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Case Study: Focus on Structures Math 130 Lecture 21 B Smith: 10/04: Required 35 minutes to complete. 15 minutes was spent returning test 2 and completing.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Review Pseudo Code Basic elements of Pseudo code
Progam.-(6)* Write a program to Display series of Leaner, Even and odd using by LOOP command and Direct Offset address. Design by : sir Masood.
Linking Verb? Action Verb or. Question 1 Define the term: action verb.
Squares and Square Root WALK. Solve each problem REVIEW:
© 2012 National Heart Foundation of Australia. Slide 2.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
GG Consulting, LLC I-SUITE. Source: TEA SHARS Frequently asked questions 2.
Addition 1’s to 20.
25 seconds left…...
Chapter 9: Data Structures I
Week 1.
We will resume in: 25 Minutes.
1 Unit 1 Kinematics Chapter 1 Day
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Data Structures Using C++ 2E
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.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
Presentation transcript:

1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: This required 2 classes. 1 st class covered linked- lists extempo, 2 nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2 nd lecture for covering malloc/free B Smith: This required 2 classes. 1 st class covered linked- lists extempo, 2 nd class discussed malloc() and free(). For Fall05, use this for lecture-1, and develop a 2 nd lecture for covering malloc/free

2

3

4 SWC Team

5 Administrivia Mindstorms Lab 10 (due Friday: 04/15/05) Ex 10.2, pbm 4 Ex 10.3, pbm 2

6 Overview Linked Lists Lists vs Arrays List Insertion List Deletion

7 Recall Arrays An array is a grouped list of two or more variables of the same type Array elements are contiguous. Subscripts can be any integer expression Each array element is a separate variable grades[0] grades[1] grades[2] grades[3] grades:

8 Linked-List linked-list of grades: array of grades: The data could be held in a struct containing an integer and a pointer The data here is held in an array of integers What is the data type of the pointer? The pointer is defined such that it points to another node. The node it points to is the same type of struct in which it resides.

9 Linked-List Definition Definition: A linked list is a set of items where each item is a part of a node that also contains a link to a node. It is a sequence of nodes, each containing data field(s) (the Item ) one (or more) links to the next node; a reference N EMO node link item

10 End of a Linked List The link in the final node can be represented a number of ways As a null link that points to no node A reference to a dummy node that contains no item A reference back to the first node, making the list a circular list. N EMO N EMO N EMO B Smith: 4/8/05 Fri: Stopped here and worked at the board to define the nodes. B Smith: 4/8/05 Fri: Stopped here and worked at the board to define the nodes.

11 Linked List Code struct node { int item; struct node* next; }; struct node { int item; struct node* next; }; struct node { int item; link next; }; typedef struct node* link; struct node { int item; link next; }; typedef struct node* link; B Smith: Stopped here on 11/2/05. Rate: 3. Used a physical paper link and chairs to simulate linked list. B Smith: Stopped here on 11/2/05. Rate: 3. Used a physical paper link and chairs to simulate linked list. Alternatively, the node could be represented as shown below

12 Telephone List What data structure would you use to organize the following list? What challenges do you see if we want to add Hagar, June to the list if organized as an array of structures? Acme, Sam (555) Dolan, Edith (555) Lanfrank, John (555) Mening, Stephanie (555) Zemann, Harold (555)

13 Simple Linked List Example-Telephone List First member is the individuals name Second member is the telephone number Last member is the address of the next item struct TeleType { char name[30]; char phoneNum[15]; struct TeleType *nextaddr; };}; TeleType name[] phoneNum[] nextaddr* Acme, Sam (555) t1 &t2

14 Linking The Data Using Addresses Acme, Sam (555) t1 Dolan, Edith (555) t2 Lanfrank, Jo (555) t3 NULL B Smith: This example was used to show them how to walk through a list with a while loop. Elaborate in Sp05 on walking; more diagrams, pix. B Smith: This example was used to show them how to walk through a list with a while loop. Elaborate in Sp05 on walking; more diagrams, pix.

15 List Implementation #include struct TeleType { char name[30]; char phoneNum[15]; struct TeleType *nextaddr; }; int main() { struct TeleType t1 = {"Acme, Sam","(555) "}; /* an instance */ struct TeleType t2 = {"Dolan, Edith","(555) "}; struct TeleType t3 = {"Lanfrank, John","(555) "}; struct TeleType *first; /* create a pointer to a structure */ first = &t1; /* store t1's address in first */ t1.nextaddr = &t2; /* store t2's address in t1.nextaddr */ t2.nextaddr = &t3; /* store t3's address in t2.nextaddr */ t3.nextaddr = NULL; /* store the NULL address in t3.nextaddr */ printf("\n%s \n%s \n%s",first->name,t1.nextaddr->name,t2.nextaddr->name); return 0; }

16 Record Insertion Linked lists are especially useful when a record needs to be inserted How would you grow a predefined array? To insert the record for June Hagar into the list, what steps must be taken?

17 List Item Insertion Acme, Sam (555) t1 Dolan, Edith (555) Lanfrank, Jo (555) NULL The next field must be changed to point to Hagar Hagar, June (555) This node must point to Lanfrank. B Smith: Stopped here on 11/1/04 B Smith: Stopped here on 11/1/04

18 NULL - End of List The NULL pointer is frequently used as a sentinel to mark the end-of-string NULL. both serve similar purposes

19 List Item Deletion What might be the steps for removing a record from a linked list?

20 Remove Dolan, Edith Acme, Sam (555) t1 Dolan, Edith (555) Lanfrank, Jo (555) NULL The next field must point to Lanfrank t1.next = t1.next->next; t1.next = t2.next;

21 Summary Linked lists help us manage constantly changing lists of data help us with insertion and deletion of existing records is a basic data structure where each item contains information on how to get to the next item is a set of items where each item is part of a node that also contains a link to a node