Linked Data Representations

Similar documents
Linked Data Representations. Data Structures and Programming Techniques

Recursion. Manolis Koubarakis. Data Structures and Programming Techniques

Stacks. Manolis Koubarakis. Data Structures and Programming Techniques

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Class Information ANNOUCEMENTS

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

CSC 1600 Memory Layout for Unix Processes"

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Introduction to Data Structures. Systems Programming

EL2310 Scientific Programming

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

Dynamic Memory Allocation and Linked Lists

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Lectures 5-6: Introduction to C

Lecture 3: C Programm

Lectures 5-6: Introduction to C

SYSC 2006 C Winter 2012

Procedural programming with C

JTSK Programming in C II C-Lab II. Lecture 3 & 4

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

CS 237 Meeting 19 10/24/12

CS 314 Principles of Programming Languages. Lecture 11

C Language Part 3. Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Intermediate Programming, Spring 2017*

Functions in C C Programming and Software Tools

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Kurt Schmidt. October 30, 2018

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Compiling and Running a C Program in Unix

This is CS50. Harvard University Fall Quiz 0 Answer Key

Darshan Institute of Engineering & Technology for Diploma studies Unit 4

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Cpt S 122 Data Structures. Data Structures

C for C++ Programmers

[0569] p 0318 garbage

Dynamic Memory CMSC 104 Spring 2014, Section 02, Lecture 24 Jason Tang

CSCI 171 Chapter Outlines

MODULE 5: Pointers, Preprocessor Directives and Data Structures

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

CS201 Some Important Definitions

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

o Code, executable, and process o Main memory vs. virtual memory

The C Programming Language Part 4. (with material from Dr. Bin Ren, William & Mary Computer Science, and

Output of sample program: Size of a short is 2 Size of a int is 4 Size of a double is 8

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

COP 3223 Final Review

ch = argv[i][++j]; /* why does ++j but j++ does not? */

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Lecture 2: C Programm

Lecture 04 Introduction to pointers

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

Language comparison. C has pointers. Java has references. C++ has pointers and references

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Arrays and Pointers (part 1)

Arrays and Pointers (part 1)

CA31-1K DIS. Pointers. TA: You Lu

Dynamic memory. EECS 211 Winter 2019

Dynamic memory allocation

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

DECLARAING AND INITIALIZING POINTERS

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

by Pearson Education, Inc. All Rights Reserved.

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS61, Fall 2012 Section 2 Notes

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Dynamic Data Structures (II)

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

COP 3223 Final Review

C Pointers. 7.2 Pointer Variable Definitions and Initialization

CS 345. Functions. Vitaly Shmatikov. slide 1

Data Structures and Algorithms for Engineers

Lecture 9 Assertions and Error Handling CS240

2/28/2018. Overview. The C Programming Language Part 4. Pointers. Pointers. Pointers. Pointers

Technical Questions. Q 1) What are the key features in C programming language?

However, in C we can group related variables together into something called a struct.

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

High-performance computing and programming Intro to C on Unix/Linux. Uppsala universitet

