140
Pierre-Alain FOUQUE Input/Output and Recursive structures

Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

Pierre-Alain FOUQUE

Input/Output and Recursive structures

Page 2: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

2

Summary

1 - String and Files

2 - Recursive structure

3 - Example : chaining lists

Page 3: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

String

• array containing char and terminating with the special character ‘\0’ of type char *t

• strlen gives the length of the string

• strcmp compares in the alphabetic order

• strncpy copies n characters

• cf. string.h

Page 4: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

4

Files• File = pointer to a complex structur of type FILE

(FILE *)

• Opening a file in writing/reading mode

FILE *f = fopen(« toto.txt », «rw»);

Checking if everything works well

if(f==NULL) {

printf(« erreur dans fopen\n »);

exit(EXIT_FAILURE);

}

• Closing a file fclose(f);

Page 5: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

Write and read in a file • fprintf(f, “Coucou %d\n”, a);

• int fputc(int c, FILE *f);

• int fputs(char *str, FILE *f)

• char c=fgetc(FILE *f);

• char *fgets(char * str, int size, FILE *f)

• read at most size-1 characters and strore them in str

• stop if there is a character‘\n’

• append \0’ at the end

Page 6: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

6

StructuresStructures allow to put together objects of different

types

⇒ new type : struct bloc

Page 7: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

6

StructuresStructures allow to put together objects of different

types

⇒ new type : struct bloc

struct bloc { int number; float value;};

Page 8: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

6

StructuresStructures allow to put together objects of different

types

⇒ new type : struct bloc

struct bloc { int number; float value;};

Each object of this type has an integer field, called number

Page 9: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

6

StructuresStructures allow to put together objects of different

types

⇒ new type : struct bloc

struct bloc { int number; float value;};

Each object of this type has an integer field, called number

• a floating field, called valeur

Page 10: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

7

Pointers/Arrayint *pa;

⇒ declare a pointer pa onto an int

0x0A000x0B000x0C00 pa

Page 11: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

7

Pointers/Arrayint *pa;

⇒ declare a pointer pa onto an int

