2 - Linked Data Representations
2 - Linked Data Representations
2 - Linked Data Representations
Manolis Koubarakis
Sequential Linked
Representations Representations
Pointer Parallel
Strings Arrays
Arrays Representations Arrays
of
Records
α: β β:
α:
β:
Data Structures and Programming
4
Techniques
Pointers in C
typedef int *IntegerPointer;
IntegerPointer A, B;
/* the declaration int *A, *B has the same effect */
A=(IntegerPointer)malloc(sizeof(int));
B=(int *)malloc(sizeof(int));
A:
B:
A: 5
B: 17
The unary operator * (τελεστής αναφοράς) on the left side of the assignment
designates the storage location to which the pointer A refers. We call this
pointer dereferencing.
X:
A: 3
The unary operator & (τελεστής διεύθυνσης) gives the address of some object
(in the above diagram the address of variable X).
A: 5
B: 17
A: 17 A: 5
B: 17 B: 17
A: 5
B: 17
Answer: The right diagram. Now A and B are called aliases because they name
the same storage location. Note that the storage block containing 5 is now inaccessible.
Modern programming languages have a garbage collection facility for such storage.
free(A);
A=B;
A:
B: 17
A:
B: 17
.
Question: Suppose now we call free(B). What is the value of *A+3 then?
A:
?
B:
A: .
Now we cannot access the storage location to which A pointed to earlier. So
something like *A=5; will give us “segmentation fault”.
NULL is automatically considered to be a value of any pointer type that can be defined in
C. NULL is defined in the standard input/output library <stdio.h> and has the value 0.
Temp=X;
X=Y;
Y=Temp;
}
In Swap: P: Q:
Temp=*P;
*P=*Q;
*Q=Temp;
}
AirportCode C;
NodePointer L;
strcpy(C, “BRU”);
printf(“%s\n”, C);
L=(NodePointer)malloc(sizeof(NodeType));
strcpy(L->Airport, C);
printf(“%s\n”, L->Airport);
NodePointer L, M;
L=(NodePointer)malloc(sizeof(NodeType));
strcpy(L->Airport, “DUS”);
M=(NodePointer)malloc(sizeof(NodeType));
strcpy(M->Airport, “ORD”);
L->Link=M;
M->Link=NULL;
M:
L: DUS ORD .
Airport Link
BRU
N=(NodeType *)malloc(sizeof(NodeType));
Airport Link
N: ? ?
strcpy(N->Airport,”BRU”);
Airport Link
N: BRU ?
N: BRU X ?
N: BRU
N=L;
while (N != NULL){
if (strcmp(N->Airport,A)==0){
return N;
} else {
N=N->Link;
}
}
return N;
}
N:
N:
N: .
Airport Link Airport Link
.
Airport Link
N: .
Airport Link Airport Link
.
Airport Link
if (*L != NULL) {
if ((*L)->Link == NULL){
free(*L);
*L=NULL;
} else {
PreviousNode=*L;
CurrentNode=(*L)->Link;
while (CurrentNode->Link != NULL){
PreviousNode=CurrentNode;
CurrentNode=CurrentNode->Link;
}
PreviousNode->Link=NULL;
free(CurrentNode);
}
}
}
PreviousNode: CurrentNode:
X X
Airport Link Airport Link
.
Airport Link
L:
Data Structures and Programming
56
Techniques
Why **?
• This is for the case that the list in the calling
function has one node only.
• Then, the value of pointer A to the only
element of that list must be set to NULL in
the function DeleteLastNode.
• This can only be done by passing &A in the call
of the function DeleteLastNode.
N=(NodeType *)malloc(sizeof(NodeType));
strcpy(N->Airport, A);
N->Link=NULL;
if (*L == NULL) {
*L=N;
} else {
P=*L;
while (P->Link != NULL) P=P->Link;
P->Link=N;
}
printf(“(“);
N=L;
while(N != NULL) {
printf(“%s”, N->Airport);
N=N->Link;
if (N!=NULL) printf(“,”);
}
printf(“)\n”);
}
/* function prototypes */
void InsertNewLastNode(char *, NodeType **);
void DeleteLastNode(NodeType **);
NodeType *ListSearch(char *, NodeType *);
void PrintList(NodeType *);
L=NULL;
PrintList(L);
InsertNewLastNode(“DUS”, &L);
InsertNewLastNode(“ORD”, &L);
InsertNewLastNode(“SAN”, &L);
PrintList(L);
DeleteLastNode(&L);
PrintList(L);
if (ListSearch(“DUS",L) != NULL) {
printf(“DUS is an element of the list\n");
}
}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: