Linked List Sudeshna Sarkar.

Slides:



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

CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review Learn about linked lists
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Lecture 25 Self-Referential Structures Linked Lists
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Linked List Spring 2013Programming and Data Structure1.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
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 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.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing 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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Linked List, Stacks Queues
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
The List ADT.
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Linked List Variations
Sequences 8/2/ :16 AM Linked Lists 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.
Introduction to Data Structures
Lists.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Lists.
Circular Buffers, Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
Problem Understanding
Linked List.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Programming and Data Structure
Data Structures & Algorithms
CS210- Lecture 6 Jun 13, 2005 Announcements
General List.
Linked Lists.
Structures and List Processing
BY PROF. IRSHAD AHMAD LONE.
Chapter 9 Linked Lists.
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

Linked List Sudeshna Sarkar

Self Referential Structures A structure referencing itself – how? So, we need a pointer inside a structure that points to a structure of the same type. struct list { int data; struct list *next; } ; typedef struct list ELEMENT; typedef ELEMENT * LINK;

Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem NULL A B C D

Linear Linked Lists head A B C D NULL Data type of head?

42 98 12 6 heap stack head

Linear Linked Lists tail head A B C D NULL Data type of head?

Representation with Dummy Node Ignore 10 17 head dummy node Insertion at the beginning is the same as insertion after the dummy node

Keeping track of a linked list: Must know the pointer to the first element of the list (called start, head, etc.). Linked lists provide flexibility in allowing the items to be rearranged efficiently. Insert an element. Delete an element. Spring 2012 Programming and Data Structure

struct list a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; a b c 1 NULL 2 NULL 3 NULL data next data next data next

Chaining these together a.next = &b; b.next = &c; a b c 1 2 3 NULL data next data next data next What are the values of : a.next->data a.next->next->data 2 3

Header file : list.h #include <stdio.h> #include <stdlib.h> typedef char DATA; struct list { DATA d; struct list * next; }; typedef struct list ELEMENT; typedef ELEMENT * LINK;

Storage allocation LINK head ; head = malloc (sizeof(ELEMENT)); head->d = ‘n’; head->next = NULL; creates a single element list. head n NULL

Storage allocation head->next = malloc (sizeof(ELEMENT)); head->next->d = ‘e’; head->next->next = NULL; A second element is added. head n e NULL

Storage allocation head->next->next = malloc (sizeof(ELEMENT)); head->next->next->d = ‘w’; head->next->next-> = NULL; We have a 3 element list pointed to by head. The list ends when next has the sentinel value NULL. head n e w NULL

List operations (ii) how to insert such a structure into the LIST, (i) How to initialize such a self referential structure (LIST), (ii) how to insert such a structure into the LIST, (iii) how to delete elements from it, (iv) how to search for an element in it, (v) how to print it, (vi) how to free the space occupied by the LIST?

Produce a list from a string (recursive version) #include “list.h” LINK StrToList (char s[]) { LINK head ; if (s[0] == ‘\0’) return NULL ; else { head = malloc (sizeof(ELEMENT)); head->d = s[0]; head->next = StrToList (s+1); return head; }

list from a string (iterative version) #include “list.h” LINK SToL (char s[]) { LINK head = NULL, tail; int i; if (s[0] != ‘\0’) { head = malloc (sizeof(ELEMENT)); head->d = s[0]; tail = head; for (i=1; s[i] != ‘\0’; i++) { tail->next = malloc(sizeof(ELEMENT)); tail = tail->next; tail->d = s[i]; } tail->next = NULL; return head; list from a string (iterative version)

Inserting at the Head Allocate a new node Insert new element Make new node point to old head Update head to point to new node Linked Lists

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node Linked Lists

Linear Linked List with tail head A B C D NULL

Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node Linked Lists

Removing at the Tail Removing at the tail of a singly linked list cannot be efficient! There is no constant-time way to update the tail to point to the previous node Linked Lists

Insertion To insert a data item into an ordered linked list involves: creating a new node containing the data, finding the correct place in the list, and linking in the new node at this place.

Example of an Insertion first prev ptr 3 5 8 12 - 7 new Create new node for the 7 Find correct place – when ptr finds the 8 (7 < 8) Link in new node with previous (even if last) and ptr nodes Also check insertion before first node!

Header file : list.h #include <stdio.h> #include <stdlib.h> struct list { int data; struct list * next; }; typedef struct list ELEMENT; typedef ELEMENT * LINK;

Create_node function Link create_node(int data) { LINK new; new = (LINK) malloc (sizeof (ELEMENT)); new -> data = data; return (new); }

Adding an Element to a Linked List Involves two steps: Finding the correct location Three possible positions: The front The end Somewhere in the middle Doing the work to add the node

Inserting to the Front There is no work to find the correct location head 93 head 48 17 142 There is no work to find the correct location Empty or not, head will point to the right location

