Docsity

Linked Data Representations-Data Structures and Representation-Lecture Slides, Slides of Data Structures and Algorithms

Toggle sidebar
bg1
1
Lecture Notes on
Linked Data Representations
We can define structures with pointer fields that refer to the
structure type containing them.
struct list {
int data;
struct list *next;
}
The pointer variable next is called a link. Each structure is
linked to a succeeding structure by way of the field next.
The pointer variable next contains an address of either the
location in memory of the successor struct list
element or the special value NULL.
data next
2
Example:
struct list a, b, c;
a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;
a.next = &b;
b.next = &c;
a.next->data has value 2
a.next->next->data has value 3
b
a c
NULL NULL NULL 3 2 1
b
a c
NULL 3 2 1
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Linked Data Representations-Data Structures and Representation-Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

1

Lecture Notes on

Linked Data Representations

struct list {structure type containing them. We can define structures with pointer fields that refer to the struct list *next;int data;

The pointer variable}

(^) next (^) is called a

(^) link. (^) Each structure is

linked to a succeeding structure by way of the field

(^) next .

The pointer variable

(^) next (^) contains an address of either the

location (^) in (^) memory

(^) of (^) the (^) successor

(^) struct

(^) list

element or the special value

NULL

data

next

a.next->data b.next = &c; a.next = &b; a.next = b.next = c.next = NULL;c.data = 3;b.data = 2;a.data = 1; struct list a, b, c; Example:

has value

(^2)

a.next->next->data

has value

(^3)

b

a

c

NULL

NULL

NULL

b

a

c

NULL

docsity.co

3

Static and Dynamic Variables

โ€ข Static Variables:

int^ โˆ’^ Names are associated with them.execution of the program.^ โˆ’^ They cannot be allocated/reallocated during thememory is reserved for them.)^ โˆ’^ They are created during compilation. (Fixed

x;

char

y[10];

int

z[100];

โ€ข Dynamic Variables:

โˆ’ They

are

created

(allocated)

and

reallocated

dynamicand setting a pointer to point to it, or returning aabout creating a new dynamic variable of type xtype like any other variable. Thus we can talkare created they contain data and must have a^ โˆ’^ They donโ€™t exist during compilation. Once theyway to access them is by using pointers.^ โˆ’^ No names are associated with them. The onlyduring the execution of the program.

variable

of

type

x

to

the

system

(reallocation).

A Conceptual View of Memory

PROGRAM MEMORY MEMORY

main

called (^) and

functionsstandard

global

heapprogram

stack^ system

DATA MEMORY

docsity.co

7

struct node{ Example:

struct node *next;int data;

ptr = (struct node *)struct node *ptr;};

malloc(sizeof(struct node));

  • (^) Function

(^) free (^) deallocates memory- i.e. the memory is

returned (^) to (^) the (^) system (^) so (^) that (^) the (^) memory

(^) can (^) be

free(ptr);e.g.reallocated in the future.

??

ptr ptr ?

Linked Lists

โˆ’ (^) Linkedphysically in the memory. โˆ’ (^) Each node does not necessarily follow the previous oneincrease or decrease as necessary. โˆ’ (^) Linked lists are dynamic, so the length of a list canunpredictable.elements to be represented in the data structure at once is โˆ’ (^) Linked lists are appropriate when the number of dataLinked Lists โˆ’ (^) Inserting and deleting elements into an array is difficult.sometimes impossible. โˆ’ (^) Maximum size of the array must be predicted which isbig enough (overflow problem) โˆ’ (^) Arrays are fixed size: either too big (unused space or notphysically (i.e. contiguous spaces in the memory) โˆ’ (^) In an array each node (element) follows the previous oneArraysprovide several advantages: โ€ข (^) Lists of data can be stored in arrays, but linked listseach node is linked to the node following it. โ€ข (^) An abstraction of a list: i.e. a sequence of nodes in which โ€ข (^) It is an important data structure.

(^) lists (^) can (^) be (^) maintained

(^) in (^) sorted (^) order (^) by

list.inserting or deleting an element at the proper point in the

docsity.co

9

Simple Linked List

has a link value NULL.and each node points at a successor node. The last nodeThe head pointer addresses the first node of the list,

head = NULL;NULL. Empty Linked list is a single pointer having the value of Empty List^ head

head

Nodes

A node with one data fieldnumber that contains the address of the next node in the sequence.fields. One of the fields is a data field; the other is a pointer A node in a linked list is a structure that has at least two

struct structint (^) number; (^) node{ (^) node (^) * link;

};

name (^) id (^) grdPts

A node with 3 data fields

struct char student{ name[20];

int (^) id;

double (^) grdPts;

struct *next_student; (^) student

};

name (^) address

(^) phone

structA structure in a node char person{ name[20];

char (^) address[30];

char (^) phone[10];

struct}; struct (^) person_node{ (^) person (^) data;

struct (^) person_node

(^) *next;

};

docsity.co

13

AFTERpHead = pNew;pNew->next = pHead; // Same code BEFORE Add Node at Beginning

39

pPre pHeadpNew

(^75)

124

39

pPre pHeadpNew

(^75)

124

AFTERpPre->next = pNew; pNew->next = pPre->next; BEFORE Add Node in Middle

96

pPre pNew

(^75)

124

96

pPre pNew

(^75)

124

docsity.co

15

AFTERpPre->next = pNew;pNew->next = pPre->next;middle // Same as the code for adding a node in the OR:pPre->next = pNew; pNew->next = NULL; BEFORE Add Node at End

134

pPre pNew

(^75)

124 134

pPre pNew

(^75)

124

16

Inserting a Node to a Linked List

Given the head pointer (

pHead ), the predecessor (

pPre )

and the data to be inserted (

item ), we must allocate

memory for the new node (

pNew ) and adjust the link

// Insert a node into a linked listpointers. pNew =(struct node *) malloc(sizeof(structstruct node *pNew;

node));

