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.

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
EENG212 Algorithms and Data Structures
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
C and Data Structures Baojian Hua
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
Double Linked List Operations Dr. David Tsai 2010/4/12.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
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.
CSCE 3110 Data Structures & Algorithm Analysis
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Linked List C and Data Structures Baojian Hua
Doubly Linked List. Contents  Linked list  Doubly linked list  Structure  Insertion of node  Deletion of node  Traversing of node  Application.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Introduction to Data Structure
Introduction to Data Structures Systems Programming.
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.
Chapter 4 (cont.) Additional Lists Operations Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
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.
Algorithms and Data Structures
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 ARRAYS AND STRUCTURES. 2 Arrays Array: a set of index and value data structure For each index, there is a value associated with that index. representation.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Programming Application in Civil Engineering
Programming Circular Linked List.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures 7th Week
Abstract Data Types Sparse Matrices CSCI 240
Programmazione I a.a. 2017/2018.
Algorithms and Data Structures
Reminder: Array Representation In C++
Linked Lists Chris Wright Winter 2006.
Chapter 16-2 Linked Structures
Reminder: Array Representation In C++
EENG 212 ALGORITHMS And DATA STRUCTURES
Review & Lab assignments
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Matrices An appeaser is one who feeds a crocodile—hoping it will eat him last. Winston Churchhill.
Reminder: Array Representation In C++
Presentation transcript:

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 an item. To minimize the amount of time needed to access the list items we use doubly linked list. So that the list can be accessed in both directions. l An item of the doubly linked list is defined as: struct item{ int val; // could be any type struct item *next, *previous; } NODEPTR *item;

Data Structures: Doubly Linked List2 Get node & free node operations NODEPTR getnode(void) { NODEPTR p; P = (NODEPTR) malloc(sizeof(struct item)); return(p); } void freenode(NODEPTR p) { free(p); }

Data Structures: Doubly Linked List3 Linked List – Insert, cont. void insafter(NODEPTR p, int x) { NODEPTR q; if (p == NULL) { printf("void insertion\n"); exit(1); } q = getnode(); q -> info = x; q -> next = p -> next; q -> previous = p; p->next=q; q->next->previous = q; } /* end Insert after */

Data Structures: Doubly Linked List4 Linked List – Delete, cont. DeleteAfter void deleteafter(NODEPTR p, int *px) { NODEPTR q; if (p == NULL) || (p -> next == NULL)) { printf("void deletion\n"); exit(1); } q = p -> next; *px = q -> info; p -> next = q -> next; q->next->previous = p; freenode(q); } /* end delete after */

Data Structures: Doubly Linked List5 Application: Sparse matrix l What is a sparse matrix? A matrix of most of its entries are zeros. l What is the size of memory for a matrix of size 100x100? l If there are many matrices? l Is there a way of representing the Sparse matrix?

Data Structures: Doubly Linked List6 Sparse Matrix Representation l Each node must contain 5 fields: n Value n Row n Column n Next row n Next column l struct item { int val; int col, row; struct item *nextcol, *nextrow; }; l typedef item *NODEPTR;

Data Structures: Doubly Linked List7 Create a new matrix Its size is: nxn NODEPTR create(int n) { int c, r; NODEPTR mp, cp, rp, p, q; mp = getnode(); mp->col = -1; mp->row = -1; mp->val = 0; mp->nextcol = mp; mp->nextrow = mp; p = mp; for(c=0; c<n ;c++) { q = getnode(); q->col= c; q->row =-1; q->val = 0; q->nextrow = q; p->nextcol= q; p = q; } p->nextcol = mp; p = mp; for(r=0; r < n; r++) { q = getnode(); q->row= r; q->col =-1; q->val = 0; q->nextcol = q; p->nextrow= q; p = q; } p->nextrow = mp; return mp; }

Data Structures: Doubly Linked List8 Initialize a matrix void initiliaze(NODEPTR m, int n) { int v, c, r; NODEPTR cp, rp, p; rp = m->nextrow; cp = rp; for(r=0; r<n; r++){ for(c=0;c<n;c++){ printf("Enter Number %d %d\n", r, c); scanf("%d", &v); p = findabove(m, c); if(v) cp = insertafter(cp, p, v); } rp = rp->nextrow; cp=rp->nextcol; } }

Data Structures: Doubly Linked List9 Find above & insert after NODEPTR findabove(NODEPTR m, int c){ NODEPTR rp, cp; for (cp = m->nextcol; cp->col != c; cp=cp->nextcol); for(rp = cp; rp->nextrow != cp; rp = rp->nextrow); return rp;} NODEPTR insertafter(NODEPTR cp, NODEPTR rp, int v){ NODEPTR q, p; q = getnode(); q->val = v; q->col = rp->col; q->row = cp->row; q->nextcol = cp->nextcol; q->nextrow = rp->nextrow; rp->nextrow = q;cp->nextcol = q; return q;}

Data Structures: Doubly Linked List10 Add function void add(NODEPTR m1, NODEPTR m2, NODEPTR m) { NODEPTR q1, p1, q2, p2, q, p; int v; for(p1=m1->nextrow,p2=m2->nextrow,p=m->nextrow; p1!= m1 && p2 != m2; p1=p1->nextrow,p2=p2->nextrow, p=p->nextrow) {for (q1=p1->nextcol,q2=p2->nextcol, q=p->nextcol; p1!=q1 && p2!=q2; ) if (q1->col == q2->col){q=insertafter(q, findabove(m, q1->col),q1->val+q2->val); q1=q1->nextcol; q2=q2->nextcol;} else if (q1->col col) {q=insertafter(q, findabove(m, q1->col),q1->val); q1=q1->nextcol;} else if (q1->col > q2->col) {q=insertafter(q, findabove(m, q2->col),q2->val); q2=q2->nextcol;} while (p1!=q1) { q=insertafter(q, findabove(m, q1->col),q1->val); q1=q1->nextcol;} while(p2!=q2) { q=insertafter(q, findabove(m, q2->col),q2->val); q2=q2->nextcol; } }

Data Structures: Doubly Linked List11 The print function void print(NODEPTR m, int n) { int v, c, r; NODEPTR cp, rp, p; p = m->nextrow; cp = p->nextcol; for(r=0; r<n; r++){ for(c=0;c<n;c++){ if (cp->col == c) {printf("%d ", cp->val); cp=cp->nextcol;} else printf("%d ",0); } p = p->nextrow; cp = p->nextcol; printf("\n"); } }