chapter 2 pointer & linked list. introduction if we have a set of data, we can keep it in an...

34
Chapter 2 Chapter 2 Pointer & Linked List Pointer & Linked List

Upload: kassidy-haddox

Post on 30-Mar-2015

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Chapter 2Chapter 2Pointer & Linked ListPointer & Linked List

Page 2: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

IntroductionIf we have a set of data, we can keep it in an

array. But the problem with array is, the size is fixedOverflowSize can’t be extendedWasted if unused

To overcome this – we use linked list.To understand linked list, we must first

understand the fundamentals – the pointer.

Page 3: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Pointer ? Variable concept

Declaring a variableMemory allocation

Pointer ?A variable which give location of other variable.

Linked List?Put a pointer in a structure, giving the next

location of the next structure.

Static variable vs. Dynamic variableStatic variable – declared and named in programDynamic – created during program execution.

Page 4: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Understanding PointersDeclare a pointer

int *aPtr;int *aPtr = null;

Assigning a pointeraPtr = &aVar;

Read the pointer valueprintf(“%d “, *aPtr);

Page 5: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Exercise1. Declare an integer variable a and b2. Declare pointer variables aPtr and

bPtr3. Assign a to has a value of 100, and b

to has a value of 2004. Assign aPtr to points to a5. Assign bPtr to points to aPtr6. By using bPtr, change value of a to be

the same as b.

Page 6: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Linked Lista pointer in a structure, giving the next

location of the next structure.

123

A C

Page 7: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Linked List - Declaration

Linked List consist of structureLlist – is a pointer, pointing to a linked

list structureNext is a pointer in the linked list

structure

Temp

Item Next Item Next

Llist

NULL

Page 8: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Structure Declarationtypedef char item;typedef struct node{

item data;struct node *next;

} Node;

Page 9: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Declaring the linked listItem Next

Llist

NULL

To declare a linked list

Node *Llist; //Llist –pointer pointing to a node type

Page 10: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Linked List Operation Create a Nod Verify for an empty list Traversal along the linked nodes Insert new nodes Delete a node

Page 11: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Creating New NodeNode *newnode (item c) {

Node *n;n = (Node *) malloc (sizeof (Node));if ( n != NULL) {

n-> data = c;n->next = NULL;

}return n;

}

Page 12: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Insert New Node at the beginingItem Next

Temp

NULL

Item Next Item Next

Llist

NULL

Item Next

Temp

X

Item Next Item Next

Llist

NULL

Page 13: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Inserting node in the middleItem Next

Temp

NULL

Item Next Item Next

Llist

NULLItem Next

CurrPtr

Item Next

Temp

Item Next Item Next

Llist

NULLItem Next

CurrPtr

