Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined at run-time.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Pointers.
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Queues and Linked Lists
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
CS201: Data Structures and Discrete Mathematics I Linked Lists, Stacks and Queues.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Data Structure HKOI training /4/2010 So Pak Yeung.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
CSCI2100B Linked List Jeffrey
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.
Module R2 Overview. Process queues As processes enter the system and transition from state to state, they are stored queues. There may be many different.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
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 Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
Linked Data Structures
CSE 1342 Programming Concepts
Elementary Data Structures
Cpt S 122 – Data Structures Abstract Data Types
Chapter 4 The easy stuff.
Data Structure Dr. Mohamed Khafagy.
Exercise 1 From Lec80b-Ponter.ppt.
Data Structures and Algorithms
Queue data structure.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CSC172 Data Structures Linked Lists (C version)
Stacks Stack: restricted variant of list
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Linked List (Part I) Data structure.
ADT list.
Data Structures and Algorithms
Containers: Queue and List
Queues Definition of a Queue Examples of Queues
CSCS-200 Data Structure and Algorithms
Abstract Data Types Stacks CSCI 240
Stacks and Linked Lists
Presentation transcript:

Linked Lists

Dynamic Data Structure Applications –where amount of required memory is determined at run-time.

Definition & Examples A list is a finite sequence of zero or more elements. –{a 1,a 2,..., a n } E.g. the list of prime numbers less than 20 –{2,3,5,7,11,13,17,19} A line of text of individual characters –{h,e,l,l,o,,w,o,r,l,d} A list of lists! –{{1,2,, e,g,g,s},{1,, s,a,l,a,m,i},{1,k,g,r,,o,f,,to,m,a,t,o,e,s}} a1 { a2 { a3 {

List Operations Print Length Insert Remove Lookup Other, e.g. list cloning, sub-list. Error Checking } “Running” through the list. Enumeration

Representation a1 a2 a3 a4 a14 datumpointer a27a310a to next element

Implementation Node { Handle datum, Handle pointerToNextElement} e.g. struct Node {int myDatum, struct Node *next}; or class Node {int myDatum, Node next}; 1st Head of listTail of list List Handle

Accessing the Data Structure List Handle setData(), getData(), setNext(), getNext() E.g. void setData(struct Node *whichNode, int value); Or Node.setData(int); Printing the list Handle current = head; While (notAtEndOfList(current)) { Print(current); Current = getNext(current); } headcurrent a1 a2 a3 a4

Access the list void printTriangleList(TriListEl *list) { TriListEl *x; for (x=list;x;x=x->next) printf("->%d\n",x->datum); /* datum can be a data structure then printTriangle (x->datum)*/ }

Number of elements int triangleListLen(TriListEl *list) { TriListEl *x; int result = 0; for (x=list;x;x=x->next) result++; return result; }

Insert a1 a2 a3 a4 aNaN aNaN at position 2 a1 a2 a3 a4 aNaN aNaN current aNaN + 1.create new node 2.find handle to new position 3.update handles 4.return

Insert // New node aN = new Node(theNewValue); // Handle to position current = head; loop until current indicates the target position // Update handles aN->next = current->next; current->next = aN; a1 a2 a3 a4 aNaN aNaN a1 a2 a3 a4 aNaN aNaN current aNaN +

TriListEl * addToTriangleList(TriListEl *list, int datum) { TriListEl *x, *result, *current, *newEl; newEl = (TriListEl *) malloc(sizeof(TriListEl)); newEl->datum = datum; newEl->next = (TriListEl *) NULL; if (list == (TriListEl *) NULL) { result = newEl; result->next = (TriListEl *) NULL; } else { for (x=list;x;x=x->next) { if (x->next == (TriListEl *) NULL) break; } current = x; current->next = newEl; current = newEl; current->next = (TriListEl *) NULL; result = list; } return result; }

for (x=list;x;x=x->next) { if (x->next == (TriListEl *) NULL) break; } current = x; current->next = newEl; current = newEl; current->next = (TriListEl *) NULL; result = list;

Remove 1.find handle to removal position 2.update handles 3.free memory free(current); 4.return a5a5 a5a5 a1 a2 a3 a4 current a5a5 a5a5 a1 a2 a3 a4 current Don’t forget

Lookup a1 a2 a3 a4 current a1 a2 a3 a4 current a1 a2 a3 a4 current Enumerate through list { if (current equals target) return current; } return NOT_FOUND; Return Value index, handle, Boolean, numeric

list_el * getByKey(list_el *list, char *mykey) { list_el *x; for (x=list;x;x=x->next) { if (strcmp(x->key,mykey)==0) return x; } return (list_el *) NULL; }

List of Data Structures Insertion Removal ?

Sub - List current May be composed of simpler operations

typedef struct Triangle { Point p1,p2,p3; } Triangle; typedef struct List { Triangle datum; struct List *next; } List;

Structural Variants Doubly Linked List Cyclic List List of Lists a2 a3 a4 a5a5 a5a5 a1 a6a6 a6a6 list handle

List of lists typedef struct TriListEl { int datum; struct TriListEl *next; } TriListEl; typedef struct ListList { TriListEl *datum; struct ListList *next; } ListList;

void printCardinalities(ListList *list) { ListList *x; int cnt = 1; for (x=list;x;x=x->next) { printf(“%d ",triListLen(x->datum)); cnt++; } printf("\n all elems = %d\n“,cnt); }

void freeListList(ListList *list) { ListList *x, *prev; prev = (ListList *) NULL; for (x=list;x;x=x->next) { if (prev != (ListList *) NULL) free(prev); freeTriangleList(x->datum); prev = x; } if (prev != (ListList *) NULL) free(prev); }

void freeListList(ListList *list) { ListList *x, *prev; prev = (ListList *) NULL; for (x=list;x;x=x->next) { if (prev != (ListList *) NULL) free(prev); freeTriangleList(x->datum); prev = x; } if (prev != (ListList *) NULL) free(prev); }

Abstraction Abstraction of Representation w.r.t. Stored Data Type Code Reusability Implementation –Pointer Abstraction C –Object abstraction Java –Templates C++

Algorithmic Variants Push / Pop Stack Enqueue, Dequeue Queues –FILO (stack) –FIFO (priority queue)

How to remember the length? struct ListHolder { int numberOfElements /* private, init=-1*/ ListElement *head; } int lengthList(struct ListHolder *) addElement, deleteElement must update numberOfElements