






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







1
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
data
next
has value
(^2)
a.next->next->data
has value
(^3)
b
a
c
NULL
b
a
c
NULL
3
PROGRAM MEMORY MEMORY
main
called (^) and
functionsstandard
global
heapprogram
stack^ system
DATA MEMORY
7
struct node *next;int data;
ptr = (struct node *)struct node *ptr;};
malloc(sizeof(struct node));
(^) 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 ?
โ (^) 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
9
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
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;
};
13
39
pPre pHeadpNew
(^75)
124
39
pPre pHeadpNew
(^75)
124
96
pPre pNew
(^75)
124
96
pPre pNew
(^75)
124
15
134
pPre pNew
(^75)
124 134
pPre pNew
(^75)
124
16
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
}
19
(^75)
124
96
pPre
pCur
(^75)
124
Recycled
pPre
pCur
if (pPre == NULL) pHead = pCur->next;// Deleting first node
else pPre->next = pCur ->next;// Deleting other nodes
free(pCur);
21
pCur = pCur->next;pPre = pCur;
if (pCur)//Determine if target is found} found = 1;
else found = 0;
pWalker = pWalker ->next;printf(โ%d โ, pWalker->data);
Reference:}