CSCE 3110 Data Structures & Algorithm Analysis

Slides:



Advertisements
Similar presentations
Slide 1 Insert your own content. Slide 2 Insert your own content.
Advertisements

Chapter 3: Linked List Tutor: Angie Hui
0 - 0.
Addition Facts
Numerical Recipes The Art of Scientific Computing (with some applications in computational physics)
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
EENG212 Algorithms and Data Structures
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists: deleting...
Queues and Linked Lists
Linear Lists – Linked List Representation
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Chapter 4 Lists Pointers Singly Linked Lists
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
CSEB324 Data Structures & Algorithms
Fall 2005 Data Structure Linked List – Single Linked List.
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.
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
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.
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.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
§1 Abstract Data Type (ADT)
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
Linked Lists.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Addition 1’s to 20.
Week 1.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Doubly-linked list library.
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.
CSCE 3110 Data Structures & Algorithm Analysis Stacks and Queues Reading: Chap.3 Weiss.
C Intro.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
CSCE 3110 Data Structures & Algorithm Analysis Arrays and 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.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
UNIT-II Topics to be covered Singly linked list Circular linked list
CSCE 3110 Data Structures & Algorithm Analysis
Data Structure Dr. Mohamed Khafagy.
CSCE 3110 Data Structures & Algorithm Analysis
Data Structures 7th Week
Java collections library
CSCE 3110 Data Structures & Algorithm Analysis
Lists.
Programmazione I a.a. 2017/2018.
CSCE 3110 Data Structures & Algorithm Analysis
Lists.
Presentation transcript:

CSCE 3110 Data Structures & Algorithm Analysis Rada Mihalcea http://www.cs.unt.edu/~rada/CSCE3110 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; }; 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_FULL(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(?)