Implementation of Linked List For more notes and topics visit: www.eITnotes.com eITnotes.com.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Lists CS 3358.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
CHP-5 LinkedList.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
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 List Spring 2013Programming and Data Structure1.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Subject Name : Data Structure Using C Title : Linked Lists
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
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.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
CSCS-200 Data Structure and Algorithms Lecture
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
Linked List ADT used to store information in a list
Unit – I Lists.
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Linked Lists.
Linked List.
Programming and Data Structure
BY PROF. IRSHAD AHMAD LONE.
Presentation transcript:

Implementation of Linked List For more notes and topics visit: eITnotes.com

Disadvantages of Array 1.The size of the array is fixed. 2.Inserting (and deleting) elements into the middle of the array is potentially expensive because existing elements need to be shifted over to make room. 3.Wastage of memory. 4.Elements are need to be at contiguous memory locations. Before Starting eITnotes.com

Introduction A linked list can be defined as a sequence of data elements in which each elements of list must contains at least two field’s one for data item and other for storing the address of next elements (link). The combination of these two fields is known nodes. The nodes are chained together to form a linked list. eITnotes.com

Introduction… – Successive elements are connected by pointers. – Last element points to NULL. – It can grow or shrink in size during execution of a program. – It can be made just as long as required. – It does not waste memory space. A head BC HEAD AB C eITnotes.com

List is an Abstract Data Type What is an abstract data type? – It is a data type defined by the user. – Typically more complex than simple data types like int, float, etc. Why abstract? – Because details of the implementation are hidden. – When you do some operation on the list, say insert an element, you just call a function. – Details of how the list is implemented or how the insert function is written is no longer required. eITnotes.com

Types of Lists – Linear singly-linked list (or simply linear list) One we have discussed so far. HEAD eITnotes.com

– Circular linked list The pointer from the last element in the list points back to the first element. It does not contain any NULL pointer. HEAD eITnotes.com

– Doubly linked list Pointers exist between adjacent nodes in both directions. The list can be traversed either forward or backward. Usually two pointers are maintained to keep track of the list, head and tail. eITnotes.com

Overflow and Underflow Underflow: HEAD = NULL or START = NULL Overflow: AVAIL = NULL eITnotes.com

Basic Operations on a List Creating a list Traversing the list Inserting an item in the list Deleting an item from the list Concatenating two lists into one Searching an item in the list eITnotes.com

Illustration: Insertion eITnotes.com

Illustration: Deletion eITnotes.com

In essence... For insertion: – A record is created holding the new item. – The next pointer of the new record is set to link it to the item which is to follow it in the list. – The next pointer of the item which is to precede it must be modified to point to the new item. For deletion: – The next pointer of the item immediately preceding the one to be deleted is altered, and made to point to the item following the deleted item. eITnotes.com

Example: Working with linked list Consider the structure of a node as follows: struct stud { introll; char name[25]; intage; struct stud *next; }; struct Node { Object element; Node *next; }; eITnotes.com

Creating a List eITnotes.com

How to begin? To start with, we have to create a node (the first node), and make head point to it. head = (node *) malloc(sizeof(node)); head roll name age next HEAD ROLL NAME NEXT AGE eITnotes.com

Contd. If there are n number of nodes in the initial linked list: – Allocate n records, one by one. – Read in the fields of the records. – Modify the links of the records so that the chain is formed. HEAD eITnotes.com

node *create_list() { int k, n; node *p, *head; printf ("\n How many elements to enter?"); scanf ("%d", &n); for { (k=0; k<n; k++) if (k == 0) { head = (node *) malloc(sizeof(node)); p = head; } else { p->next = (node *) malloc(sizeof(node)); p = p->next; } scanf ("%d %s %d", &p->roll, p->name, &p->age); } p->next = NULL; return (head); } eITnotes.com

To be called from main() function as: node *head; ……… head = create_list(); eITnotes.com

Traversing the List eITnotes.com

What is to be done? Once the linked list has been constructed and head points to the first node of the list, – Follow the pointers. – Display the contents of the nodes as they are traversed. – Stop when the next pointer points to NULL. eITnotes.com

void display (node *head) { int count = 1; node *p; p = head; while (p != NULL) { printf ("\nNode %d: %d %s %d", count, p->roll, p->name, p->age); count++; p = p->next; } printf ("\n"); } eITnotes.com

To be called from main() function as: node *head; ……… display (head); eITnotes.com

Inserting a Node in a List eITnotes.com

How to do? The problem is to insert a node before a specified node. – Specified means some value is given for the node (called key). – In this example, we consider it to be roll. Convention followed: – If the value of roll is given as negative, the node will be inserted at the end of the list. eITnotes.com

Contd. When a node is added at the beginning, – Only one next pointer needs to be modified. head is made to point to the new node. New node points to the previously first element. When a node is added at the end, – Two next pointers need to be modified. Last node now points to the new node. New node points to NULL. When a node is added in the middle, – Two next pointers need to be modified. Previous node now points to the new node. New node points to the next node. eITnotes.com

void insert (node *head) { int k = 0, rno; node *p, *q, *new; new = (node *) malloc(sizeof(node)); printf ("\nData to be inserted: "); scanf ("%d %s %d", &new->roll, new->name, &new->age); printf ("\nInsert before roll (-ve for end):"); scanf ("%d", &rno); p = *head; if (p->roll == rno) { new->next = p; *head = new; } /* At the beginning */ eITnotes.com

else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if { (p == NULL) q->next = new; new->next = NULL; } else if(p->roll { q->next = new; new->next = p; } } } /* At the end */ The pointers q and p always point to consecutive nodes. == rno) /* In the middle */ eITnotes.com

To be called from main() function as: node *head; ……… insert (&head); eITnotes.com

Deleting a node from the list eITnotes.com

What is to be done? Here also we are required to delete a specified node. – Say, the node whose roll field is given. Here also three conditions arise: – Deleting the first node. – Deleting the last node. – Deleting an intermediate node. eITnotes.com

void delete (node *head) { int rno; node *p, *q; printf ("\nDelete for roll :"); scanf ("%d", &rno); p = *head; if (p->roll == rno) /* Delete the first element */ { *head = p->next; free (p); } eITnotes.com

else { while ((p != NULL) && (p->roll != rno)) { q = p; p = p->next; } if(p == NULL)/* Element not found */ printf ("\nNo match :: deletion failed"); else if (p->roll == rno) /* Delete any other element */ { q->next = p->next; free (p); } } } eITnotes.com