Linked Lists.

Slides:



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

Linked Lists Geletaw S..
Chapter 3: Linked List Tutor: Angie Hui
Pointers.
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures Using C++
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
CSEB324 Data Structures & Algorithms
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Linked Lists.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked List
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
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.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
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 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
LINKED LISTS.
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 16: Linked Lists.
Unit – I Lists.
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
Lecture - 6 On Data Structures
Linked Lists.
LINKED LISTS CSCD Linked Lists.
Chapter 18: Linked Lists.
Linked List.
Review & Lab assignments
Chapter 17: Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists.
Presentation transcript:

Linked Lists

List Overview Linked lists Basic operations of linked lists Abstract data type (ADT) Basic operations of linked lists Insert, find, delete, print, etc. Variations of linked lists Circular linked lists Doubly linked lists

Linked Lists A linked list is a series of connected nodes B C  Head A linked list is a series of connected nodes Each node contains at least A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node The last node points to NULL node A data pointer

A Simple Linked List Class Linked lists are made up of Nodes Declare Node class for the nodes data: double-type data in this example next: a pointer to the next node in the list struct node { int data; struct node *next; /* pointer to next element in list */ } LLIST;

Operations on Linked List LLIST *list_add(LLIST **p, int i); void list_remove(LLIST **p); LLIST **list_search(LLIST **n, int i); void list_print(LLIST *n);

Inserting a new node Steps Insert a node with data equal to x after the index’th elements. (i.e., when index = 0, insert the node as the first element; when index = 1, insert the node after the first element, and so on) If the insertion is successful, return the inserted node. Otherwise, return NULL. (If index is < 0 or > length of the list, the insertion will fail.) Steps Locate index’th element Allocate memory for the new node Point the new node to its successor Point the new node’s predecessor to the new node index’th element newNode

Inserting a new node Possible cases of InsertNode Insert into an empty list Insert in front Insert at back Insert in middle But, in fact, only need to handle two cases Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4)

LLIST *list_add(LLIST **p, int i) { if (p == NULL) return NULL; LLIST *n = (LLIST *) malloc(sizeof(LLIST)); if (n == NULL) n->next = *p; /* the previous element (*p) now becomes the "next" element */ *p = n; /* add new empty element to the front (head) of the list */ n->data = i; return *p; }

Finding a node Search for a node with the value equal to x in the list. If such a node is found, return its position. Otherwise, return 0. LLIST **list_search(LLIST **n, int i) { if (n == NULL) return NULL; while (*n != NULL) if ((*n)->data == i) return n; } n = &(*n)->next;

Deleting a node Steps Like InsertNode, there are two special cases int DeleteNode(double x) Delete a node with the value equal to x from the list. If such a node is found, return its position. Otherwise, return 0. Steps Find the desirable node (similar to FindNode) Release the memory occupied by the found node Set the pointer of the predecessor of the found node to the successor of the found node Like InsertNode, there are two special cases Delete first node Delete the node in middle or at the end of the list

void list_remove(LLIST **p) /* remove head */ { if (p != NULL && *p != NULL) LLIST *n = *p; *p = (*p)->next; free(n); }

Printing all the elements void List_Print(void) Print the data of all the elements Print the number of the nodes in the list

void list_print(LLIST *n) { if (n == NULL) printf("list is empty\n"); } while (n != NULL) printf("print %p %p %d\n", n, n->next, n->data); n = n->next;

Using all these routines int main(void) { LLIST *n = NULL; list_add(&n, 0); /* list: 0 */ list_add(&n, 1); /* list: 1 0 */ list_add(&n, 2); /* list: 2 1 0 */ list_add(&n, 3); /* list: 3 2 1 0 */ list_add(&n, 4); /* list: 4 3 2 1 0 */ list_print(n); list_remove(&n); /* remove first (4) */ list_remove(&n->next); /* remove new second (2) */ list_remove(list_search(&n, 1)); /* remove cell containing 1 (first) */ list_remove(&n->next); /* remove second to last node (0) */ list_remove(&n); /* remove last (3) */ return 0; }

Variations of Linked Lists Circular linked lists The last node points to the first node of the list How do we know when we have finished traversing the list? (Tip: check if the pointer of the current node is equal to the head.) A B C Head

Variations of Linked Lists Doubly linked lists Each node points to not only successor but the predecessor There are two NULL: at the first and last nodes in the list Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards A B C   Head

Array versus Linked Lists Linked lists are more complex to code and manage than arrays, but they have some distinct advantages. Dynamic: a linked list can easily grow and shrink in size. We don’t need to know how many nodes will be in the list. They are created in memory as needed. In contrast, the size of a C++ array is fixed at compilation time. Easy and fast insertions and deletions To insert or delete an element in an array, we need to copy to temporary variables to make room for new elements or close the gap caused by deleted elements. With a linked list, no need to move other nodes. Only need to reset some pointers.