Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Similar documents
Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : End-Semester Examination

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

CSE 230 Intermediate Programming in C and C++

Dept. of CSE, IIT KGP

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

Singly linked lists in C.

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

ECE264 Spring 2013 Final Exam, April 30, 2013

ESC101N: Fundamentals of Computing End-sem st semester

Binary Search Tree 1.0. Generated by Doxygen Mon Jun :12:39

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

Arrays & Linked Lists

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

DC54 DATA STRUCTURES DEC 2014

MODULE 5: Pointers, Preprocessor Directives and Data Structures

CS61, Fall 2012 Section 2 Notes

CS 237 Meeting 19 10/24/12

Recursion, Trees and Tree Traversal. Module CE System Programming & Computer Control Systems

CA341 - Comparative Programming Languages

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Pointers and File Handling

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Fall, 2015 Prof. Jungkeun Park

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-0-0)

BBM 201 DATA STRUCTURES

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

ESC101N Fundamentals of Computing

Kurt Schmidt. October 30, 2018

What is recursion. WAP to find sum of n natural numbers using recursion (5)

Linked List. April 2, 2007 Programming and Data Structure 1

S.E. Sem. III [CMPN] Data Structures. Primitive Linear Non Linear

Solution for Data Structure

MODULE V: POINTERS & PREPROCESSORS

Data Structures and Algorithms (DSA) Course 9 Lists / Graphs / Trees. Iulian Năstac

Dynamic memory allocation

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

ECE264 Fall 2013 Exam 3, November 20, 2013

CP2 Revision. theme: dynamic datatypes & data structures

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan

S.E. Sem. III [INFT] Data Structures & Analysis. Primitive Linear Non Linear

Procedural programming with C

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

Data and File Structures Laboratory

Linked Lists in C and C++

Chapter 8 STRUCTURES IN C

Dynamic Memory Allocation

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

Computer Systems and Networks

CS349/SE382 A1 C Programming Tutorial

4) In C, if you pass an array as an argument to a function, what actually gets passed?

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Deadlock-free Packet Switching

PROGRAMMAZIONE I A.A. 2017/2018

Character Strings. String-copy Example

Low-Level C Programming. Memory map Pointers Arrays Structures

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Dynamic Data Structures. CSCI 112: Programming in C

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

File: /media/young/d1 (180713)/Work tive_tree_search/search_defs.h Page 1 of 1

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays. CS10001: Programming & Data Structures. Pallab Dasgupta Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

CS 241 Data Organization Binary Trees

Recursion. Data and File Structures Laboratory. DFS Lab (ISI) Recursion 1 / 27

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Downloaded from : Algorithm: Implementation QUESTION 1 :

Understanding Pointers

CSCE 2014 Final Exam Spring Version A

Introduction to C Language (M3-R )

Archivo: /home/young/work/cordic/binary_tree_search/search_defs.h Página 1 de 1

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Self-referential Structures and Linked List. Programming and Data Structure 1

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Data and File Structures Laboratory

Midterm Review. CS 211 Fall 2018

Welcome! COMP s1. Programming Fundamentals

CS 261 Data Structures. Introduction to C Programming

CSCI 171 Chapter Outlines

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

PDS Class Test 2. Room Sections No of students

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #09. Topic: Structures in C Date:

MODULE 3: LINKED LIST

Lecture 8 Dynamic Memory Allocation

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

List, Stack and Queue Implementation

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

Subject: Fundamental of Computer Programming 2068

Transcription:

1 Linked Lists.. and other linked structures Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