//Adding before first node or to empty listif (pPre == NULL){pNew->data = item; pHead = pNew;pNew->next = pHead;

else {} pPre->next = pNew;pNew->next = pPre->next;// Adding in middle or at end

}

docsity.co

19

AFTERfree(pCur); pPre->next = pCur->next; BEFORE General Delete Case

(^75)

124

96

pPre

pCur

(^75)

124

Recycled

pPre

pCur

// Delete a node from a linked listnode and recycle its memory.deleted, and the delete nodeโ€™s predecessor, we delete the Given a pointer to the head of the list, the node to be Deleting a node

if (pPre == NULL) pHead = pCur->next;// Deleting first node

else pPre->next = pCur ->next;// Deleting other nodes

free(pCur);

docsity.co

21

while (pCur != NULL && pCur->data != target){// the end of list// Search until target is found or we reachpCur = pHead;pPre = NULL; // Search nodes in a linked listvalue, the search return true; otherwise it returns false.node in the linked list. If a node in the list matches the target Given a target value, the search attempts to locate the requested Basic Search Concept node to be deleted and its logical predecessor. โ€ข To delete a node, we must identify the location of theof the new node. โ€ข To add a node, we must identify the logical predecessor Both insert and delete operations have to search the linked list. Search Linked List

pCur = pCur->next;pPre = pCur;

if (pCur)//Determine if target is found} found = 1;

else found = 0;

while (pWalker){printf(โ€œList contains:\nโ€);pWalker = pHead;struct node *pWalker; // Traverse a linked list List traversal requires that all of the data in the list be processed. Traversing Linked Lists

pWalker = pWalker ->next;printf(โ€œ%d โ€, pWalker->data);

Reference:}

  1. (^) Data Structure through โ€œCโ€2. (^) Data Structure through C in depth1. (^) Muhammad Azam, CIIT Abbottabad

docsity.co