Linked Lists.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Stacks, Queues, and Linked Lists
Data Structures ADT List
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.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
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,
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Prepared By Ms.R.K.Dharme Head Computer Department.
Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined at run-time.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
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.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Linked Data Structures
Memory Management.
Lecture 6 of Computer Science II
Elementary Data Structures
Review Array Array Elements Accessing array elements
COMP 53 – Week Eight Linked Lists.
Cpt S 122 – Data Structures Abstract Data Types
Popping Items Off a Stack Using a Function Lesson xx
Pointers and Linked Lists
Chapter 4 The easy stuff.
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
Top 50 Data Structures Interview Questions
Lectures linked lists Chapter 6 of textbook
Data Structure and Algorithms
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Stacks.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
CMSC 341 Lecture 5 Stacks, Queues
Circular Buffers, Linked Lists
Queues.
Popping Items Off a Stack Lesson xx
Lesson Objectives Aims
Chapter 8: Data Abstractions
Linked Lists.
Dynamic Data Structures and Generics
Data Structures and Algorithms
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
More Data Structures (Part 1)
Data Structures & Algorithms
Data Structures & Algorithms
Dynamic allocation (continued)
CS210- Lecture 6 Jun 13, 2005 Announcements
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Abstract Data Types Stacks CSCI 240
Stacks and Linked Lists
Presentation transcript:

Linked Lists

Linked Lists Dynamic Data Structure Applications where amount of required memory is determined at run-time. The linked list is a dynamic data structure, where items may be added to it or deleted from it at will. Dynamic data structures are found of use in applications where the amount of memory required for the successful completion of the implemented task, is determined at run-time. A very common source of problems in program maintenance is the need to increase the capacity of a program to handle larger collections: even the most generous allowance for growth tends to prove inadequate over time!

{ { { Definition & Examples A list is a finite sequence of zero or more elements. {a1,a2, ..., an} 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}} Conceptually a list is defined as a finite sequence if zero or more elements and suitably denoted. For example, the list of prime numbers less than 20, is a 8 element list, or the list of observed unicorns today, a zero element list. User inputted strings can be encoded in lists of individual characters. Furthermore, a list of lists may be defined, such a list of names or other character sequences. a1 { a2 { a3 {

} “Running” through the list. Enumeration 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 datum pointer a1 4 a2 7 a3 10 a4 1 2 3 4 5 to next element A conceptual of the computer representation of list data structure is illustrated by the top figure. As seen part of the data structure is used for the representation of the actual list elements (viewed in orange), while the rest represents the order of the sequence (viewed in violet). Finally, a reserved token is used to signal the end of elements, or else the end of the list. The physical interpretation of the above conceptual representation, in the memory of a computer, is illustrated by the figure below. The orange memory block are dedicated in storage of the list distinct information elements, or otherwise the stored data. The violet memory blocks store a cue, pointing to the memory location where the next list element can be found. Typically, the strategy adopted, is the use of the memory address of the next list element. This way the trail of elements may be followed from first to last. The end of the list is signified by the use of a non-valid memory address, and specifically the null pointer. a1 4 a2 7 a3 10 a4 1 2 3 4 5 6 7 8 9 10 11 12

Implementation List Handle Node { Handle datum, Handle pointerToNextElement} e.g. struct Node {int myDatum, struct Node *next}; or class Node {int myDatum, Node next}; Head of list Tail of list As implicitly described in the previous transparency, a list is composed by simpler, static, data structures, referred to as list nodes, each one storing a list element and a memory address. In a generic programming language the definition of the list node is given by a structure containing the variable handles for the stored variable, and the memory address, respectively. In order for a program to refer to the list a way to refer to the first element is required. Afterwards, by following the pointer trail, access to every list element is possible. A pointer pointing to the first element is employed for this task, referred to as list handle. Due to its outstanding significance, the first list element is individually characterised as list head, while the rest of the list as list tail. 1st 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); } a1 a2 a3 a4 As noted the list is accessed, as any other data structure, through its handle. The data stored at the list nodes are accessed through their respective handles stored at each list node. List content and sequence is defined by the nodes variable values, which may be set or retrieved using the list element or pointer values. The management of these variable is structured by the use of corresponding functions. For example… head current

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 + create new node find handle to new position update handles at position 2 current + create new node find handle to new position update handles return

Insert + aN a1 a2 a3 a4 aN a1 a2 a3 a4 aN // 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; + aN current a1 a2 a3 a4 aN a1 a2 a3 a4 aN

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 find handle to removal position update handles free memory current Don’t forget find handle to removal position update handles free memory free(current); return

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

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 a1 a2 list handle 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; }

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; }

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) A Stack ADT allows for the retrieval of data items in the reverse order of their storage. A stack is a First-In-Last-Out (FILO) data structure, meaning the first data item added to a stack is the last item that can be retrieved from the stack.

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