Computer Programming for Engineering Applications ECE 175 Intro to Programming.

Slides:



Advertisements
Similar presentations
Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
CSEB324 Data Structures & Algorithms
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
C Intro.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
Data Structures: A Pseudocode Approach with C
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
Linked Lists... An introduction to creating dynamic data structures.
C Programming : Dynamic memory allocation & Structures 2008/11/19 Made by Jimin Hwa Edited and presented by Souneil Park
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Linked Lists. Array Limitations Arrays have a fixed size that cannot be changed at run time What if your program had an array to store info regarding.
1 CSE 303 Lecture 12 structured data reading: Programming in C Ch. 9 slides created by Marty Stepp
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.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Lecture 25 Self-Referential Structures Linked Lists
Implementation of Linked List For more notes and topics visit: eITnotes.com.
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.
Chapter 14 Dynamic Data Structures Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Chapter 19 Data Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Data Structures A data structure.
Introduction to Data Structures Systems Programming Concepts.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
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.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 5.
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
C Structures and Memory Allocation
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
5.13 Recursion Recursive functions Functions that call themselves
Linked lists.
Introduction to Data Structures
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 16-2 Linked Structures
קורס תכנות שיעור 13: רשימות מקושרות.
Linked List.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Linked lists.
Dynamic Data Structures
Presentation transcript:

Computer Programming for Engineering Applications ECE 175 Intro to Programming

Need for Truly Dynamic “Arrays” Assume the following structure Consecutive executions of malloc reserve different parts of the memory Goal is to connect the different pieces of memory ECE 175 nameage nameage nameage typedef struct node_s { char name[20]; int age; struct node_s *listp; } node_t;

Linked Lists: non-sequential pieces of memory connected using pointers How are linked lists different than arrays? Memory cells are not sequential List size can be dynamic, no need to predefine it as with arrays One can add and delete nodes to the list Connecting Pieces of Memory – Linked Lists ECE 175

Declaration of structures with pointer attributes to themselves Structures with Pointer attributes ECE 175 nameagelistp typedef struct node_s { char name[20]; int age; struct node_s *listp; } node_t;

Creation of a Linked List ECE 175 nameagelistp nameagelistp nameagelistp headp NULL To create a linked list we need to Create a head pointer that points to the first element, so we can traverse the list from the start Make each element of the list point to the next one Make the last pointer point to NULL, so we can denote the end of the list

Creation of a Linked List ECE 175 nameagelistp nameagelistp nameagelistp headp NULL int main(void) { int i = 0; node_t *headp, *temp, *last=NULL; FILE *inp = fopen("database.txt", "r"); char c; char s1[20]; while(!feof(inp)) { temp = (node_t *)malloc(sizeof (node_t)); // creation of memory scan_fun(temp, inp); // initialization of element of list if (last == NULL) headp = temp; // setting the head of the list else last->listp=temp; // else connecting to previous element i++; // count number of elements added last = temp; // updating the current element temp->listp = NULL; // setting pointer to null. }

Scanning and Printing Functions Scanning elements from a file, and printing on the screen ECE 175 void scan_fun(node_t *pt, FILE *in) //scans file for node_t structure { fscanf(in, "%s%d", pt->name, &pt->age); } void print_fun(node_t *pt) //prints a node_t structure { printf("%s, %d\n", pt->name, pt->age); } void print_list(node_t *pt) //prints the entire list { if (pt == NULL) printf("The list is empty\n"); else { while (pt != NULL) // traversing the list { print_fun(pt); pt = pt->listp; }

Traversing the List ECE 175 nameagelistp nameagelistp nameagelistp headp NULL while (pt != NULL) // traversing the list { print_fun(pt); pt = pt->listp; // point to next element }

Searching by the name attribute Searching the List by Name ECE 175 node_t *find_name(node_t *pt, char *query) { // finds a name in the list // returns pointer to structure matched or null while(strcmp(pt->name, query) != 0 && pt! = NULL) { pt=pt->listp; } return pt; }

Searching by a range of ages Searching the List by Age ECE 175 void find_age(node_t *pt, int min, int max) { // finds elements within a range of ages while( pt! = NULL) { if (pt->age >= min && pt->age <= max) print_fun(pt); pt=pt->listp; }

Adding a new Element at the End of the List ECE 175 nameagelistpnameagelistp headp NULL nameagelistp void add_member(node_t **h, node_t **l) // adds a list element { node_t *temp; temp = (node_t *)malloc(sizeof (node_t)); // memory allocation printf("Enter the age of the new member:"); scanf("%d", &temp->age); //scan for the age printf("Enter the name of the new member:"); scanf("%s", temp->name); if (*h == NULL) // if list is empty *h = temp; // point the head to temp else { (*l)->listp = temp; // point previous last element to temp } *l = temp; // update the current last element temp->listp=NULL; // point temp->listp to NULL }

Deleting a member ECE 175 nameagelistp nameagelistp nameagelistp headp NULL

Deleting a member ECE 175 void delete_member(node_t **h, node_t **l) { node_t *target; // pointer to string to be deleted node_t **temp = h; // pointer to the head of the list char s[20]; printf("Enter the name of the entry you want to delete:"); fflush(stdin); scanf("%s", s); target = find_name(*h, s); // finding the element to be deleted if (target == NULL) printf("This entry does not exist\n"); else { while ((*temp) != target) { temp = &(*temp)->listp; // locating the address of previous element } if (target == *h) // if first element must be deleted *h = target->listp; // make head point to the next element if (target == *l) // if last element is to be deleted *l = *temp; // update the position of the last element *temp = target->listp; // skip element to be deleted free(target); // free the memory }

Calling List Functions ECE 175 add_member(&headp, &current); // add a new member to the list print_list(headp); // print the list pause(); // wait for user input print_list(headp); // print the list pause(); find_age(headp, 22, 28); //find elements with age between 22 and 28 pause(); delete_member(&headp, &last); // delete a member print_list(headp); // print the list