Linked List Variations

Slides:



Advertisements
Similar presentations
Chapter 3: Linked List Tutor: Angie Hui
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures ADT List
C and Data Structures Baojian Hua
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.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Doubly-linked list library.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked Lists... An introduction to creating dynamic data structures.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
2/21/20161 List Operations Advanced Programming Ananda Gunawardena.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Linked List Variations
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
Lists.
Chapter 16-2 Linked Structures
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Programming Linked Lists.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Data Structures and Algorithms
Chapter 17: Linked Lists.
LINKED LISTS.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
BY PROF. IRSHAD AHMAD LONE.
Presentation transcript:

Linked List Variations COP 3502

Linked List Practice Problem Write a recursive function that deletes every other node in the linked list pointed to by the input parameter head. (Specifically, the 2nd 4th 6th etc. nodes are deleted) From Fall 2009 Foundation Exam void delEveryOther(node* head){ if (head == NULL || head->next == NULL) return; node *temp = head->next; head->next = temp->next; free(temp); delEveryOther(head->next); }

Linked List Practice Problem Write an iterative function that deletes every other node in the linked list pointed to by the input parameter head. (Specifically, the 2nd 4th 6th etc. nodes are deleted) From Fall 2009 Foundation Exam void delEveryOther(struct node *head) { struct node* curr = head; while(curr != NULL && curr->next != NULL) { struct ll* temp = curr->next; curr->next = temp->next; curr=temp->next; free(temp); }

Linked List Variations There are 3 basic types of linked lists: Singly-linked lists Doubly-Linked Lists Circularly-Linked Lists We can also have a linked lists of linked lists

Circularly Linked Lists Singly Linked List: Circularly-Linked List head 1 2 3 4 NULL head 1 2 3 4

Circularly Linked Lists Why use a Circularly Linked List? It may be a natural option for lists that are naturally circular, such as the corners of a polygon OR you may wish to have a queue, where you want easy access to the front and end of your list. For this reason, most circularly linked lists are implemented as follows: With a pointer to the end of the list (tail), we can easily get to the front of the list (tail->next) tail 4 1 2 3

Circularly Linked List Consider inserting to the front of a circular linked list: The first node is the node next to the tail node We want to insert the new node between the tail node and the first node. We want to insert here tail front 4 1 2 3

Circularly Linked List Steps: Create a new node in memory, set its data to val Make the node point to itself if tail is empty, then return this node, it’s the only one in the list If it’s not the only node, then it’s next is tail->next and tail->next should now point to the new node. temp 6 We want to insert here tail front 4 1 2 3

Circularly Linked List Steps: Create a new node in memory, set its data to val Make the node point to itself if tail is empty, then return this node, it’s the only one in the list If it’s not the only node, then it’s next is tail->next and tail->next should now point to the new node. Resulting List: tail front 4 6 1 2 3

Circularly Linked List typedef struct node { int data; node *next; } node; node* AddFront(node* tail, int val) { // Create the new node // Set the new node’s next to itself (circular!) // If the list is empty, return new node // Set our new node’s next to the front // Set tail’s next to our new node // Return the end of the list }

Circularly Linked List typedef struct node { int data; node *next; } node; node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) // If the list is empty, return new node // Set our new node’s next to the front // Set tail’s next to our new node // Return the end of the list }

Circularly Linked List typedef struct node { int data; node *next; } node; node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node // Set our new node’s next to the front // Set tail’s next to our new node // Return the end of the list }

Circularly Linked List node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node if (tail == NULL) return temp; // Set our new node’s next to the front // Set tail’s next to our new node // Return the end of the list }

Circularly Linked List node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node if (tail == NULL) return temp; // Set our new node’s next to the front temp->next = tail->next; // Set tail’s next to our new node // Return the end of the list }

Circularly Linked List node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node if (tail == NULL) return temp; // Set our new node’s next to the front temp->next = tail->next; // Set tail’s next to our new node tail->next = temp; // Return the end of the list }

Circularly Linked List node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node if (tail == NULL) return temp; // Set our new node’s next to the front temp->next = tail->next; // Set tail’s next to our new node tail->next = temp; // Return the end of the list }

Circularly Linked List node* AddFront(node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node if (tail == NULL) return temp; // Set our new node’s next to the front temp->next = tail->next; // Set tail’s next to our new node tail->next = temp; // Return the end of the list return tail; }

Circularly Linked List Inserting a node at the End of a Circular Linked List The new node will be placed just after the tail node (which is the last node in the list) So again the new node will be inserted between the tail node and the front node. The only difference with AddFront, is that now we need to change where tail points after we add the node. That’s the only difference, so the code is pretty similar. We want to insert here tail front 4 1 2 3

Circularly Linked List typedef struct node { int data; node *next; } node; struct node* AddEnd(struct node* tail, int val) { // Create the new node node *temp = (node*)malloc(sizeof(node)); temp->data = val; // Set the new node’s next to itself (circular!) temp->next = temp; // If the list is empty, return new node if (tail == NULL) return temp; // Set our new node’s next to the front temp->next = tail->next; // Set tail’s next to our new node tail->next = temp; // Return the new end of the list return temp; } The only line of code that’s different

Circularly Linked List Deleting the First Node in a Circular Linked List The first node can be deleted by simply replacing the next field of tail node with the next filed of the first node: temp = tail->next; // This is the front tail->next = temp->next; // This is the node after front free(temp); tail front temp 4 1 2 3

Circularly Linked List Deleting the Last Node in a Circular Linked List This is a little more complicated The list has to be traversed to reach the second to last node. This had to become the tail node, and its next field has to point to the first node. curr tail tail 4 1 2 3

Doubly Linked List Doubly Linked List Node: DoublyLinkedList: typedef struct node { int data; node *next; node *prev; } node; Doubly Linked List Node: DoublyLinkedList: Each node in the list contains a reference to both: the node which immediately precedes it AND to the node which follows it in the list. 1 prev data next head 1 2 3 4 prev data next

Doubly Linked List DoublyLinkedList: Advantages: typedef struct node { int data; node *next; node *prev; } node; DoublyLinkedList: Advantages: Allows searching in BOTH directions Insertion and Deletion can be easily done with a single pointer. head 1 2 3 4 prev data next

Doubly Linked List Circular Doubly Linked List typedef struct node { int data; node *next; node *prev; } node; head 1 2 3 4 prev data next Circular Doubly Linked List Same as a circular doubly linked list BUT the nodes in the list are doubly linked, so the last node connects to the front AND the first node connects to the last.

Doubly Linked List - Insertion temp We want to insert here curr 3 head 1 2 4 5 Code: temp->prev = curr; temp->next = curr->next; curr->next->prev = temp; curr->next = temp; Disadvantage of Doubly Linked Lists: extra space for extra link fields maintaining the extra link during insertion and deletion

Doubly Linked List - Deletion curr head 1 2 3 4 Code: curr->prev->next = curr->next; curr->next->prev = curr->prev; free(curr); (Assuming curr->prev and curr->next are NOT NULL)

A Linked List of Linked Lists Linked Lists can be part of more complicated data structures. Consider the situation where we have a linked list of musical artists It might also make sense that each artist has a linked list of songs (or albums) stored

head cdList cdList cdList cdList Beat It One Cherish Thriller Michael Jackson U2 Madonna Weezer cdList cdList cdList cdList Beat It One Cherish Buddy Holly Thriller Sweetest Thing Like a Prayer Say it aint so Billy Jean Memories struct ArtistNode { char first[30]; char last[30]; struct CDNode *cdList; struct ArtistNode *next; } ; struct CDNode { char title[50]; struct CDNode *next; } ;

Practice Problem typedef struct node { int data; node *next; } node; Write a function which accepts a linear linked list J and converts it to a circular linked list. Note this means: J is a pointer to the front of the list. node *convert (node *J) { }

Practice Problem typedef struct node { int data; node *next; } node; Write a function which accepts a linear linked list J and converts it to a circular linked list. (Question on the Summer 2010 Foundation Exam) Note this means: J is a pointer to the front of the list. node *convert (node *J) { if (J == NULL) return NULL; node *curr = J; // 1 pt while ( curr->next != NULL) curr = curr->next; curr->next = J; return curr; }