1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data;...

28
1 LIST PROCESSING

Transcript of 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data;...

Page 1: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

1

LIST PROCESSING

Page 2: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

Senem Kumova Metin 2

Self Referential Structures 1/4

struct node { int data; struct node *next; };

struct node a,b;

a b

a.data=1; b.data=2; a.next= b.next=NULL; // a and b do not point to

some other node

a b

A structure of type struct node

data next

data nextdata next

2 NULL1 NULL

Page 3: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

3

Self Referential Structures 2/4

// next is a pointer to a “struct node” object

a.next= &b; // next node for a is b// as a result b = *(a.next)

a b

*(a.next).data =? a.next->data =?

2 1 &b

a.next=&b

NULL

Page 4: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

4

Self Referential Structures 3/4struct node { int data; struct node *next; };

struct node * p1 ; struct node * p2;

// Create objects using pointers, pointers store the address of each object

p1= (struct node *)malloc(sizeof(struct node)); p2= (struct node *)malloc(sizeof(struct node));

p1->data = 4; p2->data= 5;

p1->next= p2; // p2 indicates adress of objectP2->next =NULL;

data=5 // next=NULL

data=4 // next=p2

p1 p2

Page 5: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

5

Self Referential Structures 4/4

struct node * p3= malloc(sizeof(struct node));

p3->data=1; p3->next=NULL;p2->next= p3;

p1->next == p2p1->next->next== p3

p2->data == p1->next->datap3->data == p1->next->next-

>data

data=5 // next=p3

data=4 // next=p2

p1 p2 p3

data=1 // next=NULL

Page 6: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

6

Linear Linked Lists

Linear linked list is a data structure of explicit ordering of items (nodes)

Each item(node) contains two portions:• information(data) portion • next address portion

Generally the variable head contains an address or pointer that gives the location of the first node of the linked list

next NULL

data

nextdata

nextdata

head head->next

Page 7: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

7

Linear Linked Lists : Definition

struct node { int data; struct node *next; };

// type name for new type is “struct node”

struct node * head; // declares the pointer for first node (head)

next NULL

data

nextdata

nextdata

head head->next

Page 8: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

8

Linear Linked Lists : 2 nodes in main() struct node

{ int data; struct node *next; };

main(){ struct node * head;

/* Create List */head = (struct node *)malloc(sizeof(struct node)); head->data=1;head->next=NULL;

/* Add 1st element */head->next= (struct node *) malloc(sizeof(struct

node)); head->next->data =2;head->next->next=NULL;

}

data=1 next=NULL

head

data=2

next=NULL

data=1

next=&

head head->next

Page 9: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

9

Linear Linked Lists : Create and fill 3 nodes in main()

typedef struct node { int data; struct node *next; } NODE;

main() { NODE * head;

head = malloc(sizeof(NODE)); head->data=1; head->next=NULL;

/* Add 1st element */head->next= malloc(sizeof(NODE)); head->next->data =2;head->next->next=NULL;

/* Add 2nd element */head->next->next = malloc(sizeof(NODE)); head->next->next->data =3;head->next->next->next=NULL;

}

data=1 next=NULL

head

data=2

next=0

data=1

next=&

head head->next

data=2

next=&

data=1

next=&

head

data=2

next=0

Page 10: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

10

Linked List TypesSingle linked lists (next)

NULL

Headnext

nextprevious previous

next

Head Tail next

NULL

next

Double linked lists (next + previous)

Circle linked lists (next)

Head next Tail next

Page 11: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

11

Basic Linear Linked List Operations

1. Creating a list 2. Counting elements of list3. Printing data in list4. Inserting elements(nodes) to lists5. Deleting elements(nodes) of list6. Finding/searching elements in the

list7. Sorting elements8. etc..

Page 12: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

12

CREATING LINEAR LINKED LIST

An integer array (x[4]) will be used to create a list.

Example 1 : In main() add from head

Example 2 : In main() add from tail

Example 3 : In function ( Using Iteration)

Example 4 : In function ( Using Recursion)

How to call functions in Example 3 and 4

Page 13: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

Senem Kumova Metin 13

Example 1: In main() add from headtypedef struct node {int data; struct node *next; } NODE;

main(){ int x[4]={1,2,3,4};

NODE * head=NULL; NODE* tmp=NULL;

for (i=0; i<4; i++)

{ if(head==NULL) // FIRST NODE IN LIST

{ head=malloc(sizeof(NODE));

head->data=x[i];

head->next =NULL; }

else { tmp=malloc(sizeof(NODE));

tmp->data=x[i];

tmp->next=head;

head=tmp; }

}

}

Page 14: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

Senem Kumova Metin 14

