Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked List Variations
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Linked Lists... An introduction to creating dynamic data structures.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Exercise 11 Dynamic allocation, structures, linked lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Linked Lists. 2 Implementing a student package We want to create (part of) a course- management program. We need to – Maintain a list of the participating.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Linked List and Namespace ~ include dynamic objects.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures — Lists and Trees CS-2301, B-Term Data Structures — Lists and Trees CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
ENEE150 – 0102 ANDREW GOFFIN Linked List/Project 3.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Recursive Objects (Part 2) 1. Adding to the front of the list  adding to the front of the list  t.addFirst('z') or t.add(0, 'z') 2 'a' 'x' LinkedList.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
LINKED LISTS.
ECE Application Programming
Linked List :: Basic Concepts
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Problems with dynamic arrays
Linked lists.
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists.
Chapter 16-2 Linked Structures
Arrays and Linked Lists
Programming Linked Lists.
- Dynamic Allocation - Linked lists
Chapter 16 Linked Structures
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Exercise 12 Linked lists.
Linked Lists.
Linked lists.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Linked Lists.
Presentation transcript:

Programming Linked Lists

Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays

Array Recap Allocated as one block in memory Convenient to declare  int arr[100]; Easy access to a single element  arr[i] Size is fixed  Wasting memory because of “large enough” allocation Adding / Deleting elements  need to shift large parts of the array

Linked Lists Dynamic Efficient use of memory  Allocate just as much as needed Easy insertion in front Local deletion Hard to get to any particular element

Graphic Description Array Linked List  Each link (node) contains data and a pointer to the next link

Linked Lists A list is a chain of nodes. typedef struct node_type { struct node_type* next; } Node; NULL Data Next Data Next Data Next head

Linked Lists Operation Insert  front, back, middle Remove Find Size

Insertion (front) Data Next head 1. create the new node 2. have it point to the first element 3. change the head of the list Data Next Data Next NULL Data Next

Insertion (back) Data Next head 1. create the new node 2. locate the last element in the list 3. have the last element point to the new one Data Next Data Next NULL last NULL Data Next

Insertion (middle) Data Next head NULL 1. create the new node 2. locate the insertion point 3. new_item->next = curr->next 4. curr->next = new_item Data Next Data Next curr new_item NULL Data Next

Delete Node Data Next head 1. find node to remove 2. have the previous node point to the one after the node to be removed 3. remove the node Data Next Data Next Data Next NULL remove

Iterating Data Next head Data Next Data Next Data Next 1. start at the beginning 2. advance your iterator one node at a time iter = iter->next 3. unti the end is reached iter NULL iter

Length Count the number of nodes in a list int length(Node *head) { int count = 0; while (head != NULL) { count++; head = head->next; } return count; }

Length (recursive) Count the number of nodes in a list int length(Node *head) { if (head == NULL) return 0; return length(head->next) + 1; }

Using Length int main(void) { Node *head = build_list(); int length = length(head); printf("The length of the list is: %d\n", length); free_list(head); return 0; }

Course Management System Maintain a list of students  Keep their ID, name, grade etc. Allow for adding / removing a student Find a student in the list Produce Reports  Average grade

Storing a Collection of Students Use an array of student structures There are problems with this –  we must allocate a big-enough array before accepting students (how do we know what’s big enough?)  How shall we remove students from the list without creating “holes”?  How can we maintain the list sorted by grade?  Insertion and deletion may be problematic

Linking Students Use a linked list Define a student node typedef struct student { char id[ID_LENGTH]; char name[NAME_LENGTH]; int grade; /* A pointer to the next node in the list */ struct student *next; } Student;

Exercise Download find_student_ex.c from the tirgul home pagefind_student_ex.c Implement Student* find_student(const Student *head, const char* id) find_student searches for a student with a given id. It returns a pointer to the student if found, otherwise it returns NULL.

Solution /* find a student whose id matches the given id */ Student* find_student(Student *head, char *id) { while (head != NULL) /* go over all the list */ { if (strcmp(head->id, id) == 0) /* same id */ return head; head = head->next; } /* If we're here, we didn't find it */ return NULL; }

Adding students Usually when using linked lists we don’t know how many elements will be in the list Therefore we would like to be able to dynamically allocate new elements when the need arises A possible implementation follows…

Creating a New Student Student* new_student(char* name, char* id, int grade) { Student *std =(Student*)malloc(sizeof(Student)); if (std != NULL) { strcpy(std->name, name); strcpy(std->id, id); std->grade = grade; std->next = NULL; } return std; }

Add in Front Student* add_front(Student *head, Student *std) { std->next = head; return std; } int main(void) { Student *std_list, *std;... std = new_student(...); std_list = add_front(std_list, std);... return 0; }

Sorted Add Adding a student to a list can be done in such a way that the list remains sorted by grade We will implement this in a separate function

Adding a student - begining … Head

Adding a student – mid/end … Head Insert new item: PreviousNext

Student *add_student(Student *head, Student *to_add) { Student *curr_std, *prev_std = NULL; if (head == NULL) return to_add; if (to_add->grade > head->grade) { to_add->next = head; return to_add; } curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; } handle empty list handle beginning the rest

Adding a student – beginning if (head == NULL) return to_add; if (to_add->grade > head->grade) { to_add->next = head; return to_add; } … to_add 100 head

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Adding a student – mid / end curr_std = head; while (curr_std != NULL && to_add->grade grade) { prev_std = curr_std; curr_std = curr_std->next; } prev_std->next = to_add; to_add->next = curr_std; return head; head to_add 75 curr_stdprev_std

Removing a student We would like to be able to remove a student by her/his ID. The function that performs this is remove_student

… Head PreviousCurrent Removing a student - reminder

Removing a student – beginning if (head == NULL) return head; cur = head; if (strcmp(cur->id, id) == 0) { head = head->next; free(cur); return head; } head cur … ID 14525

Removing a student – mid list head … ID cur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

Removing a student – mid list head … ID prevcur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

Removing a student – mid list head … ID prevcur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

Removing a student – mid list head … ID prevcur while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head;

Removing a student – mid list while (cur != NULL && strcmp(cur->id, id) != 0) { prev = cur; cur = cur->next; } if (cur != NULL) { prev->next = cur->next; free(cur); } return head; head … ID prevcur

Deallocating all students void free_list(Student *head) { Student *temp = head; while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students NULL headtemp while (head != NULL) { temp = head; head = head->next; free(temp); }

Deallocating students void free_list(Student *head) { if (head == NULL) return; free_list(head->next); free(head); }

Exercise Use change_grade_ex.c and implement the change_grade function.change_grade_ex.c The function takes as input the head of the list, the ID whose grade we’d like to change, and the new grade Hint – Create a new student with the same name and ID as the old one, but with the new grade. Remove the old student from the list and add the new one using the existing functions

solution Student* find_student(Student* head, char* id) { while (head != NULL && strcmp(head->id, id) != 0) { head = head->next; } return head; } Student* change_grade(Student *head, char* id, int new_grade) { Student *std = find_student(head, id); std = new_student(std->name, id, new_grade); head = remove_student(head, id); return add_student(head, std); }