• Non ci sono risultati.

LINKED LISTS

N/A
N/A
Protected

Academic year: 2021

Condividi "LINKED LISTS"

Copied!
30
0
0

Testo completo

(1)

PROGRAMMAZIONE I

A.A. 2018/2019

(2)

LINKED LISTS

(3)

LINKED LIST

What are the problems with arrays?

üSize is fixed

üArray items are stored contiguously

üInsertions and deletions at particular positions is complex

Why linked lists?

üSize is not fixed

üData can be stored at any place

üInsertions and deletions are simpler and faster

(4)

WHAT IS A LINKED LIST

A linked list is a collection of nodes with various field It contains

üData fields üLink fields

Data fields Link fields

Data fields Link fields

(5)

DIFFERENT KINDS OF LISTS

Singly linked lists

Circular singly linked list Doubly linked lists

Circular doubly linked list

(6)

SINGLY LINKED LIST

10 1000 30 4000 5 NULL

7000

Pointer to the first element

Stored at memory address 7000

Stored at memory address 1000

Stored at memory address 4000

(7)

CIRCULAR SINGLY LINKED LIST

10 1000 30 4000 5 7000

7000

Pointer to the first element

(8)

DOUBLY LINKED LIST

10

NULL 1000

7000

50

7000 5000

30

1000 NULL

Stored at memory address 7000

Stored at memory address 1000

Stored at memory address 5000

(9)

CIRCULAR DOUBLY LINKED LIST

10

5000 1000

7000

50

7000 5000

30

1000 7000

(10)

REPRESENTATION AND OPERATIONS OF SINGLY

LINKED LISTS

(11)

REPRESENTATION OF A NODE

struct node { int info1;

char info2;

struct data info3;

struct node* pNext;

} struct node {

int info;

struct node* pNext;

}

info

pNext info1

info2 info3 pNext

typedef struct node Node

(12)

ALLOCATION IN THE HEAP

int main() {

Node *p = (Node*) malloc(sizeof(Node));

… }

heap

info pNext 1000

p= 1000

stack

(13)

INITIALIZATION

int main() {

Node *p = (Node*) malloc(sizeof(Node));

p->info = 3;

p->pNext= NULL;

… }

heap

info = 3 pNext=

NULL 1000

p= 1000

stack

(14)

SIDE STRUCTURES

One pointer to the first element of the list

One pointer to the last element of the list (optional)

10 1000 30 4000 5 NULL

7000 Node* pFirst

4000

Node* pLast

(15)

MOST COMMON OPERATIONS

Print all the elements Insertion

üHead üTail

üAt a given position

Deletion

üHead üTail

üAt a given position

(16)

PRINT (LIST SCAN)

// As a parameter, it takes the pointer to the first node of a list void print_list(Node* pFirst)

