Lecture 25 Self-Referential Structures Linked Lists

Slides:



Advertisements
Similar presentations
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Advertisements

Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
LIST PROCESSING.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
C Intro.
Data Structures: A Pseudocode Approach with C
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Linked Lists... An introduction to creating dynamic data structures.
Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
ISP - 2 nd Recitation Functions Pointers Structs Files Code Examples Homework!
Structures and Lists (and maybe stacks) Chapters 9-10.
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
1 Review of Class on Nov 23:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
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.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
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.
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.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lecture 14 Linked Lists 14-1 Richard Gesick. Linked Lists Dynamic data structures can grow and shrink at execution time. A linked list is a linear collection.
CSC2100B Tutorial 2 List and stack implementation.
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.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Structures. Heterogeneous Structures Collection of values of possibly different types. Name the collection. Name the components. Example : Student record.
Introduction to Data Structures Systems Programming Concepts.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
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.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
Fig Storage of a C program. Fig Memory allocation with malloc.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Lists, Stacks and Queues in C
Dynamic Allocation Review Structure and list processing
5.13 Recursion Recursive functions Functions that call themselves
Chapter 4 Linked Lists.
Introduction to Data Structures
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Data Structures ADT List
Review & Lab assignments
Linked Lists Chapter 4.
Linked List.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
C Programming Lecture-8 Pointers and Memory Management
Structures and List Processing
Dynamic Data Structures
Presentation transcript:

Lecture 25 Self-Referential Structures Linked Lists C Programming Lecture 25 Self-Referential Structures Linked Lists

Self-Referential Structures Self-referential structures have pointer members that hold the address of the same structure type. The pointer members allow the linking together of an unspecified number of such structures. Example: struct list { int data; struct list *next; };

Pictorial Representation To help us understand and think about self-referential structures, we use pictures: data next The data members. The pointer member.

An Example struct list { int data; struct list *next; } struct list a, b, c; a.data = 1; The result of these b.data = 2; declarations and c.data = 3; initializations is pictured below: a.next = b.next = c.next = NULL; a b c 1 NULL 2 NULL 3 NULL

Continuation of Example a.next = &b; b.next = &c; a b c 1 2 3 NULL Now we can use the links to retrieve data from successive elements. a.next->data has a value of 2 a.next->next->data has a value of 3

Linear Linked Lists A linked list has a head pointer that addresses the first element of the list. Then the pointer member in each structure in the list points to a successor structure. The last structure has its pointer member set to NULL. Typically, a linked list is created dynamically.

Dynamic Storage Allocation “Dynamic” storage allocation refers to allocation of storage during program execution time, rather than during compile time. Utility functions such as malloc() are provided in the standard library to allocate storage dynamically. malloc()stands for “memory allocation”.

Header File for Example of Dynamic Creation of a Linked List #include <stdio.h> typedef char DATA; /* use char in examples */ struct linked_list { DATA d; struct linked_list *next; }; typedef struct linked_list ELEMENT; typedef ELEMENT * LINK;

Example of Dynamic Allocation of a Linked List head = malloc(sizeof(ELEMENT)); head->d = ‘n’; head->next = NULL; head n NULL head->next = malloc(sizeof(ELEMENT)); head->next->d = ‘e’; head->next->next = NULL; head n e NULL head->next->next = malloc(sizeof(ELEMENT)); head->next->next->d = ‘w’; head->next->next->next = NULL; head n e w NULL

List Operations Basic List Operations Creating a list Counting the elements Looking up an element Inserting an element Deleting an element

Creating a List from a String using Recursion #include “list.h” LINK string_to_list(char s[]) { LINK head; if (s[0] == ‘\0’) /* base case */ return NULL; else { head = malloc(sizeof(ELEMENT)); head->d = s[0]; head->next = string_to_list(s + 1); return head; } /* We will walk through this in class for the string “hello” */

Counting the Elements in a List /* Count the elements recursively */ #include “list.h” int count(LINK head) { if (head == NULL) return 0; else return(1 + count(head->next)); }

Lookup c in the List Pointed to by head #include “list.h” LINK lookup(DATA c, LINK head) { if (head == NULL) return NULL; else if (c == head->d) return head; else return(lookup(c, head->next)); }

Illustration of Insertion of a New List Element Before insertion: . . . A C B q NULL After insertion: . . . A C B q

Recursive Function to Insert an Element in a List #include “list.h” void insert(LINK p1, LINK p2, LINK q) { p1->next = q; /* insertion */ q->next = p2; } p1 p2 . . . A C B q

Deleting an Element from a List p->next = q->next; p q . . . A B C p->next prior to delete. p->next after the delete. Note that the deleted node is now garbage (of no use). Its storage can be returned to the system by using the standard library function free().

Recursive Deletion of a List #include “list.h” void delete_list(LINK head) { if (head != NULL) { delete_list(head->next); free(head); /* release storage */ }

Demonstration Structures struct date_rec { int month; int day; int year; }; /* year is stored as yyyy */ struct friend_rec { char fname[15]; /* assume first name is stored in caps */ char phone[9]; /* local number only, such as 555-1234 */ struct date_rec birthday; }; struct my_friends { struct friend_rec a_friend; struct my_friends *next; . . . struct date_rec dob; struct friend_rec friend; struct my_friends *head, *current, *new;

Direct Input/Output The functions fread() and fwrite() are used to read and write binary files. No conversions are performed as they are with fscanf(). In certain applications (those that use structures), the use of these functions can save considerable time. Because one fread or fwrite can input or output an entire structure.