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.

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

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.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
CS 367 – Introduction to Data Structures
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.
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
LIST PROCESSING.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
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.
Review Learn about linked lists
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Linked Lists. Outline Why linked lists? Linked lists basics Implementation Basic primitives ­Searching ­Inserting ­Deleting.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
Linked List C and Data Structures Baojian Hua
CS Data Structures Chapter 4 Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 Today’s Material List ADT –Definition List ADT Implementation: 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.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
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.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
Doubly Linked List Review - We are writing this code
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures and Algorithms
Java collections library
CSC172 Data Structures Linked Lists (C version)
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 Lists with Tail Pointers
Lists.
Doubly linked lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Lists.
Linked Lists Chris Wright Winter 2006.
Circularly Linked Lists
Linked Lists: Implementation of Queue & Deque
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Review & Lab assignments
Data Structures and Algorithms
FIFO Queues CSE 2320 – Algorithms and Data Structures Alexandra Stefan
General List.
Presentation transcript:

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 a pointer to the next linked list element. It may also contain a pointer to the previous linked list element. In this case, we call it a doubly linked list element.

Linked List (cont.)

Linked List Implementation struct _LLElement { int data; _LLElement *next, *prev; }; typedef struct _LLElement LLElement;

Linked List Implementaion (cont.) LLElement *LLElement_new( int data ) used to create a new linked list element with the given data. Usage: LLElement *element = LLElement_new(10);

Linked List Implementation (cont.) LLElement *LLElement_new( int data ) { LLElement *result = (LLElement *)malloc(sizeof(LLElement)); if (result != NULL) { result->data = data; result->next = NULL; result->prev = NULL; } return result; }

Linked List Implementaion (cont.) void LLElement_insert_after( LLElement *position, LLElement *e) inserts a linked list element e after position.

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) void LLElement_insert_after( LLElement *position, LLElement *e) { e->prev = position; e->next = position->next; if (position->next != NULL) position->next->prev = e; position->next = e; }

Linked List Implementaion (cont.) void LLElement_insert_before( LLElement *position, LLElement *e) inserts the linked list element e before position.

Linked List Implementation (cont.) position e

Linked List Implementation (cont.) e position

Linked List Implementation (cont.) e position

Linked List Implementation (cont.) e position

Linked List Implementation (cont.) e position

Linked List Implementation (cont.) this e

Linked List Implementation (cont.) void LLElement_insert_before( LLElement *position, LLElement *e) { e->next = position; e->prev = position->prev; if (prev != NULL) position->prev->next = e; position->prev = e; }

Linked List Implementaion (cont.) void LLElement_remove(LLElement *e) removes the linked list element e from the chain. In effect, it links es prev with next.

Linked List Implementation (cont.) this

Linked List Implementation (cont.) e

Linked List Implementation (cont.) e

Linked List Implementation (cont.) e

Linked List Implementation (cont.) e

Linked List Implementation (cont.) e

Linked List Implementation (cont.) void LLElement_remove(LLElement *e) { if (e->prev != NULL) e->prev->next = e->next; if (e->next != NULL) e->next->prev = e->prev; e->prev = NULL; e->next = NULL; }

Linked List Elements Efficiency? Space: O(1) Running Time InsertBeforeO(1) InsertAfterO(1) RemoveO(1)

Implementing List with Linked List To simplify implementation, we will use two dummy elements to act as the first element and the last element of the list. These two dummies do not hold real data. Elements between them do.

Implementing List with Linked List (cont.) ??? head ??? tail

Implementing List with Linked List (cont.) typedef struct { LLElement *head, *tail; int size; } LinkedList; size linked list

Implementing List with Linked List (cont.) void LinkedList_init(LinkedList *list) Initializes a linked list. Create the head element. Create the tail element. Link them together. Set the size to 0. void LinkedList_init(LinkedList *list) { list->head = LLElement_new(0); list->tail = LLElement_new(0); list->head->next = tail; list->tail->prev = head; list->size = 0; } 0 head 0 tail

Implementing List with Linked List (cont.) int LinkedList_get(LinkedList *list, int i) { int j; LLElement *ptr = list->head->next; for(j=0;j<i;j++) ptr = ptr->next; return ptr->data; }

Implementing List with Linked List (cont.) void LinkedList_set(LinkedList *list, int i, int x) { int j; LLElement *ptr = list->head->next; for(j=0;j<i;j++) ptr = ptr->next; ptr->data = x; }

Implementing List with Linked List (cont.) void LinkedList_find(LinkedList *list, int x) { int result = 0; LLElement *ptr = list->head->next; while (ptr != list->tail && ptr->data != x) { ptr = ptr->next; result++; } if (ptr == tail) return -1; else return result; }

Implementing List with Linked List (cont.) void LinkedList_insert(LinkedList *list, int i, int x) { LLElement *ptr = list->head; int j; for(j=0;j<i-1;j++) ptr = ptr->next; LLElement_insert_after(ptr, LLElement_new(x)); list->size++; }

Implementing List with Linked List (cont.) void LinkedList_remove(LinkedList *list, int i) { LLElement *ptr = head->next; int j; for(j=0;j<i;j++) ptr = ptr->next; LLElement_remove(ptr); free(ptr); list->size--; }

Linked List Implementations Efficiency Space: O(n) Running Time: Get(i)O(i) = O(n) Set(x,i)O(i) = O(n) Find(x)O(n) Insert(x,i)O(i) = O(n) Remove(x,i)O(i) = O(n)

Linked List Implementations Efficiency (cont.) Notice that how you specify the operations can have a lot of impact on the implementations efficiency. We can insert a linked list element into a linked list in O(1) if you know the element just before or after it. But, if we are given the position i to insert, it takes O(i) time just to get there.

Linked List Implementations Efficiency (cont.) How is linked list better than array? The space is O(n) at all time. More efficient use of memory. It performs some operation faster. Insert(x, 0) Remove(0) Both are O(1) in LinkedList, but O(n) in ArrayList. So, a queue implemented by a linked list takes O(1) per operation.