11 'e' 'x' 'e' 'm' 'p' 'l' 'i' 'f' 'i' 'e' 'd' bool equal(const unsigned char pstr[], const char *cstr) {

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

ME964 High Performance Computing for Engineering Applications

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

MYcsvtu Notes LECTURE 34. POINTERS

Programs in memory. The layout of memory is roughly:

Here's how you declare a function that returns a pointer to a character:

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

CS 61C: Great Ideas in Computer Architecture Introduction to C

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Transcription:

Linked Data Representations Manolis Koubarakis 1

Linked Data Representations Linked data representations such as lists, stacks, queues, sets and trees are very useful in computer science and applications. E.g., in databases, artificial intelligence, graphics, hardware etc. We will cover all of these data structures in this course. Linked data representations are useful when it is difficult to predict the size and shape of the data structures needed. 2

Levels of Data Abstraction Lists Stacks Sets Trees Queue s ADT s Sequential Representatio ns Linked Representatio ns Arrays Strings Arrays of Record s Pointer Representati ons Paralle l Arrays 3

Pointers Linked data representations are realized using pointers. A pointer is a variable that references a unit of storage. Graphical notation (α is a pointer to β): α: β β: α: β: 4

Pointers in C typedef int *IntegerPointer; IntegerPointer A, B; /* the declaration int *A, *B has the same effect */ A=(IntegerPointer)malloc(sizeof(int)); B=(int *)malloc(sizeof(int)); The above code results in the following situation: A: B: 5

typedef C provides a facility called typedef for creating new data type names. typedefs are useful because: They help to organize our data type definitions nicely. They provide better documentation for our program. They make our program portable. 6

Pointers in C (cont d) The previous statements first define a new data type name IntegerPointer which consists of a pointer to an integer. Then they define two variables A and B of type IntegerPointer. Then they allocate two blocks of storage for two integers and place two pointers to them in A and B. The void pointer returned by malloc is casted into a pointer to a block of storage holding an integer. You can omit this casting and your program will still work correctly. 7

malloc void *malloc(size_t size) is a function of the standard library stdlib. malloc returns a pointer to space for an object of size size, or NULL if the request cannot be satisfied. The space is uninitialized. This is called dynamic storage allocation. size_t is the unsigned integer type 8

Pointers in C (cont d) *A=5; *B=17; A: 5 B: 17 The unary operator * on the left side of the assignment designates the storage location to which the pointer A refers. We call this pointer dereferencing. 9

Pointers in C (cont d) int X=3; A=&X; A: X: 3 The unary operator & gives the address of some object (in the above diagram the address of variable X). 10

Pointers in C (cont d) Question: What happens if we now execute B=20;? 11

Pointers in C (cont d) Answer: We have a type mismatch error since 20 is an integer but B holds a pointer to integers. The compiler gcc will give a warning: assignment makes pointer from an integer without a cast. 12

Pointers in C (cont d) Suppose we start with the diagram below: A: 5 B: 17 13

Pointers in C (cont d) Question: If we execute A=B; which one of the following two diagrams results? A: 17 A: 5 B: 17 B: 17 14

Pointers in C (cont d) A=B; A: 5 B: 17 wer: The right diagram. Now A and B are called aliases because they name same storage location. Note that the storage block containing 5 is now inacces e languages such as Lisp have a garbage collection facility for such storage. 15

Recycling Used Storage We can reclaim the storage space to which A points by using the reclamation function free: free(a); A=B; A: B: 17 16

Dangling Pointers Question: Suppose now we call free(b). What is the value of *A+3 then? A: B: 17. 17

Dangling Pointers (cont d) Answer: We do not know. Storage location A now contains a dangling pointer and should not be used. A: B:? reasonable to consider this to be a programming error even though the com he runtime system will not catch it. 18

NULL There is a special address denoted by the constant NULL which is not the address of any node. The situation that results after we execute A=NULL; is shown graphically below: A:. Now we cannot access the storage location to which A pointed to earlier. So something like *A=5; will give us segmentation fault. L is automatically considered to be a value of any pointer type that can be defi NULL is defined in <stdio.h> and has the value 0. 19

Pointers and Function Arguments Let us suppose that we have a sorting routine that works by exchanging two out-of-order elements using a function Swap. Question: Can we call Swap(A,B) where the Swap function is defined as follows? void Swap(int X, int Y) { int Temp; } Temp=X; X=Y; Y=Temp; 20

Pointers and Function Arguments (cont d) Answer: No. C passes arguments to functions by value therefore Swap can t affect the arguments A and B in the routine that called it. Swap only swaps copies of A and B. The way to have the desired effect is for the calling program to pass pointers to the values to be changed: Swap(&A, &B); 21

The Correct Function Swap void Swap(int *P, int *Q) { int Temp; } Temp=*P; *P=*Q; *Q=Temp; 22

In Pictures In the calling program: A: B: In Swap: P: Q: 23

Linked Lists A linear linked list (or linked list) is a sequence of nodes in which each node, except the last, links to a successor node. We usually have a pointer variable L containing a pointer to the first node on the list. The link field of the last node contains NULL. Example: a list representing a flight Airport Link Airport Link Airport L: DUS ORD SAN Link. 24

Diagrammatic Notation for Linked Lists L: Info Link Info Link Info Link. Last: 25

Declaring Data Types for Linked Lists The following statements declare appropriate data types for our linked list: typedef char AirportCode[4]; typedef struct NodeTag { AirportCode Airport; struct NodeTag *Link; } NodeType; typedef NodeType *NodePointer; We can now define variables of these datatypes: NodePointer L; or equivalently NodeType *L; 26

Structures in C A structure is a collection of one or more variables possibly of different types, grouped together under a single name. The variables named in a structure are called members. In the previous structure definition, the name NodeTag is called a structure tag and can be used subsequently as a shorthand for the part of the declaration in braces. 27

Example Given the previous typedefs, what would be the output of the following piece of code: AirportCode C; NodePointer L; strcpy(c, BRU ); printf( %s\n, C); L=(NodePointer)malloc(sizeof(NodeType)); strcpy(l->airport, C); printf( %s\n, L->Airport); 28

The Function strcpy The function strcpy(s,ct) copies string ct to string s, including \0. It returns s. The function is defined in <string.h>. 29

Accessing Members of a Structure To access a member of a structure, we use the dot notation as follows: structure-name.member To access a member of a structure pointed to by a pointer P, we can use the notation (*P).member or the equivalent arrow notation P- >member. 30

Question Why didn t I write C= BRU ; and L->Airport= BRU in the previous piece of code? 31

Answer The assignment C= BRU ; assigns to variable C a pointer to the character array BRU. This would result in an error (type mismatch) because C is of type AirportCode. Similarly for the second assignment. 32

Example Given the previous typedefs, what does the following piece of code do?: NodePointer L, M; L=(NodePointer)malloc(sizeof(NodeType)); strcpy(l->airport, DUS ); M=(NodePointer)malloc(sizeof(NodeType)); strcpy(m->airport, ORD ); L->Link=M; M->Link=NULL; 33

Answer The piece of code on the previous slide constructs the following linked list of two elements: M: L: Airport DUS Link Airport ORD Link. 34

Inserting a New Second Node on a List Example: adding one more airport to our list representing a flight Airport Link BRU L: Airport Link Airport Link Airport DUS ORD SAN Link. 35

Inserting a New Second Node on a List void InsertNewSecondNode (void) { NodeType *N; N=(NodeType *)malloc(sizeof(nodetype)); strcpy(n->airport, BRU ); N->Link=L->Link; L->Link=N; } 36

Inserting a New Second Node on a List (cont d) Let us execute the previous function step by step: N=(NodeType *)malloc(sizeof(nodetype)); Airport Link N:?? strcpy(n->airport, BRU ); Airport Link N: BRU? 37

Inserting a New Second Node on a List (cont d) N->Link=L->Link; Airport Link N: BRU X? L: Airport Link Airport Link Airport DUS ORD SAN Link. 38

Inserting a New Second Node on a List (cont d) L->Link=N; N: Airport BRU Link L: Airport Link Airport Link Airport DUS X ORD SAN Link. 39

Comments In the function InsertNewSecondNode, variable N is local. Therefore it vanishes after the end of the function execution. However, the dynamically allocated node remains in existence after the function has terminated. 40

Searching for an Item on a List Let us now define a function which takes as input an airport code A and a pointer to a list L and returns a pointer to the first node of L which has that code. If the code cannot be found, then the function returns NULL. 41

Searching for an Item on a List NodeType *ListSearch(char *A, NodeType *L) { NodeType *N; } N=L; while (N!= NULL){ if (strcmp(n->airport,a)==0){ return N; } else { N=N->Link; } } return N; 42

Comments The function strcmp(cs,ct) compares string cs to string ct and returns a negative integer if cs precedes ct alphabetically, 0 if cs==ct and a positive integer if cs follows ct alphabetically (using the ASCII codes of the characters of the strings). 43

Comments (cont d) Let us assume that we have the list below and we are searching for item ORD. When the initialization statement N=L is executed, we have the following N: situation: L: Airport DUS Link Airport Link Airport Link ORD SAN. 44

Comments (cont d) Later on, inside the while loop, the statement N=N->Link is executed and we have the following situation: N: L: Airport DUS Link Airport Link Airport Link ORD SAN. 45

Comments (cont d) Then, the if inside the while loop is executed and the value of N is returned. Assuming that we did not find ORD here, the statement N=N->Link is again executed and we have the following situation: N: L: Airport DUS Link Airport Link Airport Link ORD SAN. 46

Comments (cont d) Then, the while loop is executed one more time and the statement N=N->Link results in the following situation: N: L: Airport DUS Link Airport Link Airport Link ORD SAN. NULL 47

Comments (cont d) Then, we exit from the while loop and the statement return N returns NULL: N: L: Airport DUS Link Airport Link Airport Link ORD SAN. NULL 48

Deleting the Last Node of a List Let us now write a function to delete the last node of a list L. If L is empty, there is nothing to do. If L has one node, then we need to dispose of the node s storage and then set L to be the empty list. If L has two or more nodes then we can use a pair of pointers to implement the required functionality as shown on the next slides. 49

Deleting the Last Node of a List (cont d) Note that we need to pass the address of L as an actual parameter in the form of &L enabling us to change the contents of L inside the function. Therefore the corresponding formal parameter of the function will be a pointer to a pointer to NodeType. 50

Deleting the Last Node of a List void DeleteLastNode(NodeType **L) { NodeType *PreviousNode, *CurrentNode; } if (*L!= NULL) { if ((*L)->Link == NULL){ free(*l); *L=NULL; } else { PreviousNode=*L; CurrentNode=(*L)->Link; while (CurrentNode->Link!= NULL){ PreviousNode=CurrentNode; CurrentNode=CurrentNode->Link; } PreviousNode->Link=NULL; free(currentnode); } } 51

Comments When we advance the pointer pair to the next pair of nodes, the situation is as follows: PreviousNode: CurrentNode: *L: Airport X X Link Airport Link Airport Link DUS ORD SAN. L: 52

Inserting a New Last Node on a List void InsertNewLastNode(char *A, NodeType **L) { NodeType *N, *P; N=(NodeType *)malloc(sizeof(nodetype)); strcpy(n->airport, A); N->Link=NULL; if (*L == NULL) { *L=N; } else { P=*L; while (P->Link!= NULL) P=P->Link; P->Link=N; } } 53

Printing a List void PrintList(NodeType *L) { NodeType *N; } printf( ( ); N=L; while(n!= NULL) { printf( %s, N->Airport); N=N->Link; if (N!=NULL) printf(, ); } printf( )\n ); 54

The Main Program #include <stdio.h> #include <string.h> #include <stdlib.h> typedef char AirportCode[4]; typedef struct NodeTag { AirportCode Airport; struct NodeTag *Link; } NodeType; typedef NodeType *NodePointer; /* function prototypes */ void InsertNewLastNode(char *, NodeType **); void DeleteLastNode(NodeType **); NodeType *ListSearch(char *, NodeType *); void PrintList(NodeType *); 55

The Main Program (cont d) int main(void) { NodeType *L; L=NULL; PrintList(L); InsertNewLastNode( DUS, &L); InsertNewLastNode( ORD, &L); InsertNewLastNode( SAN, &L); PrintList(L); DeleteLastNode(&L); PrintList(L); } if (ListSearch( DUS",L)!= NULL) { printf( DUS is an element of the list\n"); } /* Code for functions InsertNewLastNode, PrintList, */ /* ListSearch and DeleteLastNode goes here. */ 56

Readings T. A. Standish. Data Structures, Algorithms and Software Principles in C. Chapter 2. 57