Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Linked Lists.
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
CSEB324 Data Structures & Algorithms
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Doubly-linked list library.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Chapter 17 Linked List.
Iterators CS 367 – Introduction to Data Structures.
Reference: Vinu V Das, Principles of Data Structures using C and C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
COMP x1 Computing 2 C Outside the Style Guide, Linked Lists and Function Pointers 1.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
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.
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.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
© 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.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Lists and Strings Manolis Koubarakis Data Structures and Programming Techniques 1.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Linked List :: Basic Concepts
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Lectures linked lists Chapter 6 of textbook
Data Structure and Algorithms
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Linked lists.
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 insertion Head. Linked list insertion Head.
Programmazione I a.a. 2017/2018.
Threaded Trees Binary trees have a lot of wasted space: the leaf nodes each have 2 null pointers We can use these pointers to help us in inorder traversals.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Data Structures and Programming Techniques
Linked List.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
General List.
Linked Lists.
Linked lists.
Presentation transcript:

Linked Lists

A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the list. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the list. The last node in the list must have pointer field value NULL and the list is given by a pointer to the first node. is given by a pointer to the first node. No header node. Here are the declarations. typedef struct node { typedef struct node { int info; int info; struct node* next; struct node* next; } Node; } Node; Node* q; Node* q;

Suppose we had a linked list containing the four values 2, 1, 7, 5 in exactly that order and that q points to the first node in the list. Suppose we had a linked list containing the four values 2, 1, 7, 5 in exactly that order and that q points to the first node in the list. How would we name the info field of the node containing the value 7? the value 7? The answer is q->next->next->info If we were using the parentheses version of struct references, we would have to write (*(*(*q).next).next).info (*(*(*q).next).next).info

Example Linked List Code Example Linked List Code We now write a code segment that reads in a sequence of positive integers terminated by an entry of 0 reads in a sequence of positive integers terminated by an entry of 0 creates a linked list of the values in the order they were entered. creates a linked list of the values in the order they were entered. prints the values from first to last. prints the values from first to last. We assume that at least one positive integer is entered. Note that this code depends on the fact that the next field of the last node has value NULL.

int hold; int hold; Node *first = NULL; Node *first = NULL; Node *last, *tmp; Node *last, *tmp; printf("Enter the first " printf("Enter the first " "positive number of " "positive number of " "your list: "); "your list: "); scanf("%d",&hold); first = malloc(sizeof(Node)); first = malloc(sizeof(Node)); first->info = hold; first->info = hold; last = first; last = first;

