The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Similar documents
Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Programming. Lists, Stacks, Queues

Ashish Gupta, Data JUET, Guna

Linked Lists and other Dynamic Data Structures

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Data Structure Series

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Chapter 17: Linked Lists

Chapter 17: Linked Lists

Solution for Data Structure

Cpt S 122 Data Structures. Data Structures

Dynamic Data Structures (II)

Self-referential Structures and Linked List. Programming and Data Structure 1

Linked List. April 2, 2007 Programming and Data Structure 1

CMSC 341 Lecture 7 Lists

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Write a program that creates in the main function two linked lists of characters and fills them with the following values:

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

Lists (Section 5) Lists, linked lists Implementation of lists in C Other list structures List implementation of stacks, queues, priority queues

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

CIS 190: C/C++ Programming. Linked Lists

CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

CS32 Discussion Week 3

Dynamic Data Structures

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Programming II (CS300)

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

+ Abstract Data Types

Heap Arrays and Linked Lists. Steven R. Bagley

CS 2113 Software Engineering

However, in C we can group related variables together into something called a struct.

Midterm Review. CS 211 Fall 2018

Introduction to Data Structures. Systems Programming

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

CA341 - Comparative Programming Languages

Introduction to Linked Lists

.:: UNIT 4 ::. STACK AND QUEUE

UNIT IV 4 LINKED LIST

Dynamic Memory Allocation and Linked Lists

Linked List using a Sentinel

COMP 524 Spring 2018 Midterm Thursday, March 1

Linked Lists in C and C++

BBM 201 DATA STRUCTURES

CP2 Revision. theme: dynamic datatypes & data structures

15. Stacks and Queues

Homework Assignment #1

.:: UNIT 3 ::. LIST AND LINKED LIST

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

The time and space are the two measure for efficiency of an algorithm.

Data Structures and Algorithms for Engineers

Lecture 7: Data Structures. EE3490E: Programming S1 2017/2018 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Programming II (CS300)

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

Introduction to Linked List: Review. Source:

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Class Information ANNOUCEMENTS

CT11 (ALCCS) DATA STRUCTURE THROUGH C JUN 2015

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

Procedural Programming

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Here's how you declare a function that returns a pointer to a character:

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Low-Level C Programming. Memory map Pointers Arrays Structures

In Java we have the keyword null, which is the value of an uninitialized reference type

malloc(), calloc(), realloc(), and free()

DC104 DATA STRUCTURE JUNE Q.2 a. If you are using C language to implement the heterogeneous linked list, what pointer type will you use?

C Data Structures Stacks. Stack. push Adds a new node to the top of the stack

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

Queues. October 20, 2017 Hassan Khosravi / Geoffrey Tien 1

Chapter 20: Binary Trees

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Rules for Abstract Classes and Methods

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

Lists, Stacks and Queues in C. CHAN Hou Pong, Ken CSCI2100A Data Structures Tutorial 4

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 4

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

ITI Introduction to Computing II

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

(Section : Computer Science)

CS61, Fall 2012 Section 2 Notes

Linked Lists. What Is A Linked List? Example Declarations. An Alternative Collections Data Structure. Head

INSERT AS A FIRST NODE

CE204 Data Structures and Algorithms Part 1

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

Structures, Pointers, Linked Lists

Data Structure. Recitation III

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Transcription:

Data Structures in C C Programming and Software Tools N.C. State Department of Computer Science Data Structures in C The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures Linked lists Trees Graphs Without dynamic memory allocation, you could still create these data structures within an array Using structs and pointers 1

Collections Array Lists Elements stored in a partially filled array Size of collection can quickly identify next place to add element (if adding at end of the list) If size == capacity of array, the array grows automatically through the creation of a new, larger array, with the elements copied Linked Lists A struct represents a single node in the list A node contains a pointer to the next node in the list A null value represents the end of the list Collections (2) When considering any functionality related to a list collection always consider: An empty list Beginning of the list Middle of the list End of the list 2