pa = malloc(3*(sizeof(int));0x0A000x0B000x0C00 pa

Page 12: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

7

Pointers/Arrayint *pa;

⇒ declare a pointer pa onto an int

pa = malloc(3*(sizeof(int));0x0A000x0B000x0C00 pa 0x0A01

Page 13: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

7

Pointers/Arrayint *pa;

⇒ declare a pointer pa onto an int

pa = malloc(3*(sizeof(int));0x0A000x0B000x0C00 pa 0x0A01

The size can be modified by the program if it is not sufficient :

Page 14: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

7

Pointers/Arrayint *pa;

⇒ declare a pointer pa onto an int

pa = malloc(3*(sizeof(int));0x0A000x0B000x0C00 pa 0x0A01

The size can be modified by the program if it is not sufficient :

realloc = malloc + copy

Page 15: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

8

List

In algorithmic, the list is an important data structure:

Page 16: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

8

List

In algorithmic, the list is an important data structure:

cell

2

Page 17: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

8

List

In algorithmic, the list is an important data structure:

cell

2 5

Page 18: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

8

List

In algorithmic, the list is an important data structure:

cell

2 5 3

Page 19: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

8

List

In algorithmic, the list is an important data structure:

cell

Append a cell when needed

2 5 3

Page 20: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

8

List

In algorithmic, the list is an important data structure:

cell

Append a cell when needed⇒ Dynamic structure

2 5 3

Page 21: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

9

Structure cell

The cell has two fields :

• a value

• a new cell (or a pointer)

Page 22: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

9

Structure cell

The cell has two fields :

• a value

• a new cell (or a pointer)

struct cellule {

Page 23: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

9

Structure cell

The cell has two fields :

• a value

• a new cell (or a pointer)

struct cellule { int value;

Page 24: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

9

Structure cell

The cell has two fields :

• a value

• a new cell (or a pointer)

struct cellule { int value; struct cellule next;

Page 25: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

9

Structure cell

The cell has two fields :

• a value

• a new cell (or a pointer)

struct cellule { int value; struct cellule next;};

Page 26: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

9

Structure cell

The cell has two fields :

• a value

• a new cell (or a pointer)

struct cellule { int value; struct cellule next;};

Page 27: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

Page 28: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

struct cellule { int value; struct cellule next;};

Page 29: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

What is the size of this structure ?struct cellule { int value; struct cellule next;};

Page 30: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

What is the size of this structure ?

t = 4 + t ⇒ infinite size ?

struct cellule { int value; struct cellule next;};

Page 31: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

What is the size of this structure ?

t = 4 + t ⇒ infinite size ?

⇒ not recursive

struct cellule { int value; struct cellule next;};

Page 32: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

What is the size of this structure ?

t = 4 + t ⇒ infinite size ?

⇒ not recursive

struct cellule { int value; struct cellule next;};

Page 33: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

What is the size of this structure ?

t = 4 + t ⇒ infinite size ?

⇒ not recursive

struct cellule { int value; struct cellule next;};

struct cellule { int value; struct cellule *next;};

Page 34: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

10

Recursive Structure

What is the size of this structure ?

t = 4 + t ⇒ infinite size ?

⇒ not recursive

struct cellule { int value; struct cellule next;};

Fixe size:an int and a pointer

struct cellule { int value; struct cellule *next;};

Page 35: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

11

List

Page 36: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

11

List

The list can be implemented as follows :

Page 37: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

11

List

The list can be implemented as follows :

struct s_cellule { int value; struct s_cellule *next;};

Page 38: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

11

List

The list can be implemented as follows :

struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

Page 39: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

Page 40: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

Page 41: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

struct s_cellule

Page 42: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

struct s_cellule

= cellule

Page 43: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

struct s_cellule

= cellule

Page 44: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

struct s_cellule

= cellulelist = pointer onto a

cellule

Page 45: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

12

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;

struct s_cellule

= cellulelist = pointer onto a

cellule

Empty list = particular pointer ⇒ NULL

Page 46: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

13

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;list nil = NULL;

Page 47: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

13

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;list nil = NULL;

Type cellule

Page 48: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

13

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;list nil = NULL;

Type cellule

Type list

Page 49: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

13

List (suite)struct s_cellule { int value; struct s_cellule *next;};

typedef struct s_cellule cellule;typedef cellule *list;list nil = NULL;

Empty list = nilNULL = pointer 0

Type cellule

Type list

Page 50: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

14

Size of structurestypedef struct { int number; float value;} sbloc;

sbloc *pb;pb = malloc(sizeof(sbloc));

Page 51: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

14

Size of structurestypedef struct { int number; float value;} sbloc;

sbloc *pb;pb = malloc(sizeof(sbloc));

pb

Page 52: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

14

Size of structurestypedef struct { int number; float value;} sbloc;

sbloc *pb;pb = malloc(sizeof(sbloc));

pb

1 2.0

Page 53: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

14

Size of structurestypedef struct { int number; float value;} sbloc;

sbloc *pb;pb = malloc(sizeof(sbloc));

pb

1 2.0

sizeof(<type>)byte number for an object of type <type>

sizeof(sbloc)

Page 54: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

15

Create a cell

list L; /* pointer onto a cell but no associate cell*/

L = malloc(sizeof(cellule)); /* memory is allocate

cell is not initialized */

Page 55: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

15

Create a cell

list L; /* pointer onto a cell but no associate cell*/

L

?

L = malloc(sizeof(cellule)); /* memory is allocate

cell is not initialized */

Page 56: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

15

Create a cell

list L; /* pointer onto a cell but no associate cell*/

L

L

?

L = malloc(sizeof(cellule)); /* memory is allocate

cell is not initialized */

Page 57: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

15

Create a cell

*L is a cell :(*L).value : field value(*L).next : field next

list L; /* pointer onto a cell but no associate cell*/

L

L

?

L = malloc(sizeof(cellule)); /* memory is allocate

cell is not initialized */

Page 58: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Page 59: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L…

Page 60: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list L

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L…

Page 61: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list LLet an integer x

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L…

Page 62: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list LLet an integer x

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

L…

Page 63: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list LLet an integer x

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

L…

Page 64: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list LLet an integer x

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

x

L…

Page 65: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list LLet an integer x

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

x

L…

Page 66: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

16

Create a list

Let a list LLet an integer x

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

x

L1 new list

L…

Page 67: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

Create a listlist cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Page 68: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Page 69: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Page 70: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Page 71: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Page 72: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

nil

Page 73: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);

nil

Page 74: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);

nil

3

L1

Page 75: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);L2 = cons(6,L1);

nil

3

L1

Page 76: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);L2 = cons(6,L1);

nil

3

L1

6

L2

Page 77: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);L2 = cons(6,L1);

nil

3

L1

6

L2

Page 78: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);L2 = cons(6,L1);L3 = cons(1,L2);

nil

3

L1

6

L2

Page 79: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);L2 = cons(6,L1);L3 = cons(1,L2);