printf("Enter the remaining " printf("Enter the remaining " "positive numbers," "positive numbers," "terminated by a " "terminated by a " "zero: "); "zero: "); scanf("%d",&hold); while(hold != 0) { while(hold != 0) { last->next = malloc(sizeof(Node)); last->next = malloc(sizeof(Node)); last = last->next; last = last->next; last->info = hold; last->info = hold; scanf("%d",&hold); scanf("%d",&hold); } /* Last node: NULL next field */ last->next = NULL; last->next = NULL;

/* print entries */ tmp = first; tmp = first; while (tmp != NULL){ while (tmp != NULL){ printf("%d ",tmp->info); printf("%d ",tmp->info); tmp = tmp->next; tmp = tmp->next; } printf("\n"); Basic list traversal technique

More Linked List Tasks More Linked List Tasks Suppose we have a linked list given by a pointer first to the first node of the list and that q is a pointer to a node in the list. node of the list and that q is a pointer to a node in the list. We want to write code segments to do the following tasks: 1. insert a node with info field value k just after q; 2. insert a node with info field value k just before q; 3. delete the node pointed at by q.

When solving problems like this it is very important to draw pictures to guide your code creation. When solving problems like this it is very important to draw pictures to guide your code creation. You should You should - write the code for a generic picture - write the code for a generic picture - check to see if the code works for special cases (first & last nodes) - check to see if the code works for special cases (first & last nodes) - modify code to take care of special cases, if needed.

Problem 1: insert a node with info field value k just after q Node * tmp = malloc(sizeof(Node)); tmp->info = k; tmp->info = k; tmp->next = q->next; tmp->next = q->next; q->next = tmp;

Problem 1: insert a node with info field value k just after q Node * tmp = malloc(sizeof(Node)); Node * tmp = malloc(sizeof(Node)); tmp->info = k; tmp->info = k; tmp->next = q->next; tmp->next = q->next; q->next = tmp; Question: does this still work if q is the first or last node in the list?

Problem 2: insert a node with info field value k just before q; Generic case: we need to find the node directly before q and insert after that node. Node *tmp = Node *tmp = malloc(sizeof(Node)); malloc(sizeof(Node)); Node* prev; Node* prev; tmp->info = k; tmp->info = k; tmp->next = q; tmp->next = q;

prev = first; prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = tmp; prev->next = tmp;

Node *prev = first; Node *prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = tmp; prev->next = tmp;

Node *prev = first; Node *prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = tmp; prev->next = tmp;

Node *prev = first; Node *prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = tmp; prev->next = tmp;

The above code does not work if q = = first, because there is no previous node! So we must treat that situation as a special case. previous node! So we must treat that situation as a special case. In this case we must do the following:

The above code does not work if q = = first, because there is no previous node! So we must treat that situation as a special case. previous node! So we must treat that situation as a special case. In this case we must do the following: first = tmp; first = tmp;

The final code is Node *prev; Node *prev; Node *tmp = malloc(sizeof(Node)); Node *tmp = malloc(sizeof(Node)); tmp->info = k; tmp->info = k; tmp->next = q; tmp->next = q; if (q == first) if (q == first) first = tmp; first = tmp; else { else { prev = first; prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = tmp; prev->next = tmp; } Note that the above works even if q is the last node in the list.

Problem 3: delete the node pointed at by q First (and most obvious) approach: find the node just before q, First (and most obvious) approach: find the node just before q, make its next field point to the node following q, then free q. make its next field point to the node following q, then free q. The first node is a special case. It is assumed that q != NULL. Node *prev; Node *prev; if (q == first) if (q == first) first = first->next; first = first->next;

Problem 3: delete the node pointed at by q First (and most obvious) approach: find the node just before q, First (and most obvious) approach: find the node just before q, make its next field point to the node following q, then free q. make its next field point to the node following q, then free q. The first node is a special case. It is assumed that q != NULL. Node *prev; Node *prev; if (q == first) if (q == first) first = first->next; first = first->next;

else { else { prev = first; prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = q->next; prev->next = q->next; } free(q);

else { else { prev = first; prev = first; while( prev->next != q) while( prev->next != q) prev = prev->next; prev = prev->next; prev->next = q->next; prev->next = q->next; } free(q);

Second approach: if q is the last node (and not the first node) the code of the else statement is necessary. if q is the last node (and not the first node) the code of the else statement is necessary. If not, a little bit of cleverness can be done: copy the info field of the node following q into the node q and delete the node following q (which is easy):

Node *tmp; Node *tmp; if (q == first) if (q == first) first = first->next; first = first->next; else if (q->next == NULL) { else if (q->next == NULL) { tmp = first; tmp = first; while( tmp->next != q) while( tmp->next != q) tmp = tmp->next; tmp = tmp->next; tmp->next = q->next; tmp->next = q->next; } else { else { q->info = q->info = q->next->info; q->next->info; tmp = q; tmp = q; q = q->next; q = q->next; tmp->next = q->next; tmp->next = q->next; } free(q);

Homework 1.Complete the code for a function that replaces each occurrence of value x by value y in a linked list with first node pointer p. Assume the info type is int. void replaceLinkedList(Node* p, int x, int y) 2. Complete the code for a function that inserts a node with info value x before each node with info value y in a linked list with first node pointer p. Assume the info type is int. The function returns a pointer to the first node of the modified list. Node* InsertBefore(Node* p, int x, int y,) first node pointer p. Assume the info type is int. The function returns a pointer to the first node of the modified list. Node* InsertBefore(Node* p, int x, int y,) Utility function: Node * MakeNode(int newVal, Node* successor) { Node * tmp = malloc(sizeof(Node)); tmp->info = newVal; tmp->info = newVal; tmp->next = successor; tmp->next = successor; return tmp; return tmp;}