Inserting to the End // // Find the end of the list (when at NIL) head 93 // 48 17 142 // Find the end of the list (when at NIL) Recursion or iteration

Inserting to the Middle head 93 // 142 17 48 142 // Used when order is important Go to the node that should follow the one to add Recursion or iteration

The Work to Add the Node newnode = (node *) malloc(sizeof(node)); typedef struct stud { int roll; char name[25]; int age; struct stud *next; } node; node *head; Create the new node Fill in the data field Deal with the next field Point to nil (if inserting to end) Point to current (front or middle) newnode = (node *) malloc(sizeof(node)); newnode->data = new_data newnode->next = current ; current = newnode;

Three Types of Insertion To front To end Get to the end, then add node In middle Get to the node before which the new node is to be inserted.

insert at front LINK insertfront (int data, LINK head) { LINK new, prev, first; new = create_node(data); new -> next = ptr; return new; // return pointer to first node }

Insert to end LINK insertend (int data, LINK ptr) { LINK new, head; new = create_node(data); head = ptr; if (ptr == NULL ) { new -> next = NULL; return new; } while (ptr->next != NULL) ptr = ptr -> next; ptr -> next = new; new -> next = NULL; return head;

Sorted insert function LINK insert (int data, LINK ptr) { LINK new, prev, first; new = create_node(data); if (ptr == NULL || data < ptr -> value){ // insert as new first node new -> next = ptr; return new; // return pointer to first node }

else { // not first one first = ptr; // remember start prev = ptr; ptr = ptr -> next; // second while (ptr != NULL && data > ptr -> data) { ptr = ptr -> next; } prev -> next = new; // link in new -> next = ptr; //new node return first; } // end else } // end insert

Deletion To delete a data item from a linked list involves (assuming it occurs only once!): finding the data item in the list, and linking out this node, and freeing up this node as free space.

Example of Deletion first prev ptr 3 5 8 12 - When ptr finds the item to be deleted, e.g. 8, we need the previous node to make the link to the next one after ptr (i.e. ptr -> next). Also check whether first node is to be deleted.

// delete the item from ascending list LINK delete_item(int data, LINK ptr) { LINK prev, first; first = ptr; // remember start if (ptr == NULL) { return NULL; } else if (data == ptr -> data) // first node { ptr = ptr -> next; // second node free(first); // free up node return ptr; // second

else // check rest of list { prev = ptr; ptr = ptr -> next; // find node to delete while (ptr != NULL && data > ptr->data) ptr = ptr -> next; }

if (ptr == NULL || data != ptr->data) // NOT found in ascending list // nothing to delete { return first; // original } else // found, delete ptr node prev -> next = ptr -> next; free(ptr); // free node } // end delete

Representation with Dummy Node head dummy node Insertion at the beginning is the same as insertion after the dummy node

Initialization head typedef struct list { int data; struct list *next; Write a function that initializes LIST typedef struct list { int data; struct list *next; } ELEMENT; ELEMENT* Initialize (int element) { ELEMENT *head; head = (ELEMENT *)malloc(sizeof(data)); // Create initial node head->data = element; head -> next = NULL; return head; }

Insert head head

ELEMENT* Insert(ELEMENT *head, int element, int position) { int i=0; ELEMENT *temp, *new; if (position < 0) { printf("\nInvalid index %d\n", position); return head; } temp = head; for(i=0;i<position;i++){ temp=temp->next; if(temp==NULL) { new = (ELEMENT *)calloc(1,sizeof(ELEMENT)); new ->data = element; new -> next = temp -> next; temp -> next = new;

Delete head temp head temp

ELEMENT* Delete(data *head, int position) { int i=0;data *temp,*hold; if (position < 0) { printf("\nInvalid index %d\n", position); return head; } temp = head; while ((i < position) && (temp -> next != NULL)) { temp = temp -> next; i++; if (temp -> next == NULL) { hold = temp -> next; temp -> next = temp -> next -> next; free(hold);

Searching a data element int Search (ELEMENT *head, int element) { int i; ELEMENT *temp; i = 0; temp = head -> next; while (temp != NULL) { if (temp -> x == element) return TRUE; temp = temp -> next; i++; } return FALSE;

Printing the list void Print (ELEMENT *head) { ELEMENT *temp; temp = head -> next; while (temp != NULL) { printf("%d->", temp -> data); temp = temp -> next; }

Print the list backwards . head How can you when the links are in forward direction ? Can you apply recursion?

Print the list backwards void PrintArray(ELEMENT *head) { if(head -> next == NULL) { //boundary condition to stop recursion printf(" %d->",head -> data); return; } PrintArray(head -> next); // calling function recursively printf(" %d ->",head -> data); // Printing current elemen

Free the LIST head temp1 temp2 We can free temp1 only after we have retrieved the address of the next element (temp2) from temp1.

Free the list void Free(ELEMENT *head) { ELEMENT *temp1, *temp2; temp1 = head; while(temp1 != NULL) /*boundary condition check*/ { temp2 = temp1 -> next; free(temp1); temp1 = temp2; }

/* Count a list recursively */ int count (LINK head) { if (head == NULL) return 0; return 1+count(head->next); } /* Count a list iteratively */ int count (LINK head) { int cnt = 0; for ( ; head != NULL; head=head->next) ++cnt; return cnt; }

/* Print a List */ void PrintList (LINK head) { if (head == NULL) printf (“NULL”) ; else { printf (“%c --> “, head->d) ; PrintList (head->next); }

/* Concatenate two Lists */ void concatenate (LINK ahead, LINK bhead) { if (ahead->next == NULL) ahead->next = bhead ; else concatenate (ahead->next, bhead); }

Delete a list and free memory /* Recursive deletion of a list */ void delete_list (LINK head) { if (head != NULL) { delete_list (head->next) ; free (head) ; /* Release storage */ }

Array versus Linked Lists Arrays are suitable for: Inserting/deleting an element at the end. Randomly accessing any element. Searching the list for a particular value. Linked lists are suitable for: Inserting an element. Deleting an element. Applications where sequential access is required. In situations where the number of elements cannot be predicted beforehand. Spring 2012 Programming and Data Structure

Types of Lists Depending on the way in which the links are used to maintain adjacency, several different types of linked lists are possible. Linear singly-linked list (or simply linear list) One we have discussed so far. A B C head Spring 2012 Programming and Data Structure

Programming and Data Structure Circular linked list The pointer from the last element in the list points back to the first element. A B C head Spring 2012 Programming and Data Structure

Programming and Data Structure 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. A B C head tail Spring 2012 Programming and Data Structure

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. Spring 2012 Programming and Data Structure

Conceptual Idea List implementation Insert and the related functions Delete Traverse Spring 2012 Programming and Data Structure

Few Exercises to Try Out Write a function to: Concatenate two given list into one big list. node *concatenate (node *head1, node *head2); Insert an element in a linked list in sorted order. The function will be called for every element to be inserted. void insert_sorted (node **head, node *element); Always insert elements at one end, and delete elements from the other end (first-in first-out QUEUE). void insert_q (node **head, node *element) node *delete_q (node **head) /* Return the deleted node */ Spring 2012 Programming and Data Structure

Thank You

Insertion After Insertion p2 p1 C A q B /* Inserting an element in a linked list. */ void insert (LINK p1, LINK p2, LINK q) { p1->next = q; q->next = p2; } After Insertion p2 p1 A C q B

Insertion Before Insertion p2 p1 C A q B Insertion in a list takes a fixed amount of time once the position in the list is found. Before Insertion p2 p1 A C q B

Deletion p 1 2 3 p->next = p->next->next; garbage p Before deletion p 1 2 3 p->next = p->next->next; garbage p After deletion 1 2 3

Deletion p 1 2 3 q = p->next; p->next = p->next->next; p Before deletion p 1 2 3 q = p->next; p->next = p->next->next; p After deletion 1 2 3 q free (q) ;

Print the list backwards . head How can you when the links are in forward direction ? Can you apply recursion?

Print the list backwards void PrintArray(ELEMENT *head) { if(head -> next == NULL) { /*boundary condition to stop recursion*/ printf(" %d->",head -> data); return; } PrintArray(head -> next); /* calling function recursively*/ printf(" %d ->",head -> data);/* Printing current elemen

Free the LIST head temp1 temp2 We can free temp1 only after we have retrieved the address of the next element (temp2) from temp1.

Free the list (iterative) void Free(ELEMENT *head) { ELEMENT *temp1, *temp2; temp1 = head; while(temp1 != NULL) /*boundary condition check*/ { temp2 = temp1 -> next; free(temp1); temp1 = temp2; }

Delete a list and free memory (recursive) /* Recursive deletion of a list */ void delete_list (LINK head) { if (head != NULL) { delete_list (head->next) ; free (head) ; /* Release storage */ }

/* Count a list recursively */ int count (LINK head) { if (head == NULL) return 0; return 1+count(head->next); } /* Count a list iteratively */ int count (LINK head) { int cnt = 0; for ( ; head != NULL; head=head->next) ++cnt; return cnt; }

/* Print a List */ void PrintList (LINK head) { if (head == NULL) printf (“NULL”) ; else { printf (“%c --> “, head->d) ; PrintList (head->next); }

/* Concatenate two Lists */ void concatenate (LINK ahead, LINK bhead) { if (ahead->next == NULL) ahead->next = bhead ; else concatenate (ahead->next, bhead); }

Assignment:Reverse a list head 1 2 3 4 head 1 2 3 4

Doubly linked list A B C Assignment : Insertion, deletion in a doubly