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.

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.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Dynamic memory allocation
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.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
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
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Winter2015 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University C: Advanced Topics.
Dynamic memory allocation. The process of allocating memory at run time is known as dynamic memory allocation. C have four library functions for allocating.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
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 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
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.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
7. Pointers, Dynamic Memory 20 th September IIT Kanpur 1C Course, Programming club, Fall 2008.
6. More on Pointers 14 th September IIT Kanpur C Course, Programming club, Fall
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1. Generic Pointer 2. NULL Pointer 3. Wild Pointer 4. Dangling Pointer.
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 
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
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.
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.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
UNIT-II Topics to be covered Singly linked list Circular linked list
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
ECE Application Programming
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Linked lists.
Programmazione I a.a. 2017/2018.
Introduction to Linked Lists
14th September IIT Kanpur
Lists.
EECE.2160 ECE Application Programming
Linked List.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
7. Pointers, Dynamic Memory
C Programming Lecture-8 Pointers and Memory Management
Linked Lists.
Linked lists.
Dynamic Data Structures
Presentation transcript:

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 50 students and the user wanted to store more than 50 records…

Arrays – Train Analogy Does a freight train have a fixed number of freight cars ?? The answer is NO. The number of cars depends on the quantity of freight that needs to be hauled If the size was fixed, – the cars would have to go empty if freight was less than total capacity – If freight was more than capacity, then more trains would need to be used Therefore, fixed size trains would be inefficient Trains’ solution: – For more freight simply add some cars – Reduce the number of cars if freight is less

Linked Lists Similarly, arrays are inefficient if we do not know in advance, the amount of storage needed The problem can be solved with Dynamic Memory Allocation Linked list is a very important data structure which makes use of dynamic memory allocation and can solve the problem of inefficient data storage of arrays The linked list works very much like a train

but something is missing …

Making of a Linked List We have so far used structures to store relevant pieces of information e.g. the struct student_info contains name, course and graduation year of a student We also used (fixed size) arrays of structures to store multiple structs Therefore, structs can act as the “freight cars” of a train. We can create as many structs as we want with every struct located somewhere in the memory

John Cop3223h 2014 ray cot Jenny cda deborah cnt

Only if we could connect these structs in a way so as to access one struct after the other !!!!

Creating Links in a Linked List typedef struct { char name[64]; char course[128]; int year; struct student_info *next; } student_info;

John Cop3223h 2014 *next ray cot *next Jenny cda *next deborah cnt *next

Now we have a “LIST” of structs that is “LINKED” The structs in a linked list are called elements or more commonly nodes But, how do we access the first node of the linked list ?? With a special pointer to the first node, usually called First.

John Cop3223h 2014 *next ray cot *next Jenny cda *next deborah cnt *next First

Dynamic Memory Allocation Used to allocate memory at Run Time. Also used to Free up memory at run time. The function malloc(mem_size) returns a pointer to a newly allocated block of memory of size mem_size The function free(ptr) de-allocates a block of memory pointed to by ptr

Dynamic Memory Allocation student_info* create_node(void) { student_info *ptr = malloc(sizeof(student_info)); } the pointer ‘ptr’ now contains address of a newly created node After creating a node, it can be assigned the values that it is created to hold and its next pointer is assigned the address of next node. If no next node exists (or if its the last node) then as already discussed, a NULL is assigned.

Storing data in a linked list node void get_student_info(student_info* st) { system("cls"); printf("Enter student's name: "); scanf("%s", &st->name); printf("\n\nEnter student's course: "); scanf("%s", &st->course); printf("\n\nEnter student's planned graduation year: "); scanf("%d", &st->year); st->next = NULL; }

Traversing a linked list void print_student_info(student_info *st) { system("cls"); while(st != NULL) { printf("\n\n%s is enrolled in %s will graduate in the year %d\n", st->name, st->course, st->year); st = st->next; } system("pause"); }

the pointer ‘ptr’ now contains address of a newly created node