Linked Lists.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
COMP171 Fall 2005 Lists.
Linked Lists.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
Linked List
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
Linked Lists 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.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
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.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
UNIT-II Topics to be covered Singly linked list Circular linked list
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
Programming Circular Linked List.
Chapter 12 – Data Structures
Introduction to Linked Lists
Sorted Linked List Same objective as a linked list, but it should be sorted Sorting can be custom according to the type of nodes Offers speedups over non-sorted.
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
List ADT & Linked Lists.
Chapter 4 Linked Lists.
Linked lists.
Linked Lists Damian Gordon.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCI 3333 Data Structures Linked Lists.
Data Structures and Algorithms IT12112
Sequences 9/18/ :20 PM C201: Linked List.
Introduction to Linked Lists
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 18: Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
Linked List.
Review & Lab assignments
Linked Lists in C and C++
Chapter 17: Linked Lists.
Lecture 14 Linked Lists CSE /26/2018.
Linked Lists.
CS148 Introduction to Programming II
Linked List Intro CSCE 121.
CS148 Introduction to Programming II
Linked lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

Linked Lists

Preliminaries Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to grow in size as needed Does not require the shifting of items during insertions and deletions

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

Linked List struct listItem { type payload; struct listItem *next; };

Linked List (continued) struct listItem { type payload; struct listItem *next; }; struct listItem *head; payload next payload next payload next payload next

DECLARING A NODE TYPE struct node{ int value; struct node *next; } struct point { double x,y; }; struct vertex { struct point element; struct vertex *next;}

Usage of Linked Lists Not massive amounts of data Linear search is okay Sorting not 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”

BUILDING LINKED LIST A node in a linked list is usually a struct struct node { int value; Node *next; }; //end struct A node is dynamically allocated node *p; p = new node; (*p).value = 10; p->value= 10; next value p p 10

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)

INSERTING A NODE AT THE BEGINNING OF THE LIST If first points to the first node of the linked list: new_node->next = head; head = new_node; struct Node *first=NULL, *new_node; head new_node new_node= new Node; head new_node

INSERTING A NODE AT THE BEGINNING OF THE LIST new_node->value=10; 10 new_node head new_node->next = head; head 10 new_node head = new_node; head 10 new_node

INSERTING A NODE AT THE BEGINNING OF THE LIST new_node = new node; head 10 new_node new_node->value = 20; head 10 20 new_node new_node->next = head; head 10 20 new_node head = new_node; head 20 new_node 10

SEARCHING A LINKED LIST for (p=head; p!=NULL; p=p->next) {… } int value=20; struct node *p; { if (p->value== value) return p; } struct node *find(struct node *list, int n){ while (list!=NULL and list->value!=n ) p=p->next; return list; } head 20 10

INSERTING A NODE AT THE MIDDLE OF THE LIST head 20 10 cur struct *node p=head; struct node * cur= find(p, 10); struct node *tmp = new node; tmp->value= cur->value; tmp->next = cur->next_ptr; 10 tmp cur->value = 15; cur->next = tmp; first 20 cur 15 first 20 cur 15

DELETING A NODE FROM THE LIST head 20 15 cur tmp 10 struct *node p=head; struct node * cur= find(p, 20); struct node *tmp; tmp = cur->next; cur->value = tmp->value; cur tmp head 15 15 10 cur->next = tmp->next; cur tmp head 15 15 10 delete tmp; head 15 10 15

Printing all the elements void DisplayList(void) Print the data of all the elements Print the number of the nodes in the list void DisplayList() { int num = 0; Node* currNode = head; while (currNode != NULL){ cout << currNode->value << endl; currNode = currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl;

Destroying the list Void DestroyList(void) Step through the list and delete each node one by one. Void DestroyList(void) { Node* currNode = head, *nextNode = NULL; while (currNode != NULL) { nextNode = currNode->next; // destroy the current node delete currNode; currNode = nextNode; }