Page 14: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Insert New Node - implementationvoid InsertNode( Node *Llist, Node *temp, Node

*CurrPtr){{

if (CurrPtr ==NULL) {temp->next = Llist;Llist = temp;

}else {

temp->next = CurrPtr->next;CurrPtr –>next = temp;

}}

Page 15: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Traverse The List

void Traversal ( Node *Llist) {Node *Temp;Temp = Llist;while ( Temp != NULL) {

printf (“data = %c", Temp->data);Temp = Temp-> next;

}}

Item Next Item Next

Llist

NULL

Item Next Item Next

Llist

NULL

Temp

Page 16: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Deleting first nodeTemp

Item Next Item NextLlist

NULL

Temp

Item Next Item Next

Llist

NULLX

Page 17: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Temp

Item Next Item Next

Llist

NULLItem Next

CurrPtr

Deleting middle or last node

Temp

Item Next Item Next

Llist

NULL

Item Next

CurrPtr

Page 18: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Deleting Node - implementationvoid DeleteNode( Node *Llist, Node *CurrPtr){Node *temp;

if (CurrPtr ==NULL) {temp = Llist;Llist = temp->next;

}else {

temp = CurrPtr->next;CurrPtr –>next = temp->next;

}free(temp)}

Page 19: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Type of Linked ListSimple one-way linked list

L

x1 x2 x3 x4

Page 20: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Type of Linked ListCircular Linked List

Formed by having the link in the l ast node of a one way linked list point back to the first node.

L

x1 x2 x3 x4

Page 21: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Type of Linked ListTwo Way Linked List

Formed from nodes that have pointers to both their left and right neighbours in the list

L

x3x1 x2

Page 22: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Type of Linked ListLinked List with Header Node

Header points to the first nodeAs a marker / stopping placeEase the deletion process of a node

L x1 x2 x3 x4L

Header Node

Page 23: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Exercise 2

Indicate whether the statement is TRUE or FALSE

1. The following code segment is to traverse and display all data from the linked list.

while (mybmi != NULL)

printf(“%d %.2f”, mybmi->age, mybmi->weight);

typedef struct bmi {

int age; float weight;

struct bmi *next;

} BMI;

BMI *mybmi;

Page 24: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Exercise 2 (cont..)

Indicate whether the statement is TRUE or FALSE

2. Assume q has been declared as a pointer of type BMI, then the statement to create a dynamic storage of type BMI is as follows:

q = (BMI*) malloc (sizeof(BMI));

typedef struct bmi {

int age; float weight;

struct bmi *next;

} BMI;

BMI *mybmi;

Page 25: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Exercise 3

1. Write code segment to delete a node with value 15.00.

2. Given, ListGame *temp;temp = (ListGame*) malloc (sizeof(ListGame));temp -> data = 12.0 ; temp->link = NULL;

Write a code segment to insert this node after the first node in above linked list.

Page 26: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Exercise 4

Given,struct Student{ char name [20];

float cgpa;Student *next;

};

Refer to the diagram, write code segment to delete the node Siew Chee.

Page 27: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Linked List Using ArrayOlder and widely used computer language

(COBOL, Fortran, BASIC) do not provide facilities for dynamic storage allocation (pointers)

Workspace (several arrays hold different part of a logical record) is used for programming languages which do not support records.

Page 28: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Linked List Using ArrayImplementation of linked list using array is

preferred if:Number of entries is known in advanceFew insertions or deletionsData are sometimes best treated as a linked

list and other times as a contiguous

Page 29: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Linked List Using Arraytypedef char ListEntry;typedef int ListIndex;typedef struct listnode{

ListEntry entry;ListIndex next;

} ListNode;typedef int Position;typedef struct list{

ListIndex head;int count;

}List;ListIndex avail,lastused;ListNode workspace[10]

Page 30: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Array Linked List – New NodeListIndex NewNode (void){

ListIndex newindex = -1;

if(avail != -1) {newindex = avail;avail = workspace[avail].next; workspace[newindex].next = 0;

} else if (lastused < MAXLIST - 1) {

newindex = ++lastused;workspace[newindex].next = 0;

} elseprintf (“ Error Overflow : workspace for linked list is

full”);return newindex;

}

Page 31: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Array Linked List - Insertvoid InsertList ( Position p, ListEntry x, List *list){

ListIndex newindex, previous;if ( p <0 || p > list->count) printf(“ Error inserting into a nonexistent position”);else { newindex = NewNode(); workspace[newindex].entry = x; if (p == 0) {

workspace[newindex].next = list->head; list->head = newindex;

} else {

SetPosition(p-1, &previous, list); workspace[newindex].next=workspace[previous].next;

workspace[previous].next = newindex; } list->count ++;

}}

Page 32: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Array Linked List - Disposevoid DisposeNode (ListIndex oldindex, List *list){

ListIndex previous;if( oldindex == -1)

printf(“Error : Disposing a nonexistent node”); else {

if ( oldindex == list-> head)list->head = workspace[oldindex].next;

else { SetPosition(CurrentPosition(oldindex,list)– 1, &previous, list);

workspace[previous].next=workspace[oldindex].next; }

workspace[oldindex].next = avail; avail = oldindex;

}}

Page 33: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Array Linked List - Traverse

void TraverseList (List *list){

ListIndex current;

for (current= list->head ; current != -1; workspace[current].next)printf(”data of workspace[%d].entry = %c”, current,

workspace[current].entry);}

Page 34: Chapter 2 Pointer & Linked List. Introduction If we have a set of data, we can keep it in an array. But the problem with array is, the size is fixed Overflow

Class ExerciseWrite a complete program to create, insert and

delete a linked list. Each list will consist of ID (an integer) and Grade (a character)

Guides Declare linked list structure Writes all the ADT funtions (newNode, InsertNode,

DeleteNode, TraverseNode) In main program:

Create new node Traverse the list Create another node Insert into the existing list Traverse list Delete the last node Traverse list Insert new node Traverse list