Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Chapter 4 Lists Pointers Singly Linked Lists
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
LIST PROCESSING.
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.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
CS Data Structures Chapter 4 Lists.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Introduction to Data Structure
Introduction to Data Structures Systems Programming.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
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.
Introduction to Data Structures Systems Programming Concepts.
Copyright Networking Laboratory Chapter 4. LISTS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Array Lists Array Lists Dale.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Data Structure & Algorithms
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Fig Storage of a C program. Fig Memory allocation with malloc.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Software Learning Resource Service Platform CHAPTER 4 鏈結串列 (Linked List) 1.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
5.13 Recursion Recursive functions Functions that call themselves
Computer Science 210 Computer Organization
UNIT – I Linked Lists.
Introduction to Programming
Elementary Data Structures
Pointers & Dynamic Memory
Abstract Data Types Polynomials CSCI 240
Data Structures and Algorithms
Traversing a Linked List
Abstract Data Types Sparse Matrices CSCI 240
EEE2108: Programming for Engineers Chapter 4. Linked Lists
CSC172 Data Structures Linked Lists (C version)
CSCI206 - Computer Organization & Programming
Introduction to Data Structures
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 16-2 Linked Structures
Computer Science 210 Computer Organization
Pointers Call-by-Reference CSCI 230
Review & Lab assignments
Data Structures and Algorithms
Chapter 17: Linked Lists.
C Programming Lecture-8 Pointers and Memory Management
CS148 Introduction to Programming II
LINKED LIST Dr. T. Kokilavani Assistant Professor
CS148 Introduction to Programming II
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure Review

Dale Roberts Pointer Review (1) int i, *pi; i pi ?? pi = &i; i pi ?1000 *pi i = 10 or *pi = 10 i pi *pi

Dale Roberts Pointer Review (2) typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; } list_pointer ptr = NULL; ptr 1000 NULL ptr = malloc(sizeof(list_node)); ptr *ptr ptr->data  (*ptr).data data link

Dale Roberts Pointer Review (3) void delete(list_pointer *ptr, list_pointer trail, list_pointer node) ptr: a pointer point to a pointer point to a list node ptr *ptr(*ptr)->link trail (node): a pointer point to a list node trail *trail data link trail->link  (*trail).link

Dale Roberts Pointer Review (4) element delete(stack_pointer *top) top delete(&st) vs. delete(st) top st st top 400 Does not change.

Dale Roberts Dynamic Storage Question Question: Dynamically allocate an integer, and then return the memory back to the operating system.

Dale Roberts Dynamic Storage Answer Answer: int *pi; pi = (int *) malloc(sizeof(int)); free(pi); request memoryreturn memory

Dale Roberts bat  cat  sat  vat NULL Linked List Insertion Question: Given the following sorted linked list, draw the diagram for inserting the word “mat” into the list.

Dale Roberts bat  cat  sat  vat NULL mat  Linked List Insertion (Answer) Answer: Scan the list for where to insert mat (linear search). Make mat point to where cat points, and then make cat point to mat.

Dale Roberts Linked List Deletion Question: Given the following list, delete the word mat. bat  cat  sat  vat NULL mat 

Dale Roberts bat  cat  sat  vat NULL mat  dangling reference Linked List Deletion Answer: Make cat point to where mat points, then free mat. Make sure to not lose your only reference to mat when you change cat’s link.

Dale Roberts Creating a linked list of words Question: Create a linked list with one node with the value of “bat”. Definition typedef struct list_node, *list_pointer; typedef struct list_node { char data [4]; list_pointer link; }; Creation list_pointer ptr =NULL; Testing #define IS_EMPTY(ptr) (!(ptr)) Allocation ptr=(list_pointer) malloc (sizeof(list_node));

Dale Roberts b a t \0 NULL  address of first node ptr data ptr link ptr Reminder: e -> name  (*e).name strcpy(ptr -> data, “bat”); ptr -> link = NULL; Creating a linked list of words (Answer)

Dale Roberts Create a two node list typedef struct list_node *list_pointer; typedef struct list_node { int data; list_pointer link; }; list_pointer ptr =NULL 10  20 NULL ptr

Dale Roberts Create a two node list (Answer) list_pointer create2( ) { /* create a linked list with two nodes */ list_pointer first, second; first = (list_pointer) malloc(sizeof(list_node)); second = ( list_pointer) malloc(sizeof(list_node)); second -> link = NULL; second -> data = 20; first -> data = 10; first ->link = second; return first; } 10  20 NULL ptr

Dale Roberts Acknowledgements Some code is from Horowitz, Sahni, and Anderson-Freed, Fundamentals of Data Structures in C. Some slides were originally developed by Chen, Hsin-His.