L3

1

nil

3

L1

6

L2

Page 80: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

17

L1L2L3

Create a list

list L1,L2,L3;list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1 = cons(3,nil);L2 = cons(6,L1);L3 = cons(1,L2);

L3

1

nil

3

L1

6

L2

Page 81: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

Page 82: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

2 5 3

Page 83: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

• Append and delete are simple

2 5 3

Page 84: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

• Append and delete are simple

• create or destroy a cell

2 5 3

Page 85: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

• Append and delete are simple

• create or destroy a cell

• adapt the corresponding pointers (1 or 2)

2 5 3

Page 86: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

• Append and delete are simple

• create or destroy a cell

• adapt the corresponding pointers (1 or 2)

• Recursive Structure

2 5 3

Page 87: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

18

Interest of lists

• Append and delete are simple

• create or destroy a cell

• adapt the corresponding pointers (1 or 2)

• Recursive Structure

⇒ The main functions are esay to implement thanks to the recursivity

2 5 3

Page 88: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

19

Operations on liste

6 … L

Page 89: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

19

Operations on listeAppend an element

6 … L

Page 90: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

19

Operations on liste

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

Append an element

6 … L

Page 91: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

19

Operations on liste

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

1

Append an element

6 … L

Page 92: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

19

Operations on liste

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

1

Append an element Head and Tail

6 … L

Page 93: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

19

Operations on liste

list cons(int x, list L) { list L1; L1 = malloc(sizeof(cellule)); L1->value = x; L1->next = L; return L1;}

L1

1

tail(L1)

Append an element Head and Tailint head(list L) { return L->value;}

list tail(list L) { return L->next}

head(L1)

6 … L

Page 94: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

L2 5 3

Page 95: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

Display a list can be described as follows:

L2 5 3

Page 96: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

Display a list can be described as follows:print the head (as an integer)

L2 5 3

Page 97: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

Display a list can be described as follows:print the head (as an integer)print the tail (as a list)

L2 5 3

Page 98: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

Display a list can be described as follows:print the head (as an integer)print the tail (as a list)Rk : if the list is empty, do nothing

L2 5 3

Page 99: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

void affiche_list(list L) { if (L != nil) { printf(“%d “,head(L)); affiche_list(tail(L));}}

Display a list can be described as follows:print the head (as an integer)print the tail (as a list)Rk : if the list is empty, do nothing

L2 5 3

Page 100: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

20

Recursive Functions : display

void affiche_list(list L) { if (L != nil) { printf(“%d “,head(L)); affiche_list(tail(L));}}

Display a list can be described as follows:print the head (as an integer)print the tail (as a list)Rk : if the list is empty, do nothing

L2 5 3

stopping rules

Page 101: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

L2 5 3

Page 102: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

L2 5 3

Page 103: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3)

L2 5 3

Page 104: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3

L2 5 3

Page 105: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2

L2 5 3

Page 106: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3)

L2 5 3

Page 107: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3) L = → 5 → 3

L2 5 3

Page 108: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3) L = → 5 → 3 print 5

L2 5 3

Page 109: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3) L = → 5 → 3 print 5 print_list(→ 3)

L2 5 3

Page 110: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3) L = → 5 → 3 print 5 print_list(→ 3) L = → 3

L2 5 3

Page 111: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3) L = → 5 → 3 print 5 print_list(→ 3) L = → 3 print 3

