Linked Lists and other Dynamic Data Structures

Similar documents
The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Ashish Gupta, Data JUET, Guna

Linked List Practice Questions

BBM 201 DATA STRUCTURES

Module III. Linear Data Structures and their Linked Storage Representation

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Homework Assignment #1

Dynamic Data Structures

Chapter 17: Linked Lists

Programming. Lists, Stacks, Queues

Chapter 17: Linked Lists

CIS 190: C/C++ Programming. Linked Lists

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

Solution for Data Structure

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

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

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

.:: UNIT 3 ::. LIST AND LINKED LIST

CS32 Discussion Week 3

The time and space are the two measure for efficiency of an algorithm.

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

CS 103 Unit 11. Linked Lists. Mark Redekopp

CMSC 341 Lecture 7 Lists

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

INSERT AS A FIRST NODE

CA341 - Comparative Programming Languages

Computer Systems and Networks

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS 103 Unit 11. Linked Lists. Mark Redekopp

PROGRAMMAZIONE I A.A. 2017/2018

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

Now consider the following situation after deleting three elements from the queue...

CP2 Revision. theme: dynamic datatypes & data structures

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

Chapter 8 STRUCTURES IN C

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

Chapter 20: Binary Trees

NCS 301 DATA STRUCTURE USING C

Cpt S 122 Data Structures. Data Structures

Programming II (CS300)

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?

A linked list grows as data is added to it. In a linked list each item is packaged into a node.

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Data Structures (CS301) LAB

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Pointers. Array. Solution to the data movement in sequential representation

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

Elementary Data Structures: Lists

(Section : Computer Science)

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

CS350: Data Structures. Doubly Linked Lists

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

Introduction to Computer Science II CS S-20 Linked Lists IV

Midterm Review. CS 211 Fall 2018

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

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW

Linked List. ape hen dog cat fox. tail. head. count 5

+ Abstract Data Types

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

Data Structure Series

Dynamic Data Structures (II)

AIM:- To write a C program to create a singly linked list implementation.

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

CSC212 Data Structure - Section FG

lecture09: Linked Lists

Data Structures and Algorithms for Engineers

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

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists

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

Linked Lists. C Linked List

// Pointer to the first thing in the list

Data Structures and Algorithms for Engineers

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

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

CPSC 261 Midterm 2 Thursday March 17 th, 2016

ECE 2400 Computer Systems Programming, Fall 2018 PA2: List and Vector Data Structures

Introduction to Programming in C Department of Computer Science and Engineering

Ch. 17: Linked Lists. Introduction to Linked Lists

CS S-20 Linked Lists IV 1

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

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

Algorithms, Data Structures, and Problem Solving

Spring 2008 Data Structures (CS301) LAB

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work.

Programming II (CS300)

CMPT 225. Lecture 6 linked list

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

SINGLE LINKED LIST. Algorithm for allocating memory for the new node

Transcription:

Linked Lists and other Dynamic Data Structures

Arrays Fixed in size Allocated in advance within a contiguous memory block Look-up is fast Resizing and Deleting is hard (reallocate and copy)

Dynamic Data Structures Grow and shrink one element at a time Not allocated within contiguous memory block Resizing and Deleting is easy Look-up can be hard

Linked Lists

Linked Lists A linear series of memory locations connected with pointers

Linked Lists A linear series of memory locations connected with pointers typedef struct _node { int data; struct node* next; }node; Node Data Pointer to next

Linked Lists vs. Arrays Grow and shrink one element at a time No copying or realloc cost on extending Not allocated contiguously in memory Look-up/searching takes linear time, O(n) random accesses

More on Implementation

More on Implementation Beginning of a linked list Keep track of the head node. End of a linked list tail->next=null;

More on Implementation Beginning of a linked list Keep track of the head node. typedef struct _node { int data; struct node* next; struct node* prev; }node; Doubly-linked lists End of a linked list tail->next=null; Node Pointer to prev Data Linked lists can also be doubly-linked and have pointers to next as well as the previous nodes Pointer to next

Initializing a List

Initializing a List node* head=null; head points to the beginning of the (empty) list

Initializing a List node* head=null; head points to the beginning of the (empty) list head=malloc(sizeof(node)); head->data=1; head->next=null; head now contains the first element of the list

Initializing a List node* head=null; head points to the beginning of the (empty) list head=malloc(sizeof(node)); head->data=1; head->next=null; head now contains the first element of the list struct list{ node* head; node* tail; //optional }; The list can be represented as a struct of head and tail pointer

Adding an Element

Adding an Element H

Adding an Element Adding to head newnode=malloc(sizeof(node)); newnode->data=1; newnode->next=mylist->head; mylist->head=newnode; H

Adding an Element Adding to head H newnode=malloc(sizeof(node)); newnode->data=1; newnode->next=mylist->head; mylist->head=newnode; New

Adding an Element Adding to head H newnode=malloc(sizeof(node)); newnode->data=1; newnode->next=mylist->head; mylist->head=newnode; New H New

Adding an Element

Adding an Element null

Adding an Element Adding to tail 1 null node *newnode,*temp; newnode=malloc(sizeof(node)); newnode->data=num; temp=mylist->head; while(temp->next!= NULL) temp=temp->next; temp->next=newnode; newnode->next=null;

Adding an Element Adding to tail 1 null node *newnode,*temp; newnode=malloc(sizeof(node)); newnode->data=num; temp=mylist->head; while(temp->next!= NULL) temp=temp->next; New temp->next=newnode; newnode->next=null;

Adding an Element Adding to tail 1 null node *newnode,*temp; newnode=malloc(sizeof(node)); newnode->data=num; temp=mylist->head; while(temp->next!= NULL) temp=temp->next; New temp->next=newnode; newnode->next=null; New null

Adding an Element Adding to tail 2 // Assuming we have a pointer to tail node* newnode; newnode=malloc(sizeof(node)); node* temp=mylist->tail; temp->next=newnode; newnode->next=null; mylist->tail=newnode;

Adding an Element null Adding to tail 2 // Assuming we have a pointer to tail node* newnode; newnode=malloc(sizeof(node)); node* temp=mylist->tail; temp->next=newnode; newnode->next=null; mylist->tail=newnode;

Adding an Element null Adding to tail 2 // Assuming we have a pointer to tail node* newnode; newnode=malloc(sizeof(node)); node* temp=mylist->tail; temp->next=newnode; New newnode->next=null; mylist->tail=newnode;

Adding an Element null Adding to tail 2 // Assuming we have a pointer to tail node* newnode; newnode=malloc(sizeof(node)); node* temp=mylist->tail; temp->next=newnode; New newnode->next=null; mylist->tail=newnode; New null

Adding an Element

Adding an Element Left Right

Adding an Element Adding in the middle Left Right int i; node* left,right,temp,newnode; newnode=malloc(sizeof(node)); newnode->data=1; temp=mylist->head; for(i=1;i<pos-1;i++) temp=temp->next; left=temp; right=left->next left->next=newnode; newnode->next=right;

Adding an Element Adding in the middle Left Right int i; node* left,right,temp,newnode; newnode=malloc(sizeof(node)); newnode->data=1; temp=mylist->head; for(i=1;i<pos-1;i++) temp=temp->next; left=temp; New right=left->next left->next=newnode; newnode->next=right;

Adding an Element Adding in the middle Left Right int i; node* left,right,temp,newnode; newnode=malloc(sizeof(node)); newnode->data=1; temp=mylist->head; for(i=1;i<pos-1;i++) temp=temp->next; left=temp; New right=left->next left->next=newnode; newnode->next=right; Left New Right

Traversing Linked Lists node* current=mylist->head; while(current!=null){ /*Do Stuff*/ current=current->next; } Node Data Pointer to next

Deleting an Element

Deleting an Element Left Right del

Deleting an Element Left Right del if(to_delete==mylist->head){ mylist->head=to_delete->next; free(to_delete); }else{ left->next=to_delete->next; free(to_delete); }

Deleting an Element Left Right del if(to_delete==mylist->head){ mylist->head=to_delete->next; free(to_delete); }else{ left->next=to_delete->next; free(to_delete); } Left del Right

Deleting an Element Left Right del if(to_delete==mylist->head){ mylist->head=to_delete->next; free(to_delete); }else{ left->next=to_delete->next; free(to_delete); } Left del Right Left Right

Recursive Reverse

Recursive Reverse void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; // empty list base case first = *headref; // suppose first = {1, 2, 3} rest = first->next; // rest = {2, 3} if (rest == NULL) return; // empty rest base case RecursiveReverse(&rest); // Recursively reverse the smaller {2, 3} case first->next->next = first; // put the first elem on the end of the list first->next = NULL; } *headref = rest; // fix the head pointer

void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest1 =1 first1 =0 *headref = rest;}

Head 0 1 2 Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest2 =2 first2 =1 rest1 =1 first1 =0 *headref = rest;}

Head 0 1 2 Head 0 1 2 Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest3 =null first3 =2 rest2 =2 first2 =1 rest1 =1 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; *headref = rest;} rest3 =null first3 =2 rest2 =2 first2 =1 rest1 =1 first1 =0 Returns

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest2 =2 first2 =1 rest1 =1 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest2 =2 first2 =1 rest1 =1 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest2 =2 first2 =1 rest1 =1 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest2 =2 first2 =1 rest1 =2 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; *headref = rest;} rest2 =2 first2 =1 rest1 =2 first1 =0 Notice rest changes

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest1 =2 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest1 =2 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest1 =2 first1 =0 *headref = rest;}

Head 0 1 2 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; rest1 =2 first1 =0 *headref = rest;}

Head 2 1 0 void RecursiveReverse(node** headref) { node* first; node* rest; if (*headref == NULL) return; first = *headref; rest = first->next; if (rest == NULL) return; RecursiveReverse(&rest); first->next->next = first; first->next = NULL; *headref = rest;}

Dos and Don ts

Dos and Don ts Don t lose track of the head head=head->next; node* current=head; current=current->next;

Dos and Don ts Don t lose track of the head head=head->next; node* current=head; current=current->next; Do check for NULLs current=current->next; while(current!=null) current=current->next;

Dos and Don ts Don t lose track of the head head=head->next; node* current=head; current=current->next; Do check for NULLs current=current->next; while(current!=null) current=current->next; Do reassign the tail tail->next=new_node; return; tail->next=new_node; new_node->next=null; return;

Dos and Don ts Don t lose track of the head head=head->next; node* current=head; current=current->next; Do check for NULLs current=current->next; while(current!=null) current=current->next; Do reassign the tail tail->next=new_node; return; Do free memory left->next=to_delete->next; return; tail->next=new_node; new_node->next=null; return; left->next=to_delete->next; free(to_delete); return;

Other Dynamic DS

Other Dynamic DS struct tnode{ int data; struct tnode* left; struct tnode* right; };

Other Dynamic DS struct tnode{ int data; struct tnode* left; struct tnode* right; }; struct vertex{ int data; struct edges* edges; };

B-Tree Animation https://www.cs.usfca.edu/~galles/visualization/ BPlusTree.html