Computers and programming 1 The 11 th lecture Jiří Šebesta.

Slides:



Advertisements
Similar presentations
A C++ Crash Course Part II UW Association for Computing Machinery Questions & Feedback.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Dynamic memory allocation
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.
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.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Advanced Data Structures Stack –a stack is dynamic data items in a linear order, such that the item first "pushed" in is the last item "popped" out. Think.
Growing Arrays in C By: Victoria Tielebein CS 265- Spring 2011.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
1 C Review and Dissection V: Dynamic Memory Allocation and Linked Lists These lecture notes created by Dr. Alex Dean, NCSU.
CP104 Introduction to Programming Structure II Lecture 32 __ 1 Data Type planet_t and Basic Operations Abstract Data Type (ADT) is a data type combined.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Computers and programming The 7 th lecture Jiří Šebesta.
Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
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.
Informática II Prof. Dr. Gustavo Patiño MJ
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C FAQ’S Collected from the students who attended technical round in TCS recruitment.
Computers and programming The 6 th lecture Jiří Šebesta.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
CS3012: Formal Languages and Compilers The Runtime Environment After the analysis phases are complete, the compiler must generate executable code. The.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Outline Midterm results Static variables Memory model
Runtime Environments Compiler Construction Chapter 7.
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.
Computer Science Detecting Memory Access Errors via Illegal Write Monitoring Ongoing Research by Emre Can Sezer.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers OVERVIEW.
1 Writing a Good Program 8. Elementary Data Structure.
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.
CS61C Midterm 1 Review Summer 2004 Pooya Pakzad Ben Huang Navtej Sadhal.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
Programming Abstractions Cynthia Lee CS106X. Topics:  Priority Queue › Linked List implementation › Heap data structure implementation  TODAY’S TOPICS.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Computers and programming The 9 th lecture Jiří Šebesta.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
CNG 140 C Programming (Lecture set 12) Spring Chapter 13 Dynamic Data Structures.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
Dynamic Allocation Review Structure and list processing
Lectures linked lists Chapter 6 of textbook
Data Structures and Algorithms
CSC172 Data Structures Linked Lists (C version)
Chapter 16-2 Linked Structures
LINKED LISTS.
Programming Abstractions
Review & Lab assignments
Data Structures and Algorithms
Dynamic Memory A whole heap of fun….
Chapter 16 Linked Structures
C Programming Lecture-8 Pointers and Memory Management
CSCE 206 Lab Structured Programming in C
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Computers and programming 1 The 11 th lecture Jiří Šebesta

TOPIC – programming with dynamic variables 1.Coupling with dynamic variables 2.Dynamic database - example

Coupling with dynamic variables (1/13) Approach using array of pointers Allocated static array of pointer to dynamic variable (structure) – max. number of structures is fixed in advance

Coupling with dynamic variables (2/13) Linear list approach The first pointer to the first structure is only allocated in memory. Each structure has pointer to the following structure as item – max. number of structures is restricted only by memory capacity.

Coupling with dynamic variables (3/13) Linear list approach - modified Two static pointers to the first and last structure (for simple implementation).

Coupling with dynamic variables (4/13) typedef struct comp// competitor record { char name[10]; int jump; struct comp *next; }t_comp; t_comp *first;// ptr to the first comp. - global t_comp *last;// ptr to the last comp. - global int cnt = 0;// the number of recorded comp. - global Linear list – insertion of unknown number of competitors (broad jumpers) in to the list including results (length of jump) Structure and global variables comp is structure namespace t_comp is type namespace

Coupling with dynamic variables (5/13) void add(char *cname, int cjump)// add comp. record { t_comp *ccomp; // ptr to a comp ccomp = (t_comp*)malloc(sizeof(t_comp)); strcpy(ccomp->name, cname); // record filling ccomp->jump = cjump; cnt++;// the number of records plus one if(cnt == 1) {first = ccomp;// add the first record last = ccomp; } else {last->next = ccomp;// add next record last = ccomp; } Function for adding of a competitor and his length of jump to the linear list

Coupling with dynamic variables (6/13) void show(void) { t_comp *scomp; int acnt=cnt; scomp = first; do { printf("%s: %d cm\n", scomp->name, scomp->jump); scomp=scomp->next; } while (--acnt > 0); } List of competitors printing including length of jump according to insertion order (from the first to the last)

Coupling with dynamic variables (7/13) int main(void) { char cmd, aname[10]; int ajump; printf("\nA: Add, S: Show, Q: Quit\n"); scanf("%c", &cmd); fflush(stdin); while(!(cmd == 'Q' || cmd == 'q')) { if(cmd=='A' || cmd=='a') { printf("\nName: "); scanf("%s", aname); fflush(stdin); printf("\nJump [cm]: "); scanf("%d", &ajump); fflush(stdin); add(aname, ajump); } main() function for application with commands

Coupling with dynamic variables (8/13) if(cmd=='S' || cmd=='s') show(); printf("\nA: Add, S: Show, Q: Quit"); scanf("%c", &cmd); fflush(stdin); } return 0; } Example: Ex71.c

Coupling with dynamic variables (9/13) Modification of the previous example by algorithm for searching of the best competitor Replenishment of the function add() void add(char *cname, int cjump)// add comp. record { // the same as in the example 71 if(cnt == 1) {first = ccomp;// add the first record last = ccomp; } else {last->next = ccomp;// add next record last = ccomp; } last->next = NULL; // last points to null address }

Coupling with dynamic variables (10/13) void results(void) { t_comp *scomp, *gold; int mjump = first->jump; gold = first; scomp = first->next; do { if(scomp->jump > mjump) { gold = scomp; mjump = gold->jump; } scomp = scomp->next; } while(scomp != NULL); printf("\nWin %s (%d cm)\n", gold->name, gold->jump); } Example: Ex72.c

Coupling with dynamic variables (11/13) Stack approach One static pointer to the top of stack (heap) – system LIFO (last in – first out).

Coupling with dynamic variables (12/13) Tree approach

Coupling with dynamic variables (13/13) Complicated coupling of structures – family tree strcpy(me->p_sists[0]-> Name, ”Jana”); me->p_sists[0]-> p_mother = me-> p_mother; //uncles: me->p_mother-> p_brths[x] me->p_father-> p_brths[x]

Dynamic database - example (1/5) Submission: Build-up a console application, which dynamically generate a database of competitors (broad jumpers). Each structure consists of competitor’s name, competitor’s country, length of the jump, and pointer to the other structure of competitor. Create a function for appending of the competitor including length of jump and function for printing of the competitors including their results. Modify the function for appending of competitor: a new competitor have to be add to the linear list according to his result (length of the jump) – list is always sorted regarding the length of the jump.

Dynamic database - example (2/5) Linear sorted list

Dynamic database - example (3/5) typedef struct t_comp// competitor record { char name[10]; char country[10]; int jump; t_comp *next; }; t_comp *first;// ptr to the first comp. - global t_comp *last;// ptr to the last comp. - global int count = 0;// the number of recorded comp. - global Linear list – insertion of unknown number of competitors (broad jumpers) to the list including result (length of the jump) Structure and global variables

Dynamic database - example (4/5) void add(char *cname, char *ccountry, int cjump) { t_comp *ccomp, *prevcomp, *nextcomp;... Modified function for appending of the competitor and his length of the jump to the linear list with automatic sorting regarding the result Programming in lecture

Dynamic database - example (5/5) Example: Ex73.c Complete application will be issued in web pages in December, 5 as Ex73sol.c Records releasing from memory

TOPIC OF THE NEXT LECTURE 1.Advanced algorithms in C THANK YOU FOR YOUR ATTENTION