L2 5 3

Page 112: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

21

Display (suite)

void print_list(list L) { if (L != nil) { printf(“%d “,head(L)); print_list(tail(L)); }}

print_list(→ 2 → 5 → 3) L = → 2 → 5 → 3 print 2 print_list(→ 5 → 3) L = → 5 → 3 print 5 print_list(→ 3) L = → 3 print 3 print_list(nil)

L2 5 3

Page 113: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

22

Recursive function: copy

L2 5 3

Page 114: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

22

Recursive function: copy

To copy a list:

L2 5 3

Page 115: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

22

Recursive function: copy

To copy a list:append the head to the copy of the tail

L2 5 3

Page 116: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

22

Recursive function: copy

To copy a list:append the head to the copy of the tailRk : if the list is empty, return the empty list

L2 5 3

Page 117: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

22

Recursive function: copy

list copie(list L) { list L1,L2; if (L == nil) return nil; L1 = copie(tail(L)); L2 = cons(head(L),L1); return L2;}

To copy a list:append the head to the copy of the tailRk : if the list is empty, return the empty list

L2 5 3

Page 118: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

22

Recursive function: copy

list copie(list L) { list L1,L2; if (L == nil) return nil; L1 = copie(tail(L)); L2 = cons(head(L),L1); return L2;}

To copy a list:append the head to the copy of the tailRk : if the list is empty, return the empty list

L2 5 3

stopping rules

Page 119: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

L5 3

Page 120: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 121: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3)

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 122: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 123: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3)

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 124: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 125: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil)

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 126: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 127: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil ⇒ nil

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 128: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil ⇒ nil L1 = nil

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 129: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil ⇒ nil L1 = nil ⇒ cons(3,nil) = → 3

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 130: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil ⇒ nil L1 = nil ⇒ cons(3,nil) = → 3 L1 = → 3

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 131: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil ⇒ nil L1 = nil ⇒ cons(3,nil) = → 3 L1 = → 3 ⇒ cons(5, → 3) = → 5 → 3

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 132: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

23

Copy (suite)

LL = copy(→ 5 → 3) L = → 5 → 3 L1 = copy(→ 3) L = → 3 L1 = copy(nil) L = nil ⇒ nil L1 = nil ⇒ cons(3,nil) = → 3 L1 = → 3 ⇒ cons(5, → 3) = → 5 → 3LL = → 5 → 3

L5 3

list copy(list L) { list L1; if (L == nil) return nil; L1 = copy(tail(L)); return cons(head(L),L1);}

Page 133: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

24

Copy (suite)

L5 3

Page 134: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

24

Copy (suite)

L5 3

list copie(list L) { list L1;

if (L == nil) return nil; L1 = copie(tail(L));

return cons(head(L),L1);}

Page 135: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

24

Copy (suite)

L5 3

list copie(list L) { list L1;

if (L == nil) return nil; L1 = copie(tail(L));

return cons(head(L),L1);}

3

Page 136: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

24

Copy (suite)

L5 3

list copie(list L) { list L1;

if (L == nil) return nil; L1 = copie(tail(L));

return cons(head(L),L1);}

LL5

3

Page 137: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

24

Copy (suite)

L5 3

list copie(list L) { list L1;

if (L == nil) return nil; L1 = copie(tail(L));

return cons(head(L),L1);}

LL5

3

The modificationof a cell of LL

do not change the cases of L

Page 138: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

25

Lists - end

So, the recursive structure of lists allows an easy implementation

• append an element

• copy, display, concatenation

• search an element

• delete an element

• …

Page 139: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

26

Queue and stacks

Two types of list are important in algorithmics:

• list FIFO (First In, First Out) or Queue (file d’attente)

• list LIFO (Last In, First Out) or Stack (pile)

Page 140: Input/Output and Recursive structures - di.ens.frfouque/enseignement/ensta/Cours/Cours6-eng.pdfString • array containing char and terminating with the special character ‘\0’

27

Conclusion

The dynamic structures have many advantages:

• récursives structures,very easy to manipulate recursive functions

• dynamic allocation of the memory, when needed (no loss of memory)