Linked List C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
JAVA & Linked List Implementation
C and Data Structures Baojian Hua
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.
LIST PROCESSING.
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.
Data Structure & Abstract Data Type
C Module System C and Data Structures Baojian Hua
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Extensible Array C and Data Structures Baojian Hua
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
Graph C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Hash Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Hash C and Data Structure Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
Hash C and Data Structure Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
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 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
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.
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.
CSE 1342 Programming Concepts
Linked List :: Basic Concepts
UNIT-3 LINKED LIST.
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Chapter 18: Linked Lists.
Discrete Mathematics and
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Data Structures and Algorithms
Presentation transcript:

Linked List C and Data Structures Baojian Hua

Recap The extensible array-based implementation of linear list: may be too slow insert or delete operations involve data movement may be too space waste only a small portion of the allocated space is filled with data

Abstract Data Types in C // in “list.h” #ifndef LIST_H #define LIST_H typedef void *poly; typedef struct list *list; list new (); int length (list l); poly nth (list l, int n); void insert (list l, poly x, int i); poly delete (list l, int i); void foreach (list l, void (*f)(poly)); #endif

Implementation Using Linked List Linked list is a self-reference structure: to simplify operations, we add a unique head node “ head ” “ head ” does not belong to the list may hold meta information of the list head …

Linked List-based Implementation // Turn the above figure in C, we have: // in file “linkedList.c” #include #include “list.h” struct list { poly data; list next; }; data next data next data next head …

Operation: “ new ” // “new” returns an empty list, which consists of // a single head node. list new () { list l = malloc (sizeof (*l)); l->data = NULL; l->next = NULL; return l; } /\ l

Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==0

Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==1

Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==2

Operation: “ length ” int length (list l) { list p = l->next; int n = 0; while (p) { p = p->next; n++; } return n; } data next data next data next l … p n==3

Operation: “ nth ” poly nth (list l, int n) { list p = l->next; int i = 0; if (n =length(l)) error (“invalid index”); while (i!=n) { p = p->next; i++; } return p; }

Operation: “ nth ” data next data next data next l … n==2 p i==0 data next data next data next l … n==2 p i==1 data next data next data next l … n==2 p i==2

Operation: “ insert ” void insert (list l, poly x, int n) { // 1. change the “next” field of pointer x; // 2. change the “next” field of element (n-1) … } data next data next data next l … n==2 data next x we ’ d search this pointer p

Operation: “ insert ” void insert (list l, poly x, int n) { list p; if (n length(l)) error (“invalid index”); // search pointer p points to position n-1 if (n==0) p = l; else p = nth (l, n-1);

Operation: “ insert ” list temp = malloc (sizeof (*temp)); temp->data = x; // 1. change the “next” field of x temp->next = p->next; // 2. change the “next” field of p p->next = temp; return; }

Operation: “ delete ” poly delete (list l, int n) { // The key step is to search pointer p // Leave this as exercise. … } data next data next data next l … n==2 we ’ d search this pointer p

Operation: “ foreach ” void foreach (list l, void (*f)(poly)) { list p = l->next; while (p) { f (p->data); p = p->next; } data next data next data next l …

Linked List Summary Linked list: better space usage---no waste good time complexity insert or delete take linear time but have to also search the data sequential, :-( Can be further generalized circular linked list doubly linked list doubly circular linked list

Circular Linked List All the pointers forms a circle Note that the first node has two fields head: points to the head of the list tail: points to the tail of the list head tail data next data next data next l

Circular Linked List--- Implementation // in file “clist.c” struct list { struct node *head; struct node *tail; }; struct node { poly data; struct node *next; } head tail data next data next data next l

Linear List Application#1: Polynomials Polynomials: where ci  R and n  Nat uniquely determined by a linear list: For this representation, all the list operations apply

Linear List Application: Polynomials Space waste: Consider this: items with 3 non-zeros A refined representation: ci<>0 for 0<=i<=m Ex:

Polynomial ADT: Interface Abstract data type: polyn represent the polynomial data type operations: polyn new (); // an empty polyn polyn add (polyn p1, polyn p2); real value (polyn p, real x0); // p(x0) polyn mult (polyn p1, polyn p2); // add an item c*x^n, which does not appear in p void insert (polyn p, real c, nat n);

Polynomial ADT in C: Interface // in file “polyn.h” #ifndef POLYN_H #define POLYN_H typedef struct polyn *polyn; polyn new (); polyn add (polyn p1, polyn p2); real value (polyn p, real x0); polyn mult (polyn p1, polyn p2); void insert (polyn p, real c, nat n); #endif

Polynomial ADT in C: Implementation // in file “polyn.c” #include “linkedList.h” #include “polyn.h” struct polyn { linkedList coefExps; }; // where “coefExps” is a list of tuples: (c, n) // one way to read “list coefExps” is: // list > coefExps // However, C does not support this style of // declaration… :-(

Operation: “ new ” polyn new () { polyn p = malloc (sizeof (*p)); // use a linked list internally p->coefExps = newLinkedList (); return p; }

Change to the Head #include #include “linkedList.h” #include “polyn.h” struct polyn { linkedList coefExps; };

Operation: “ insert ” void insert (polyn p, real c, nat n) { // could we use “double” and “int”, instead of // “real” and “nat”? tuple t = newTuple (c, n); linkedListInsertAtTail (p->coefExps, t); return; } // Leave other functions as assignments

Change to the Head #include #include “linkedList.h” #include “tuple.h” #include “polyn.h” struct polyn { linkedList coefExps; };

Linear List Application#2: Dictionary Dictionay: where ki are keys and vi are value all ki are comparable and distinct How can dict ’ be represented in computers? many ideas (we ’ d discuss some in future) for now, we make use of a linear list

Dictionary ADT: Interface Abstract data type: dict represent the dictionary data type operations: dict new (); // an empty dict void insert (dict d, poly key, poly value); poly lookup (dict d, poly key); void delete (dict d, poly key);

“ dict ” ADT in C: Interface // in file “dict.h” #ifndef DICT_H #define DICT_H typedef struct dict *dict; dict new (); void insert (dict d, poly key, poly value); poly lookup (dict d, poly key); void delete (dict d, poly key); #endif

“ dict ” ADT in C: Implementation // in file “dict.c” #include “linkedList.h” #include “dict.h” struct dict { linkedList l; };

Operations: “ new ” dict new () { dict d = malloc (sizeof (*d)); d->l = newLinkedList (); return d; }

Operations: “ insert ” void insert (dict d, poly key, poly value) { tuple t = newTuple (key, value); linkedListInsertAtHead (d->l, t); return; } // Leave other functions as programming // assignments.