Dynamic memory allocation: review typedef struct { int hitemp; int lotemp; double precip; WeatherData; int main ( ) { int numdays; WeatherData *days; scanf ( %d, &numdays) ; days=(weatherdata *)malloc (sizeof(weatherdata)*numdays); if (days == NULL) printf ( Insufficient memory );... free (days) ;

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

Self-referential structures struct list { int data ; struct list * next ; ; The pointer variable next is called a link. Each structure is linked to a succeeding structure by next.

Pictorial representation A structure of type struct list data next The pointer variable next contains either an address of the location in memory of the successor list element or the special value NULL defined as 0. NULL is used to denote the end of the list.

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

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

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 elem next node NULL A B C D

Linear Linked Lists A pointer addresses the first element of the list. Each element points at a successor element. The last element has a link value NULL. NULL A B C D

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 ; = (LINK) malloc (sizeof(element)); ->d = n ; ->next = NULL; creates a single element list. n NULL

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

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

List operations List operations (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 <stdio.h> #include <stdlib.h> typedef char DATA; struct list { DATA d; struct list * next; ; typedef struct list ELEMENT; typedef ELEMENT *LINK; LINK StrToList (char s[ ]) { LINK ; if (s[0] == \0 ) return NULL ; else { = (LINK) malloc (sizeof(element)); ->d = s[0]; ->next = StrToList (s+1); return ;

Produce a list from a string ( iterative version ) LINK SToL (char s[ ]) { LINK = NULL, tail; int i; s H E L L O \0 if (s[0]!= \0 ) { = (LINK) malloc (sizeof(element)); ->d = s[0]; tail = ; H tail for (i=1; s[i]!= \0 ; i++) { tail->next = (LINK) malloc(sizeof(element)); tail = tail->next; tail->d = s[i]; tail->next = NULL; return ; H H tail E tail

Inserting at the Head 1. Allocate a new node X Y 2. Insert new element 3. Make new node point to old 4. Update to point to new node new new W W new = (LINK) malloc(sizeof(element)); new-> next = ; X Y new W = new; X Y

Removing the Head 1. Update to point to next node in the list 2. Allow garbage collector to reclaim the former first node X Y new = ; new X Y = new->next; new X Y free(new); new

Inserting at the Tail 1. Allocate a new node 2. Insert new element new X Z Y tail new NULL Z 3. Have new node point to null 4. Have old last node point to new node 5. Update tail to point to new node NULL new = (LINK) malloc(sizeof(element)); new->next = NULL; X Y Z NULL tail->next = new; tail new X Y Z NULL tail = new; tail

Removing the Tail 1. Bring ptr to the second last node 2. Make ptr->next equal to NULL 3. Free tail 4. Make ptr the new tail X Y Z NULL ptr tail X Y NULL Z NULL ptr->next = NULL; ptr tail X Y NULL Z NULL free(tail); tail = ptr; ptr tail

Insertion into an ordered list new N Create a new node containing the data Find the correct place in the list, and Link the new node at this place. A M P prev new = (LINK) malloc(sizeof(element)); A M prev P new->next = prev->next; prev->next = new; new N Why is the following not okay? prev->next = new; new->next = prev->next;

Insertion function #include <stdio.h> #include <stdlib.h> struct list { int data; struct list * next; ; typedef struct list ELEMENT; typedef ELEMENT * LINK; Listpointer create_node(int data) { LINK new; new = (LINK) malloc (sizeof (ELEMENT)); new -> data = data; return (new); 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) { prev = ptr; ptr = ptr -> next; prev -> next = new; // link in new -> next = ptr; //new node return first;

Deletion Steps: Finding the data item in the list, and Linking out this node, and Freeing up this node as free space. A M N P prev ptr prev->next = ptr->next; free(ptr); A M N P prev ptr What will happen if we did the following? free(ptr); prev->next = ptr->next;

Deletion function else // check rest of list { prev = ptr; ptr = ptr -> next; // 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 // find node to delete while (ptr!= NULL && data > ptr->data) { prev = ptr; ptr = ptr -> next; if (ptr == NULL data!= ptr->data) { // NOT found in ascending list return first; // original else { // found, delete ptr node prev -> next = ptr -> next; free(ptr); // free node return first; // original

Searching for a data element in a list int Search(ELEMENT *, int element) { ELEMENT *temp; temp = ; while (temp!= NULL) { if (temp -> x == element) return TRUE; temp = temp -> next; return FALSE;

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

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

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

Freeing a list What will happen if we free the first node of the list without placing a pointer on the second? In each iteration temp1 points at the of the list and temp2 points at the second node. void Free(ELEMENT *) { ELEMENT *temp1, *temp2; temp1 = ; while(temp1!= NULL) { temp2 = temp1 -> next; free(temp1); temp1 = temp2;

Counting the number of nodes in a list RECURSIVE APPROACH int count (LINK ) { if ( == NULL) return 0; return 1+count(->next); ITERATIVE APPROACH int count (LINK ) { int cnt = 0; for ( ;!= NULL; =->next) ++cnt; return cnt;

Concatenate two Lists void concatenate (LINK a, LINK b) { if (a->next == NULL) a->next = b ; else concatenate (a->next, b);

32 And Other linked structures Like Trees, Sparse Matrices and Graphs Binary Tree (Height = 3) Sparse Matrix 0 9 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 Graph (Number of Vertices = 10) (Number of Edges = 11) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

33 And Other linked structures: Binary Trees Root Data 30 At most 2 branches Parent Node 23 36 Internal Node struct node { int data; Left Child Right Child struct node* left; struct node* right; 18 27 34 40 4 21 29 38 42 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Leaf Nodes

34 And Other linked structures: Binary Trees Root 30 struct node { int data; struct node* left; struct node* right; 23 36 Node 18 27 34 40 Data 4 21 29 38 42 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR To Left Child To Right Child

35 And Other linked structures: Sparse matrices For the sparse matrix below: Storage as a 2-D Array: int M[5][6]; 0 9 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 Storage required for 30 elements (with only 4 non-zero entries) = 30 * sizeof(int) = 120Bytes (For integers of size 4 Bytes) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

36 And Other linked structures: Sparse matrices Storage as a list of Tripples : (row, column, data) 0 9 0 0 0 0 0 0 0 0 0 5 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 struct tripple { int row, column, data; struct tripple *next; 1 2 9 2 6 5 3 1 6 5 5 3 NULL Row Column Data Storage required for 4 entries = (3 sizeof(int) + sizeof(struct tripple*) 4) = (3 4 + 8) 4 = 80 Bytes < 120 Bytes INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

37 And Other linked structures: Graphs Adjacency Matrix Representation: Matrix location ( i, j ) indicates an edge between vertices i and j 0 4 0 1 0 0 0 0 1 0 1 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 Adjacency Matrix for an undirected graph 1 5 2 3 Storage as a 2-D Array: int G[6][6]; Storage required for 36 elements (with 6 vertices 5 edges) = 36 * sizeof(int) = 144Bytes (For integers of size 4 Bytes) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

38 And Other linked structures: Graphs Adjacency List Representation: Each vertex s neighbours are maintained in a linked list 0 4 0 1 2 3 1 NULL 0 2 1 3 4 5 NULL NULL NULL 1 5 struct vertex{ int id; 2 3 4 5 3 3 NULL NULL struct vertex* next_adj; Storage required = ( V + sum of degree ) * sizeof( structure ) INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR