Linked Lists and other Dynamic Data Structures

Size: px
Start display at page:

Download "Linked Lists and other Dynamic Data Structures"

Transcription

1 Linked Lists and other Dynamic Data Structures

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

3 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

4 Linked Lists

5 Linked Lists A linear series of memory locations connected with pointers

6 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

7 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

8 More on Implementation

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

10 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

11 Initializing a List

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

13 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

14 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

15 Adding an Element

16 Adding an Element H

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

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

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

20 Adding an Element

21 Adding an Element null

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

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

24 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

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

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

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

28 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

29 Adding an Element

30 Adding an Element Left Right

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

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

33 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

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

35 Deleting an Element

36 Deleting an Element Left Right del

37 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); }

38 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

39 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

40 Recursive Reverse

41 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

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

43 Head 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;}

44 Head Head 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;}

45 Head Head Head 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;}

46 Head 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

47 Head 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;}

48 Head 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;}

49 Head 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;}

50 Head 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;}

51 Head 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

52 Head 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;}

53 Head 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;}

54 Head 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;}

55 Head 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;}

56 Head 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;}

57 Dos and Don ts

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

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

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

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

62 Other Dynamic DS

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

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

65 B-Tree Animation BPlusTree.html

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

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation

More information

Ashish Gupta, Data JUET, Guna

Ashish Gupta, Data JUET, Guna Categories of data structures Data structures are categories in two classes 1. Linear data structure: - organized into sequential fashion - elements are attached one after another - easy to implement because

More information

Linked List Practice Questions

Linked List Practice Questions Linked List Practice Questions 1. The following function reverse() is supposed to reverse a singly linked list. There is one line missing at the end of the function. /* head_ref is a double pointer which

More information

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES BBM 201 DATA STRUCTURES Lecture 8: Dynamically Allocated Linked Lists 2017-2018 Fall int x; x = 8; int A[4]; An array is stored as one contiguous block of memory. How can we add a fifth element to the

More information

Module III. Linear Data Structures and their Linked Storage Representation

Module III. Linear Data Structures and their Linked Storage Representation Module III Linear Data Structures and their Linked Storage Representation 3.1 Linked List Linked list is the collection of inter connected nodes with a head node representing the first node and a tail

More information

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

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first Linked Lists Introduction to Linked Lists A data structure representing a Week 8 Gaddis: Chapter 17 CS 5301 Spring 2014 Jill Seaman A series of dynamically allocated nodes chained together in sequence

More information

Homework Assignment #1

Homework Assignment #1 CISC 2200 Data Structure Spring, 2016 Homework Assignment #1 1 Short practices on linked list, see Textbook Page 205, Problem 9-12 2 Pointers: operations (deference, address-of), and syntax errors (a)

More information

Dynamic Data Structures

Dynamic Data Structures Dynamic Data Structures We have seen that the STL containers vector, deque, list, set and map can grow and shrink dynamically. We now examine how some of these containers can be implemented in C++. To

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists 17.1 Introduction to the Linked List ADT Introduction to the Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures list head

More information

Programming. Lists, Stacks, Queues

Programming. Lists, Stacks, Queues Programming Lists, Stacks, Queues Summary Linked lists Create and insert elements Iterate over all elements of the list Remove elements Doubly Linked Lists Circular Linked Lists Stacks Operations and implementation

More information

Chapter 17: Linked Lists

Chapter 17: Linked Lists Chapter 17: Linked Lists Copyright 2009 Pearson Education, Inc. Copyright Publishing as Pearson 2009 Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 17.1 Introduction to the

More information

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

CIS 190: C/C++ Programming. Linked Lists CIS 190: C/C++ Programming Linked Lists Why Use Linked Lists? solve many of the problems arrays have like Problems with Arrays arrays have a fixed size may be too large, or too small arrays must be held

More information

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

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

More information

Solution for Data Structure

Solution for Data Structure Solution for Data Structure May 2016 INDEX Q1 a 2-3 b 4 c. 4-6 d 7 Q2- a 8-12 b 12-14 Q3 a 15-18 b 18-22 Q4- a 22-35 B..N.A Q5 a 36-38 b N.A Q6- a 39-42 b 43 1 www.brainheaters.in Q1) Ans: (a) Define ADT

More information

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

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allows for creation

More information

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

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME CLOSED BOOK Exam #1 100 Points NAME 1. The following program has (at least) 10 syntax errors. Circle each error. Write the corrected program in the blank space below. 2 points for each error you find.

More information

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

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2 CS32 - Week 2 Umut Oztok July 1, 2016 Arrays A basic data structure (commonly used). Organize data in a sequential way. Arrays A basic data structure (commonly used). Organize data in a sequential way.

More information

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

.:: UNIT 3 ::. LIST AND LINKED LIST :: UNIT 3 :: LIST AND LINKED LIST 31 A list is a useful structure to hold a collection of data The simplest method to implement a List ADT is to use an array linear list, contiguous list Characteristics

More information

CS32 Discussion Week 3

CS32 Discussion Week 3 CS32 Discussion Week 3 Muhao Chen muhaochen@ucla.edu http://yellowstone.cs.ucla.edu/~muhao/ 1 Outline Doubly Linked List Sorted Linked List Reverse a Linked List 2 Doubly Linked List A linked list where

More information

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

The time and space are the two measure for efficiency of an algorithm. There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space

More information

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

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements APS105 Lists Structures Textbook Chapters 10.1, 10.3, 10.4, 10.6 2 Structures Arrays allow a collection of elements All of the same type How to collect elements of different types? Structures; in C: struct

More information

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

CS132 Algorithm. Instructor: Jialiang Lu   Office: Information Center 703 CS132 Algorithm Instructor: Jialiang Lu Email: jialiang.lu@sjtu.edu.cn Office: Information Center 703 Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position.

More information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 103 Unit 11. Linked Lists. Mark Redekopp 1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++

More information

CMSC 341 Lecture 7 Lists

CMSC 341 Lecture 7 Lists CMSC 341 Lecture 7 Lists Today s Topics Linked Lists vs Arrays Nodes Using Linked Lists Supporting Actors (member variables) Overview Creation Traversal Deletion UMBC CMSC 341 Lists 2 Linked Lists vs Arrays

More information

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

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Readings List Implementations Chapter 20.2 Objectives Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations Additional references:

More information

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

Elementary Data Structures: Part 1: Arrays, Lists. CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Elementary Data Structures: Part 1: Arrays, Lists CSE 2320 Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1 Basic Types Types like integers, real numbers, characters.

More information

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

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queues October 20, 2017 Hassan Khosravi / Geoffrey Tien 1 Queue ADT Queue ADT should support at least the first two operations: enqueue insert an item to the back of the queue dequeue remove an item from

More information

INSERT AS A FIRST NODE

INSERT AS A FIRST NODE CIRCULAR LINKED LIST Insert as a first node Insert as a last node Delete first node Delete last node Insert after a node Insert before a node Search Traverse INSERT AS A FIRST NODE void insertf() struct

More information

CA341 - Comparative Programming Languages

CA341 - Comparative Programming Languages CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data

More information

Computer Systems and Networks

Computer Systems and Networks University of the Pacific LECTURE 5: C PROGRAMMING Computer Systems and Networks Dr. Pallipuram (vpallipuramkrishnamani@pacific.edu) Today s Class o Pointer basics o Pointers and mul;- dimensional arrays

More information

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

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi Stack and Queue How do we implement a Queue using Array? : A collection of nodes with linear ordering defined on them. Each node holds an element and points to the next node in the order. The first node

More information

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

Queues. A queue is a special type of structure that can be used to maintain data in an organized way. A queue is a special type of structure that can be used to maintain data in an organized way. This data structure is commonly implemented in one of two ways: as an array or as a linked list. In either

More information

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015

LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 LINKED LISTS cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 5 due tonight at midnight -assignment 6 is out -YOU WILL BE SWITCHING PARTNERS! 3 assignment

More information

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 103 Unit 11. Linked Lists. Mark Redekopp 1 CS 103 Unit 11 Linked Lists Mark Redekopp 2 NULL Pointer Just like there was a null character in ASCII = '\0' whose ue was 0 There is a NULL pointer whose ue is 0 NULL is "keyword" you can use in C/C++

More information

PROGRAMMAZIONE I A.A. 2017/2018

PROGRAMMAZIONE I A.A. 2017/2018 PROGRAMMAZIONE I A.A. 2017/2018 LINKED LISTS LINKED LIST What are the problems with arrays? üsize is fixed üarray items are stored contiguously üinsertions and deletions at particular positions is complex

More information

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

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

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

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ; Inheritance and Virtual Functions. Linked lists. CS427 Lecture 12.2, 11am, 26th March 2012 In today s class 1 Recall... Inheritance 2 3 4 Limitations of arrays 5 Linked lists 6 7 8 Further linked list

More information

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

Now consider the following situation after deleting three elements from the queue... Scheme of valuvation -1I Subject & Code : Data Structure and Application (15CS33) NOTE: ANSWER All FIVE QUESTIONS 1 Explain the diadvantage of linear queue and how it i olved in circular queue. Explain

More information

CP2 Revision. theme: dynamic datatypes & data structures

CP2 Revision. theme: dynamic datatypes & data structures CP2 Revision theme: dynamic datatypes & data structures structs can hold any combination of datatypes handled as single entity struct { }; ;

More information

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

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees Instructor: Krithika Venkataramani Semester 2, 2011-2012 1 Introduction to Linked Lists Each bead connected to the next through a

More information

Chapter 8 STRUCTURES IN C

Chapter 8 STRUCTURES IN C Chapter 8 STRUCTURES IN C 1 Structures Introduction Collections of related variables (aggregates) under one name Can contain variables of different data types Commonly used to define records to be stored

More information

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015 Solutions Q.1 a. What is a pointer? Explain how it is declared and initialized. (4) Different from other normal variables which can store values, pointers are special variables that can hold the address

More information

Chapter 20: Binary Trees

Chapter 20: Binary Trees Chapter 20: Binary Trees 20.1 Definition and Application of Binary Trees Definition and Application of Binary Trees Binary tree: a nonlinear linked list in which each node may point to 0, 1, or two other

More information

NCS 301 DATA STRUCTURE USING C

NCS 301 DATA STRUCTURE USING C NCS 301 DATA STRUCTURE USING C Unit-1 Part-4 Linked Lists Hammad Mashkoor Lari Assistant Professor Allenhouse Institute of Technology www.ncs301ds.wordpress.com Introduction List refers to linear collection

More information

Cpt S 122 Data Structures. Data Structures

Cpt S 122 Data Structures. Data Structures Cpt S 122 Data Structures Data Structures Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Self Referential Structures Dynamic Memory Allocation

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists MOUNA KACEM mouna@cs.wisc.edu Spring 2019 Linked Lists 2 Introduction Linked List Abstract Data Type SinglyLinkedList ArrayList Keep in Mind Introduction:

More information

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?

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? Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use? The heterogeneous linked list contains different data types in its nodes and we need a link

More information

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

A linked list grows as data is added to it. In a linked list each item is packaged into a node. Lesson 4 Data Structures What is a data structure? A data structure is a particular way of organizing data in a computer. A data structure that we have already encountered is the array. An array stores

More information

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

Data Structures (CS301) LAB

Data Structures (CS301) LAB Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Doubly Linked List practically using c++ and adding more functionality in it. Introduction to Singly

More information

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

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren CS32 Discussion Sec.on 1B Week 2 TA: Zhou Ren Agenda Copy Constructor Assignment Operator Overloading Linked Lists Copy Constructors - Motivation class School { public: }; School(const string &name); string

More information

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

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

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

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison Linked lists Comp Sci 1575 Data Structures Outline 1 2 3 4 5 Linked list Linked lists are of a linear collection of data elements, called nodes, each pointing to the next node Each node is composed of

More information

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

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list Linked Lists Introduction : Data can be organized and processed sequentially using an array, called a sequential list Problems with an array Array size is fixed Unsorted array: searching for an item is

More information

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

Pointers. Array. Solution to the data movement in sequential representation 1 LISTS Pointers Array sequential representation some operation can be very time-consuming (data movement) size of data must be predefined static storage allocation and deallocation Solution to the data

More information

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

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked Chapter 6 LISTS AND STRINGS 1. List Specifications 2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked 3. Strings 4. Application:

More information

Elementary Data Structures: Lists

Elementary Data Structures: Lists Elementary Data Structures: Lists CSE 2320 Algorithms and Data Structures Based on presentation by Vassilis Athitsos University of Texas at Arlington 1 Pointers What to Review Pointers and Memory : http://cslibrary.stanford.edu/102/pointersandmemory.pdf

More information

(Section : Computer Science)

(Section : Computer Science) (Section : Computer Science) 26. What will happen if we compile and execute the following program? static int count = 20; int main(void) { int i = 0; static int count = 6; while (i < count) { i++; count--;

More information

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

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

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

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S. Bowers 1 of 11 Doubly Linked Lists Each node has both a next and a prev pointer head \ v1 v2 v3 \ tail struct

More information

CS350: Data Structures. Doubly Linked Lists

CS350: Data Structures. Doubly Linked Lists Doubly Linked Lists James Moscola Department of Physical Sciences York College of Pennsylvania James Moscola Doubly Linked Lists Adds an additional pointer to a the list nodes that points to the previous

More information

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

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head Linked Lists An Alternative Collections Data Structure What Is A Linked List? A data structure using memory that expands and shrinks as needed Relies on identical nodes pointing or linked to other nodes

More information

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

Introduction to Computer Science II CS S-20 Linked Lists IV Introduction to Computer Science II CS112-2012S-20 Linked Lists IV David Galles Department of Computer Science University of San Francisco 20-0: Doubly Linked List Deleting from (and inserting into!) a

More information

Midterm Review. CS 211 Fall 2018

Midterm Review. CS 211 Fall 2018 Midterm Review CS 211 Fall 2018 BSB 250 Official Time: 10:00 am to 10:50 am If we can start a few minutes early, we will So try to arrive closer to 9:50am We will need to finish right at 10:50 since there

More information

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

Linked Lists. .. and other linked structures. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 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;

More information

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

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first Ch. 17: Linked Lists 17.1 Introduction to Linked Lists! A data structure representing a list! A series of nodes chained together in sequence CS 2308 Spring 2013 Jill Seaman - Each node points to one other

More information

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW

LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW LINKED LIST IMPLEMENTATION USING C LANGUAGE: A REVIEW Ekta Nehra Assistant Professor (Extn.), C.R.M jat college, Hisar, Haryana, (India) ABSTRACT This paper describes about linear data structure i.e. linked

More information

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

Linked List. ape hen dog cat fox. tail. head. count 5 Linked Lists Linked List L tail head count 5 ape hen dog cat fox Collection of nodes with a linear ordering Has pointers to the beginning and end nodes Each node points to the next node Final node points

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

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

Data Structure. Chapter 5 Trees (Part II) Angela Chih-Wei Tang. National Central University Jhongli, Taiwan Data Structure Chapter 5 Trees (Part II) Angela Chih-Wei Tang Department of Communication Engineering National Central University Jhongli, Taiwan 2010 Spring Threaded Binary Tree Problem: There are more

More information

Data Structure Series

Data Structure Series Data Structure Series This series is actually something I started back when I was part of the Sweet.Oblivion staff, but then some things happened and I was no longer able to complete it. So now, after

More information

Dynamic Data Structures (II)

Dynamic Data Structures (II) Lecture 23 Dynamic Data Structures (II) CptS 121 Summer 2016 Armen Abnousi Data Structure Data structures are different ways of organizing data in computer We design new data structures to make the programs

More information

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

AIM:- To write a C program to create a singly linked list implementation. SINGLY LINKED LIST AIM:- To write a C program to create a singly linked list implementation. ALGORITHM:- 1. Start the program. 2. Get the choice from the user. 3. If the choice is to add records, get the

More information

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

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology Lecture 7: Data Structures 1 Introduction: dynamic array Conventional array in C has fix number of elements Dynamic array is array with variable number of elements: actually a pointer and a variable indicating

More information

CSC212 Data Structure - Section FG

CSC212 Data Structure - Section FG CSC212 Data Structure - Section FG Lecture 10 The Bag and Sequence Classes with Linked Lists Instructor: Feng HU Department of Computer Science City College of New York @ Feng HU, 2016 1 Reviews: Node

More information

lecture09: Linked Lists

lecture09: Linked Lists lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

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

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) The queue ADT A queue is like a "natural" queue of elements. It is an ordered list in which all insertions occur at one end called

More information

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

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil Linked Lists Gaddis Ch. 17 CS 2308 :: Spring 2016 Molly O'Neil List ADT A list is an abstract data type representing an ordered sequence of values For example, both MP3 Player assignments have used lists:

More information

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

Advanced Linked Lists. Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Advanced Linked Lists Doubly Linked Lists Circular Linked Lists Multi- Linked Lists Review The singly linked list: consists of nodes linked in a single direction. access and traversals begin with the first

More information

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

Self-referential Structures and Linked List. Programming and Data Structure 1 Self-referential Structures and Linked List Programming and Data Structure 1 Linked List :: Basic Concepts A list refers to a set of items organized sequentially. An array is an example of a list. The

More information

Linked Lists. C Linked List

Linked Lists. C Linked List Linked Lists 1 A linked list is a data structure that uses a "chain" of node objects, connected by pointers, to organize a collection of user data values. Here's a fairly typical conceptual view of a doubly-linked

More information

// Pointer to the first thing in the list

// Pointer to the first thing in the list Linked Lists Dynamic variables combined with structs or classes can be linked together to form dynamic lists or other structures. We define a record (called a node) that has at least two members: next

More information

Data Structures and Algorithms for Engineers

Data Structures and Algorithms for Engineers 04-630 Data Structures and Algorithms for Engineers David Vernon Carnegie Mellon University Africa vernon@cmu.edu www.vernon.eu Data Structures and Algorithms for Engineers 1 Carnegie Mellon University

More information

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Darshan Institute of Engineering & Technology for Diploma studies Unit 4 Pointer A pointer is a variable that contains address or location of another variable. Pointer is a derived data type in C. Pointers contain memory address as their values, so they can also be used to

More information

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

Linked List. April 2, 2007 Programming and Data Structure 1 Linked List April 2, 2007 Programming and Data Structure 1 Introduction head A linked list is a data structure which can change during execution. Successive elements are connected by pointers. Last element

More information

CPSC 261 Midterm 2 Thursday March 17 th, 2016

CPSC 261 Midterm 2 Thursday March 17 th, 2016 CPSC 261 Midterm 2 Thursday March 17 th, 2016 [9] 1. Multiple choices [5] (a) Among the following terms, circle all of those that refer to a responsibility of a thread scheduler: Solution : Avoiding deadlocks

More information

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

ECE 2400 Computer Systems Programming, Fall 2018 PA2: List and Vector Data Structures School of Electrical and Computer Engineering Cornell University revision: 2018-09-25-13-37 1. Introduction The second programming assignment is designed to give you experience working with two important

More information

Introduction to Programming in C Department of Computer Science and Engineering

Introduction to Programming in C Department of Computer Science and Engineering Introduction to Programming in C Department of Computer Science and Engineering In this lecture, we will see slightly more advanced data type, then a singly link list. We will briefly go over one or two

More information

Ch. 17: Linked Lists. Introduction to Linked Lists

Ch. 17: Linked Lists. Introduction to Linked Lists Ch. 17: Linked Lists Part 1 CS 2308 Fall 2011 Jill Seaman Lecture 16 Using content from textbook slides: Starting Out with C++, Gaddis, Pearson/Addison-Wesley 1 Introduction to Linked Lists A data structure

More information

CS S-20 Linked Lists IV 1

CS S-20 Linked Lists IV 1 CS112-2012S-20 Linked Lists IV 1 20-0: Doubly Linked List Deleting from (and inserting into!) a linked can be challenging because you need to find the node before the node you are looking for Once you

More information

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

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Lists, Stacks and Queues in C CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4 Outline Structure Linked List Overview Implementation Stack Overview Implementation Queue Overview Implementation 2

More information

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

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island Dynamic Memory Management Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island 1 Dynamic Memory Allocation Dynamic memory allocation is used to

More information

Algorithms, Data Structures, and Problem Solving

Algorithms, Data Structures, and Problem Solving Algorithms, Data Structures, and Problem Solving Masoumeh Taromirad Hamlstad University DT4002, Fall 2016 Container Concepts containers store data container operations: insertion retrieval removal iteration

More information

Spring 2008 Data Structures (CS301) LAB

Spring 2008 Data Structures (CS301) LAB Spring 2008 Data Structures (CS301) LAB Objectives The objectives of this LAB are, o Enabling students to implement Singly Linked List practically using c++ and adding more functionality in it. o Enabling

More information

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

Come to the PASS workshop with your mock exam complete. During the workshop you can work with other students to review your work. It is most beneficial to you to write this mock midterm UNDER EXAM CONDITIONS. This means: Complete the mock final in 170 minutes. Work on your own. Keep your notes and textbook closed. Attempt every question.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT

More information

CMPT 225. Lecture 6 linked list

CMPT 225. Lecture 6 linked list CMPT 225 Lecture 6 linked list 1 Last Lecture Class documentation Linked lists and its operations 2 Learning Outcomes At the end of this lecture, a student will be able to: define one of the concrete data

More information

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

COMP26120: Linked List in C (2018/19) Lucas Cordeiro COMP26120: Linked List in C (2018/19) Lucas Cordeiro lucas.cordeiro@manchester.ac.uk Linked List Lucas Cordeiro (Formal Methods Group) lucas.cordeiro@manchester.ac.uk Office: 2.28 Office hours: 10-11 Tuesday,

More information

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

SINGLE LINKED LIST. Algorithm for allocating memory for the new node SRI CHANDRASEKHARENDRA SARASWATHI VISWA MAHAVIDYALAYA SCSVMV UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING Faculty: S.Gokulakrishnan AP/CSE SINGLE LINKED LIST Aim:- Write a C program to implement

More information