Array List vs. Linked List For each of the following characteristics, identify if it describes an Array List or a Linked List. Characteristic Array List? Linked List? Access any element via an index in the collection in constant time. Easily grow or shrink the collection. Space only allocated for elements currently in the collection. May have unused space. Constant runtime efficiency to get an item from the list at a particular index. Adding or removing an element at in the middle of the collection requires a shift of other elements (as appropriate for the operation). Declaring a Node Type Each node contains data and a pointer to the next node Use a struct Data can be multiple members of the struct Cannot use typedef when defining node type for a linked list. Otherwise, the type of the next node is unknown. struct node int value; struct node *next; ; 3

Creating a List A list is a pointer to the first node in the list. The list initially is empty (NULL) struct node *front = NULL; Procedural decomposition of list functionality Create functions that represent discrete operations on a list (similar to the LinkedList and ArrayList methods) add, remove, find, etc. Consider a global vs. local list If global list, then all functions can access the front of the list If local list, the front of the list must be passed into functions and the modified list must be returned Adding a Node to a List Adding a node to a list has three steps 1. Allocating memory for the node 2. Storing data in the node 3. Inserting the node into the list Consider empty list, front of the list, middle of the list, and end of the list There may be specializations 4

Adding a Node to the Front of List Global reference to the front of the list struct node *front; void add_to_front(int v) struct node *new_node; Use sizeof(type) not sizeof(type *) new_node = (struct node *) malloc(sizeof(struct node)); if (new_node == NULL) printf("error: malloc failed in add_to_front\n"); exit(exit_failure); new_node->value = v; new_node->next = front; front = new_node; int main() add_to_front(3); Adding a Node to the Front of List Local reference to the front of the list struct node *add_to_front(struct node *list, int v) struct node *new_node = malloc(sizeof(struct node)); if (new_node == NULL) printf("error: malloc failed in add_to_front\n"); exit(exit_failure); new_node->value = v; new_node->next = list; return new_node; int main() struct node *front = NULL; front = add_to_front(front, 3); 5

Traversing a List Algorithm Start at first element Manipulate data for element Move to next element Make sure you don t lose your list! Create a pointer that you use specifically for traversing the list Printing a List Global reference to the front of the list struct node *front; Maintain a second reference to void print_list() the front of the list for traversal. struct node *cur; for (cur = front; cur!= NULL; cur = cur->next) printf("%d ", cur->value); printf("\n"); int main() print_list(); 6

Printing a List Local reference to the front of the list void print_list(struct node *list) for (; list!= NULL; list = list->next) printf("%d ", list->value); printf("\n"); Parameter is pass by value! int main() struct node *front = NULL; print_list(front); Serves as local reference to the list in print_list function. The main front reference is not modified! Processing... (cont'd) Adding a new person to the end of the list? 7

Removing a Node from a List Removing a node from a list has three steps 1. Locate the node to be deleted 2. Alter the previous node so that it bypasses the deleted node 3. Free the memory to reclaim space of deleted node Current searching algorithm would get us to the node we want to delete Instead, we want to stop at the node BEFORE the one we want to delete Trailing pointer technique (see book Section 17.5) Removing a Node from the Back of List Global reference to the front of the list struct node *front; void remove_from_back() if (front == NULL) return; //empty list struct node *cur, *prev; for (cur = front, prev = NULL; cur->next!= NULL; prev = cur, cur = cur->next) ; //cur will point to last node of list if (prev == NULL) //special case where only one node front = front->next; //sets front to NULL else prev->next = cur->next; //sets prev->next to NULL free(cur); 8

Removing a Node from the Back of List Local reference to the front of the list struct node *remove_from_back(struct node *list) if (list == NULL) return list; //empty list struct node *cur, *prev; for (cur = list, prev = NULL; cur->next!= NULL; prev = cur, cur = cur->next) ; if (prev == NULL) //list has only one node list = list->next; else prev->next = cur->next; //point to NULL free(cur); return list; Processing (cont'd) Remove an item from the front of a list? 9

Specialized Data Structures More efficient on add/remove operations Sorted list Faster search can quit earlier if can t find value Stacks Add and remove from same end Queues Add at one end and remove from the other end Optimize speed of access by creating a doubly linked list and maintaining a reference to the front and back of the list Doubly Linked Lists A node maintains a pointer to the node before and after it in the list. struct node struct node *prev; int value; struct node *next; ; All list operations need to update appropriate prev and next pointers. Can maintain a pointer to the back of the list 10