Example 2 : In main() add from tail typedef struct node {int data; struct node *next; } NODE;

main()

{ int x[4]={1,2,3,4};

NODE * head=NULL; NODE * tail=NULL;

for (i=0; i<4; i++)

{ if(head==NULL) // FIRST NODE IN LIST

{ head=malloc(sizeof(NODE));

head->data=x[i];

head->next =NULL;

tail=head; }

else { tail->next=malloc(sizeof(NODE));

tail=tail->next;

tail->data=x[i];

tail->next=NULL; }}

Page 15: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

Senem Kumova Metin 15

Example 3 : In function ( Using Iteration)NODE * create_ite (int x[] , int size)

{ NODE * head = NULL; NODE * tail =NULL; int i;

if(size!=0)

{ head = malloc(sizeof(NODE));

head -> data = x[0];

tail = head;

for (i = 1; i<size; ++i)

{ /* add to tail */

tail -> next = malloc(sizeof(NODE));

tail = tail -> next;

tail -> data = x[i]; }

tail -> next = NULL; /* end of list */

}

return head; }

Page 16: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

16

Example 4 : In function (Using Recursion)

NODE * create _rec(int x[], int size)

{ NODE * head;

if (size==0 ) /* base case */

return NULL;

else { /* method */

head = malloc(sizeof(NODE));

head -> data = x[0];

head -> next = create_rec(x + 1 , size-1);

return head;}

}

Page 17: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

17

How to call create functions in Example 3 and 4

typedef struct node { int data; struct node *next; } NODE;

NODE * create_ite (int x[] , int size) ; // prototype for iterative function

NODE * create _rec(int x[], int size) ; // prototype for recursive function

main(){

int x[4]={1,2,3,4}; NODE * head1;NODE * head2;

head1=create_ite(x,4);head2=create_rec(x,4); }

Page 18: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

18

Count Elements of a List

Example 1 : Using IterationExample 2: Using RecursionHow to call them in main()

Page 19: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

19

Example 1 : Iterationint count_list_ite (NODE * head)

{ int count=0;

for (; head != NULL; head = head -> next)

++count;

return count; }

Example 2 : Recursion

int count_list_rec (NODE * head)

{ if (head == NULL) return 0;

else return(1 + count_list_rec(head -> next)); }

Page 20: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

20

How to call count functions in Example 1 and 2

typedef struct node { int data; struct node *next; } NODE;

int count_list_ite (NODE * head); // prototype for iterative function

int count_list_rec (NODE * head); // prototype for recursive function

main(){

int x[4]={1,2,3,4}; int size1, size2 ;NODE * head;

head=create(x,4);size1= count_list_ite(head);size2= count_list_rec(head); }

Page 21: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

21

Print Elements of a List

Example 1 : Using IterationExample 2: Using Recursion

Page 22: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

22

Example 1 : Using Iteration

void print_ite (NODE * head)

{ NODE * p;

if (head == NULL)

printf(“NULL list”);

else { for (p = head; p != NULL; p = p -> next)

printf(“%d\n ”, p -> data); }

}

Page 23: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

23

Example 2 : Using Recursion

void print_rec (NODE * head)

{

if (head == NULL) printf(“NULL list”);

else { printf(“%d\n”, head -> data);

print_rec(head ->next);

}

}

Page 24: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

24

Insertion of Elements in a List

void insert(NODE * p1, NODE * p2, NODE * q)

{ assert (p1-> next == p2);/* if the expression inside assert is false, the

system will print a message and the program will be aborted */

p1->next = q;

q->next = p2; }

B next

C next

NULL

A next

q

p1

p2

initially

Page 25: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

25

How to call insert functiontypedef struct node

{ int data; struct node *next; } NODE;

/* Function prototypes */ NODE * create_rec( int x[], int size);

void insert(NODE * p1, NODE * p2, NODE * q)

void print_ite (NODE * head)

main(){ int x[4]={1,2,3,4};

NODE * head;NODE n; n.data=7; n.next=NULL;head=create(x,4);

insert(head->next,head->next->next, &n);print_ite(head); }

Page 26: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

26

Delete Elements of a List

Example 1 : Using IterationExample 2: Using Recursion

Page 27: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

27

Example 1 : Using Iteration

void delete (NODE * head)

{ NODE * p; NODE * q;

if (head == NULL) printf(“NULL list”);

else { p=head;

while (p != NULL;)

{ q=p;

p = p -> next ;

free(q); } }

}

Page 28: 1 LIST PROCESSING. Senem Kumova Metin2 Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; ab a.data=1; b.data=2;

28

Example 2 : Using Recursion

void delete (NODE * head)

{

if (head != NULL)

{ delete(head ->next);

free(head);

}

}