LINKED LISTS.

Slides:



Advertisements
Similar presentations
Variables in C Amir Haider Lecturer.
Advertisements

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.
C Language.
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.
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Programming Languages and Paradigms The C Programming Language.
Structures Spring 2013Programming and Data Structure1.
Structures in C.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Arrays Ethan Cerami New York University Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Functions, Pointers, Structures Keerthi Nelaturu.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
Spring 2005, Gülcihan Özdemir Dağ Lecture 11, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 11 Outline 11.1.
Pointers. Pointer Variable Declarations and Initialization Pointer variables – Contain memory addresses as their values – Normal variables contain a specific.
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.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Introduction We will study how to broaden the modeling facilities of C by defining our own data types that represent structured collections of data pertaining.
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
User-Written Functions
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Lesson #6 Modular Programming and Functions.
CS1010 Programming Methodology
Lesson #6 Modular Programming and Functions.
C Programming Tutorial – Part I
Programming Languages and Paradigms
Review of C… The basics of C scanf/printf if/elseif statements
Quiz 11/15/16 – C functions, arrays and strings
Programming Paradigms
Arrays in C.
Introduction to Data Structures
CSCE 210 Data Structures and Algorithms
Programming Languages and Paradigms
Character Strings Lesson Outline
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Lesson #6 Modular Programming and Functions.
Lecture 9 Structure 1. Concepts of structure Pointers of structures
LINKED LISTS.
C Stuff CS 2308.
Week 9 – Lesson 1 Arrays – Character Strings
Lecture 18 Arrays and Pointer Arithmetic
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Programming in C Pointer Basics.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
Pointers.
Linked List.
EECE.2160 ECE Application Programming
Lesson #6 Modular Programming and Functions.
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
Week 9 – Lesson 2 Input & Output of Character Strings
Programming Languages and Paradigms
EECE.2160 ECE Application Programming
FUNCTION ||.
EECE.2160 ECE Application Programming
Structures, Unions, and Enumerations
Files Chapter 8.
Presentation transcript:

LINKED LISTS

Structures and functions When a structured variable is passed as an input to a function, all of its component values are copied into the components of the function's corresponding formal parameters. Unlike arrays that require pointers to be passed to functions, structures can be passed by value. Of course you can use pointers also if you need multiple results, like we have seen before.

Structures and functions Let's have a function that prints out a report on the element. void print_element (ele e)‏ { printf ("Name: %s\n", e.name); printf ("Symbol: %s\n", e.symbol); printf ("Weight: %.1lf\n", e.atom_weight); printf ("Atomic Number: %d\n", e.atom_number); } In the main it would be called like print_element (e1);

Structures and functions Let's have a function that compares two elements by comparing the two atomic numbers. It returns true if they are identical. int compare_elements (ele e1, ele e2)‏ { int equal; equal = e1.atom_number == e2.atom_number; return (equal); } In the main it would be called like compare_elements (e1, e[4]);

Structures and functions Let's have a function that fills a structure from the keyboard and then returns it back to the main. ele read_element (void)‏ { ele e; printf ("Enter the name, symbol, atomic weight and atomic number separated by spaces: "); scanf ("%s%s%lf%d",e.name, e.symbol, &e.atom_weight, &e.atom_number); return (e); } In the main it would be called like e1 = read_element( );

Structures, functions, and pointers Let's have a function that does exactly the same thing as the previous one except that it will read two elements and use a pointer parameter for the extra "fake result." ele read_element (ele* eptr) Before going further we need to understand the order of pointer operations. If a pointer variable of type ele is called eptr then to fill its atomic number component, for example, we would be tempted to use scanf (“%d”, &*eptr.atom_number); (notice that eptr is a pointer, the element itself is *eptr). That would be a mistake, however, as we will see on the next slide.

Structures, functions, and pointers The scanf statement on the previous slide is not correct because the dot (.) operator is always processed before the star (*) operator. That would result in accessing the atomic number of a pointer (eptr.atom_number) instead of the atomic number of an element. Therefore we need to use scanf ("%d", &(*eptr).atom_number); instead. There is a short cut in C for (*eptr)., it is eptr->, therefore, we can use scanf ("%d", &eptr->atom_number); as an alternative.

Structures and functions Now let's see the function: ele read_element2 (ele* eptr)‏ { ele e; printf ("Enter the name, symbol, atomic weight and number separated by spaces: "); scanf ("%s%s%lf%d",e.name, e.symbol, &e.atom_weight, &e.atom_number); scanf ("%s%s%lf%d",eptr->name, eptr->symbol, &eptr->atom_weight, &eptr->atom_number); return (e); } In the main it would be called like e1 = read_element2(&e2);

Linked Lists A list is a finite sequence, with order taken into account. A linked list consists of ordered nodes like N1, N2, N3,...Nn together with the address of the first node N1. Each node has several fields, one of which is an address field.

Linked Lists The address field of N1 contains the address of N2, the address field of N2, the address of N3, and so on.... The address field of Nn is a NULL address. In the following example, the variable start contains the address of the first node. Each node has two fields, the first contains an integer indicating the position of the node in the list, the second field contains the address of the next node. start 1 2 3

Example of a linked list To declare a linked list, we need to declare a structure where the last component will be a pointer to a variable of the structured type itself. Let's define a list of elephants. Notice that the next component is a pointer to a struct ELEPHANT. The other component store the animal's name and weight. typedef struct ELEPHANT { char name [10]; int weight; struct ELEPHANT* next; }elephant;

Example of a linked list Next, let's declare variables to store our list nodes and the start of the list. /* the nodes - ready to contain 3 elephant names */ elephant eleph1, eleph2, eleph3; /* the start variable - pointer to an elephant node */ elephant* start;

Example of a linked list Let's fill now the elephants' names and weight: strcpy (eleph1.name, "Edna"); strcpy (eleph2.name, "Elmer"); strcpy (eleph3.name, "Eloise"); eleph1.weight = 4500; eleph2.weight = 6000; eleph3.weight = 4750; start eleph1 eleph2 eleph3 "Edna" 4500 "Elmer" 6000 "Eloise" 4700

Example of a linked list Next, let's link all these elephants together and build the linked list. start = &eleph1; eleph1.next = &eleph2; eleph2.next = &eleph3; eleph3.next = NULL; start eleph1 eleph2 eleph3 "Edna" 4500 "Elmer" 6000 "Eloise" 4500