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.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Linked Lists.
CSEB324 Data Structures & Algorithms
C Structures What is a structure? A structure is a collection of related variables. It may contain variables of many different data types---in contrast.
Dynamic memory allocation
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
An introduction to pointers in 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.
Introduction to Memory Management. 2 General Structure of Run-Time Memory.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming and Data Structure
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.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
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:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
CS61C L05 C Structures, Memory Management (1) Garcia, Spring 2005 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 536 Spring Run-time organization Lecture 19.
CS 61C L4 Structs (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #4: Strings & Structs
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.
Run-time Environment and Program Organization
1 CS 201 Array Debzani Deb. 2 Having trouble linking math.h? Link with the following option gcc –lm –o test test.o.
CS Data Structures Chapter 4 Lists.
Pointers Applications
Chapter 12 Pointers and linked structures. 2 Introduction  The data structures that expand or contract as required during the program execution is called.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Runtime Environments Compiler Construction Chapter 7.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
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.
Page 1 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft Dynamic Memory Allocation Suppose we defined the data type: struct custrec.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
1 Pointers to structs. 2 A pointer to a struct is used in the same way as a pointer to a simple type, such as an int. Pointers to structs were introduced.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CPS4200 Unix Systems Programming Chapter 2. Programs, Processes and Threads A program is a prepared sequence of instructions to accomplish a defined task.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Memory management operators Dynamic memory Project 2 questions.
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
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.
© 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.
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.
Stack and Heap Memory Stack resident variables include:
Chapter 4 Linked Lists.
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Popping Items Off a Stack Lesson xx
Memory Allocation CS 217.
Pointer to Structures Lesson xx
Review & Lab assignments
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
C Programming Lecture-8 Pointers and Memory Management
SPL – PS2 C++ Memory Handling.
Dynamic Data Structures
Presentation transcript:

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 lists is that we do not need to indicate the number of values in our structure at any one time. We can simply allocate and deallocate the nodes as required. For example, a basic linked list would contain two values, the value to store and the value of the pointer. To define a node for a linked list we declare a structure to hold the values that are required. In this case two values are needed for each node, an integer and a pointer.

Thus, to define a node for a linked list we declare a structure to hold the values that are required. In this case two values are needed for each node, an integer and a pointer. For example, struct listnode { int value;//integer variable list node *next; //pointervariable }; Once the structure is defined we also need to define a pointer to the structure so that we may use it to access the structure. Typedef listnode *listnodepointer; We use this pointer to point to the structure namely; Listnodepointer head; Head = new listnode; (*head).value = 5; (*head).next = NULL

Here the pointer head is declared first which indicates the start of the list. By referencing (*head).value, we are instructing the computer to go to memory address of head and then add the offset for the value (in the first instance 0), and then store the value 5 at that address. By referencing (*head).next, we are accessing the next member in the list. A shorthand is provided to access lists so that for example: (*head).value = 5 is equivalent to head->value = 5 for example, let us create 3 nodes listnodepointer head; listnodepointer second; listnodepointer third; head-> next = second; second -> next = third; third -> next = NULL

If we wish to insert values into nodes we perform the following: head-> value = 1; second -> value = 2; third -> value = 3; The contents of the list would thus be as depicted in the diagram.

Linked List summary In summery therefore Linked List is a one dimensional data structure, each element contains data and a pointer to the next element. A NULL pointer indicates the end of the list. Doubly linked lists also contain pointers to the previous element in the list enabling recovery from link failure. Main Application areas are in data bases. struct LL { char nodename[10]; int ID; LL *nextLL, *prevLL; };

Assignment example Linked List and Dynamic allocation #define RECORDS 6 void main() { struct computer { char computer_name[25]; /* The computer_name */ char computer_type[25]; /* The type of computer */ int cpu_speed; /* The computers cpu_speed */ struct computer *next; /*pointer to another record of this type */ } *point, *start, *prior; /* this defines 3 pointers */ int index; start = (struct computer *)malloc(sizeof(struct computer)); strcpy(start->computer_name,"Linux 1"); strcpy(start->computer_type,"Samba server computer_type"); start->cpu_speed = 4; start->next = NULL; prior = start;

Malloc function The malloc function allocates space for example: start = (struct computer *)malloc(sizeof(struct computer)); This statement assigns something to the pointer start which will create a dynamic structure containing three variables. The malloc() function, by default, will allocate a piece of memory on a heap that is “ sizeof ” characters in length and will be of type character. The " sizeof " must be specified as the only argument to the function. The malloc() function returns a pointer to the allocated memory After the dynamically allocated data has been used the memory is deallocated using the free command.

Good programming practice dictates that we free up the dynamically allocated space before we quit. point = start; /* first block of group */ do { prior = point->next; /*next block of data */ free(point); /* free present block */ point = prior; /* point to next */ } while (prior != NULL); /* quit when next is NULL */ }

heap and segmentation The original IBM PC had a data bus which was 16 bits wide. As a result these computers use a microprocessor with a 64K segment size (2 16 =64K), and they require special calls to use data outside of a single segment. One limitation placed on users by most MS-DOS C compilers is a limit of 64K for the executable code if you happen to be in the small memory model. A heap is an area outside of this 64K boundary which can be accessed by the program to store data and variables. Thus the heap is outside the segment that the code is in and access to the heap is through an inter-segment jump. The data and variables are put on the heap by the system as calls to malloc() are made. The system keeps track of where the data is stored.