{

if(pFirst == NULL) // No node in the list {

printf(”No node in the list!");

} else {

// New pointer used to scan the list.

Node* pScan = pFirst;

do {

printf(”Info: %d\n", pScan->id);

// ptrScan is updated to point to the next node in the // list

pScan = pScan->pNext;

}while(pScan!= NULL); //NULL when this was the last node }

return;

}

(17)

HOW IT RUNS

10 1000 30 4000 5 NULL

7000 Node* pFirst

pFirst == NULL? No pScan = pFirst 7000 printf pScan->info 10

pScan = pScan ->pNext 1000 pScan != NULL? Yes

printf pScan->info 30

pScan = pScan ->pNext 4000 pScan != NULL? Yes

printf pScan->info 5

pScan = pScan ->pNext NULL pScan != NULL? No

return Info: 10

Info: 30 Info: 5

7000 1000 8000

(18)

HEAD INSERTION

// Node* pFirst is a pointer to the first node of a list (global) void head_insertion(void)

{

// Creation of a new node in the heap

Node *pNew = (Node*) malloc(sizeof(Node));

scanf(“%d”, &(pNew->info));

pNew->pNext= NULL;

if(pFirst == NULL) // No node in the list

pFirst = pNew; // The first node is the newly created one else

{

// Else, there is already at least one node in the list

pNew-> pNext= pFirst; // the first node becomes the second one pFirst= pNew; // The first node is the newly created one

}

return;

}

(19)

HOW IT RUNS

3 NULL

7000

Node* pFirst

Node *pNew = (Node*) malloc(sizeof(Node));

scanf(“%d”, pNew->info);

pNew->pNext= NULL;

pFirst == NULL? Yes pFirst = pNew 3500 3500

NULL3500

(20)

HOW IT RUNS

3 NULL

30 4000 5 NULL

7000

Node* pFirst

10 1000

Node *pNew = (Node*) malloc(sizeof(Node));

scanf(“%d”, pNew->info);

pNew->pNext= NULL;

pFirst == NULL? No

pNew -> pNext= pFirst 7000 pFirst = pNew 3500

3 7000

3500

3500

(21)

HEAD INSERTION (ALTERNATIVE)

// Node** ppFirst is a pointer to the pointer to the first node of a list void head_insertion(Node** ppFirst)

{

// Creation of a new node in the heap

Node *pNew = (Node*) malloc(sizeof(Node));

scanf(“%d”, &(pNew->info));

pNew->pNext= NULL;

if(*ppFirst == NULL) // No node in the list

*ppFirst = pNew; // The first node is the newly created one else

{

// Else, there is already at least one node in the list

pNew-> pNext= *ppFirst; // the first node becomes the second one

*pFirst= pNew; // The first node is the newly created one }

return;

}

(22)

HEAD DELETION

// Node* pFirst is a pointer to the first node of a list (global) void head_deletion(void)

{

if(pFirst == NULL) // No node in the list {

printf(”No node in the list!");

} else {

// Else, there is at least a node in the list

// Remember the pointer to the second node, which will become // the first one

Node* temp= pFirst-> pNext;

// Memory is deallocated (node canceled from memory) free(pFirst);

// The first node becomes the former second node pFirst= temp;

}

return;

}

(23)

HOW IT WORKS

10 1000 30 4000 5 NULL

7000 Node* pFirst

pFirst == NULL? No

temp= pFirst -> pNext 1000 free(pFirst) 7000

pFirst = temp 1000

(24)

ADDING AND REMOVING FROM TAIL

When we need to add or remove a node from the tail, it is useful to also have a pointer to the last node of a list

üIt simplifies writing these two functions

This pointer (pLast) needs to be updated at the end of these operations

Of course if we need to have add-or-remove from head

and add-or-remove from tail, pLast needs to be updated

also in add-or-remove from head (as pFirst in previous

examples)

(25)

TAIL INSERTION

// Node* pFirst is a pointer to the first node and Node* pLast to the last node of // a list (both global)

void tail_insertion(void) {

// Creation of a new node in the heap

Node *pNew = (Node*) malloc(sizeof(Node));

scanf(“%d”, &(pNew->info));

pNew->pNext= NULL;

if(pFirst == NULL) // No node in the list

pFirst = pNew; // The first node is the newly created one pLast = pNew; // The last node is the newly created one else

{ // Else, there is already at least one node in the list

pLast-> pNext= pNew; // the last node becomes the second one pLast= pNew; // The last node is the newly created one

}

return;

}

(26)

HOW IT WORKS

10 1000 30 4000 5 NULL

7000 Node* pFirst

15 NULL

500

5 500

4000 Node* pLast 500

pFirst == NULL? No

pLast-> pNext= pNew 500 pLast= pNew 500

(27)

TAIL DELETION

void tail_deletion() { if(pFirst == NULL)

printf(”No node in the list!\n");

else {

Node* pPrev = NULL;

Node* pScan = pFirst;

if(pScan->pNext == NULL) {// It means we only have one node in the list free(pScan); // Free memory

pFirst= NULL; // Now the list is empty }

else {// Otherwise, I need to scan the list until I find the last node (pLast) do{

if((pScan-> pNext) == pLast) {// Reached the node before the end pPrev = pScan;

break;

}else

pScan= pScan-> pNext; // Otherwise, I need to iterate }while((pScan-> pNext) != NULL);

free(pPrev-> pNext); // Free memory allocated to the last node

pPrev-> pNext = NULL; // pPrev becomes the last node (no node after it) pLast = pPrev; // pPrev becomes the last node

} }

}

(28)

HOW IT RUNS

10 1000 5 NULL

7000

Node* pFirst

30 4000

30 NULL

4000

(pScan-> pNext) == pLast pPrev= pScan 1000

Yes free(pPrev-> pNext) 4000

pFirst == NULL? No pScan= pFirst 7000

pScan-> pNext== NULL? No pScan = pScan -> pNext 1000

Node* pLast

pPrev -> pNext= NULL pLast = pPrev 1000 1000

(29)

DELETE NODE IN SOME POSITION

10 1000 30 4000 5 NULL

7000 Node* pFirst

4000 Node* pLast

void delete_if_equal(Node* pFirst, Node* pLast, int key){…}

e.g., 30

1. Iterate until (pScan -> pNext) -> info == key 2. temp= (pScan-> pNext) -> next

3. free(pScan->next)

4. pScan-> pNext = temp

4000

temp = 4000

Update pFirst or pLast if instead you remove either the first or last node

(30)

ADD NODE IN SOME POSITION

10 1000 35 NULL

7000 Node* pFirst

4000 Node* pLast

30 4000

15 NULL1000 5000

5000

Homework!

Riferimenti

Documenti correlati

Swapping trigger algorithms can reduce trigger rate while increasing

Of course if we need to have add-or-remove from head and add-or-remove from tail, pLast needs to be updated also in add-or-remove from head (as pFirst in previous

In these studies two types of modified concretes in photocatalytic mineralization of anionic surfactant sodium dodecylbenzenesulfonate (NaDBS) were used.. Commercial

・ If you are using Windows, the location of the calculator drive will depend on your Windows Version.. Use Windows Explorer to open the

Cardiovascular and torso arrays are currently avail- able in confi gurations from 4 to 32 channels. The 32-channel versions permit very high two-direction speed-up capability,

The objective of the three-year FATA project, which started in 2013, was to compare the nitrogen removal performance of different types of free surface fl ow phytoremediation areas,

Ma ne furono soci anche studiosi, operatori, ingegneri, geometri, che parteciparono in massa al convegno di Roma appena citato, onorando il lavoro e la ricerca italiana

Questa fase del questionario fa ancora parte del primo livello o fase del modello di Kirkpatrick (reaction). Grazie alle domande contenute nella sezione C), si comprende se i