CSCE 3110 Data Structures & Algorithm Analysis

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

Chapter 4 Lists Pointers Singly Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
CS Data Structures Chapter 4 Lists.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Introduction to Data Structure
CSCE 3110 Data Structures & Algorithm Analysis Arrays and Lists.
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.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Data Structure in C Transparency No. 4-1 Copyright(c) 1997, Sungkyunkwan University Chapter #4: LISTS Fundamentals of Data Structure in C Horowitz, Sahni.
 Array ◦ sequential representation ◦ some operation can be very time-consuming (data movement) ◦ size of data must be predefined ◦ static storage allocation.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
Linked List :: Basic Concepts
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Elementary Data Structures
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Linked list.
Data Structures 7th Week
Queue data structure.
Stack and Queue APURBO DATTA.
EEE2108: Programming for Engineers Chapter 4. Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
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.
Introduction to Data Structures
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCE 210 Data Structures and Algorithms
Lists.
Programmazione I a.a. 2017/2018.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked List (Part I) Data structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Hassan Khosravi / Geoffrey Tien
Lists CSE 373 Data Structures.
Data Structures and Algorithms
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Vectors, Lists and Sequences
Vectors, Lists, and Sequences
General List.
Module 13 Dynamic Memory.
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Growable Arrays. Lists. Reading: Chap. 3 Weiss

Linked Lists Avoid the drawbacks of fixed size arrays with Growable arrays Linked lists

Growable arrays Avoid the problem of fixed-size arrays Increase the size of the array when needed (I.e. when capacity is exceeded) Two strategies: tight strategy (add a constant): f(N) = N + c growth strategy (double up): f(N) = 2N

Tight Strategy Add a number k (k = constant) of elements every time the capacity is exceeded 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 C0 + (C0+k) + … (C0+Sk) = S = (N – C0) / k Running time? C0 * S + S*(S+1) / 2  O(N2)

Tight Strategy void insertLast(int rear, element o) { if ( size == rear) { capacity += k; element* B = new element[capacity]; for(int i=0; i<size; i++) { B[i] = A[i]; } A = B; A[rear] = o; rear++; size++; }

Growth Strategy Double the size of the array every time is needed (I.e. capacity exceeded) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 C0 + (C0 * 2) + (C0*4) + … + (C0*2i) = i = log (N / C0) Running time? C0 [1 + 2 + … + 2 log(N/C0) ]  O(N) How does the previous code change?

Linked Lists Avoid the drawbacks of fixed size arrays with Growable arrays Linked lists

Using Dynamically Allocated Memory (review) int i, *pi; float f, *pf; pi = (int *) malloc(sizeof(int)); pf = (float *) malloc (sizeof(float)); *pi =1024; *pf =3.14; printf(”an integer = %d, a float = %f\n”, *pi, *pf); free(pi); free(pf); request memory return memory

Linked Lists bat  cat  sat  vat NULL

Insertion bat  cat  sat  vat NULL mat  Compare this with the insertion in arrays!

Deletion bat  cat  mat  sat  vat NULL dangling reference

List ADT ADT with position-based methods generic methods size(), isEmpty() query methods isFirst(p), isLast(p) accessor methods first(), last() before(p), after(p) update methods swapElements(p,q), replaceElement(p,e) insertFirst(e), insertLast(e) insertBefore(p,e), insertAfter(p,e) removeAfter(p)

Implementation Declaration typedef struct node *pnode; typedef struct node { char data [4]; pnode next; } node; Creation pnode ptr =NULL; Testing #define IS_EMPTY(ptr) (!(ptr)) Allocation ptr=(pnode) malloc (sizeof(node));

Create one Node e  name  (*e).name strcpy(ptr  data, “bat”); ptr  link = NULL; address of first node ptr data ptr link  b a t \0 NULL ptr

Example: Create a two-nodes list pnode create2( ) { /* create a linked list with two nodes */ pnode first, second; first = (pnode) malloc(sizeof(node)); second = ( pnode) malloc(sizeof(node)); second -> next= NULL; second -> data = 20; first -> data = 10; first ->next= second; return first; } 10  20 NULL ptr

Insert (after a specific position) void insertAfter(pnode node, char* data) { /* insert a new node with data into the list ptr after node */ pnode temp; temp = (pnode) malloc(sizeof(node)); if (IS_EMPTY(temp)){ fprintf(stderr, “The memory is full\n”); exit (1); }

strcpy(temp->data, data); if (node) { noempty list temp->next=node->next; node->next= temp; } else { empty list temp->next= NULL; node =temp; } } node 10  20 NULL 50  temp

Deletion node trail = NULL node 10  20 NULL 50  20 NULL (a) before deletion (b)after deletion Delete node other than the first node head node head 10  50  20 NULL 10  20 NULL

void removeAfter(pnode node) { / void removeAfter(pnode node) { /* delete what follows after node in the list */ pnode tmp; if (node) { tmp = node -> next; node->next = node->next->next; free(tmp); } } node 10  50  20 NULL 10  20 NULL

Traverse a list Where does ptr point after this function call? void traverseList(pnode ptr) { printf(“The list contains: “); for ( ; ptr; ptr = ptr->next) printf(“%4d”, ptr->data); printf(“\n”); } Where does ptr point after this function call?

Other List Operations swapElements insertFirst insertLast deleteBefore deleteLast

Running Time Analysis insertAfter O(?) deleteAfter O(?) deleteBefore O(?) deleteLast O(?) insertFirst O(?) insertLast O(?)