View
221
Download
0
Tags:
Embed Size (px)
Citation preview
2
Chapter 12: Structures and ADTs
Outline Declaring Structures Accessing a Member in a structure variable Initialization of Structures Self-Referential Structures Linear Linked Lists The use of typedef
3
Self-Referential Structures
self-referential structures structures with pointer members that point
to the structure type containing them. Example:
struct list{ int data; struct list *pNext; } a, b, c;
member pNext points to the structure type struct list, which contains pNext as a member
struct list is a self-referential structure.
4
Self-Referential Structures
Using self-referential structures to implement linear linked lists
1
&b
a
2
&c
b
3
NULL
c
struct list{ int data; struct list *pNext; } a, b, c; a.data=1; b.data=2; c.data=3; a.pNext = &b; b.pNext = &c; c.pNext = NULL;
data
pNext
data
pNext
data
pNext
5
Linear Linked Lists
What is linear Linked List?How to implement linear linked lists
create a list counting and lookup insertion deletion
6
Linear Linked Lists
What is Linear Linked List? a list on which data structures hang
sequentially.a head pointer that points to the first
element of the list, each element points at a successor
element,the last element having a link value
NULL.
pHead
struct list{ int data; struct list *pNext; } a, b, c;
1
&b
2
&c
3
NULL
data
pNext
data
pNext
data
pNext
7
Linear Linked Lists
Linear Linked Lists A linked list is a very common data
structure. It can be used to implement efficient
algorithms, such as sorting, searching.
8
Linear Linked Lists
What is linear Linked List?How to implement linear linked lists
create a list counting and lookup insertion deletion
9
Linear Linked Lists
How to implement linear linked lists Consider the following list:
struct linked_list{ char data; struct linked_list *pNext;};
pHead
data
data
data
data
data
data …………
NULL
10
Linear Linked Lists
Operations on a linked listDefine functions such that create a list
from a value of type charfrom an array of type char
counting: the number of elements looking up an element inserting an element deleting an element
struct linked_list{ char data; struct linked_list *pNext;};
11
Linear Linked Lists
Operations on a linked list create a list from a value:
struct linked_list *create_value(char d);create a list that contains a single item;the value of member data in this item is equal to
dthe head pointer of this list is returned.
struct linked_list * pHead;pHead = create_value(‘A’);
pHead
‘A’
NULL
struct linked_list{ char data; struct linked_list *pNext;};
12
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_value(char data);#include "list.h"int main(){ struct linked_list *pHead; pHead = create_value('A'); …….}
list.h
main.c
#include "list.h"struct linked_list *create_value(char data){ struct linked_list *pHead = NULL; pHead = (struct linked_list *) malloc(sizeof(struct linked_list)); pHead->data = data; pHead->pNext = NULL; return pHead;}
list.c
pHead
‘A’
NULL
13
Linear Linked Lists
Operations on a linked list create a linked list from an array:
struct linked_list *create_array(char data_array[], int n);
a list that contains n items is created.the member data of each item is set according to
data_array.the head pointer of this list is returned.
char data_array[]={'a', 'b', 'c', 'd', 'e'};struct linked_list * pHead;
pHead = create_array(data_array, 5);
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’
NULL
struct linked_list{ char data; struct linked_list *pNext;};
14
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_array(char data_array[],
int n);
#include "list.h"int main(){ struct linked_list *pHead; char data_array[]={'a', 'b', 'c', 'd', 'e'}; pHead = create_array(data_array, 5); ……}struct linked_list *create_array(char data_array[], int n){
struct linked_list *p=NULL, *pHead = NULL; int i; if(n==0) return NULL; else{ pHead = (struct linked_list *) malloc(sizeof(struct linked_list)); pHead->data = data_array[0]; pHead->pNext = NULL; p = pHead; for (i=1; i<n;i++){ p->pNext = (struct linked_list *) malloc(sizeof(struct linked_list)); p->pNext->data = data_array[i]; p->pNext->pNext = NULL; p = p->pNext; } } return pHead;}
list.h main.c
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’NULL
list.c
15
Linear Linked Lists
Operations on a linked list count the elements in a list
int count(struct linked_list *pHead);Given a list, the head of which is pointed at by
pHead, the number of elements in this list is returned.
The value of count(pHead) is equal to 5
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’
NULL
struct linked_list{ char data; struct linked_list *pNext;};
16
int count(struct linked_list *pHead){ int i=0; while(pHead!=NULL){ pHead = pHead->pNext; i++; } return i;}
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_array(char data_array[], int n);int count(struct linked_list *pHead);
#include "list.h"int main(){ struct linked_list *pHead; char data_array[]={'a', 'b', 'c', 'd', 'e'}; pHead = create_array(data_array, 5); printf("%d", count(pHead));}
list.h
list.c
main.c
17
Linear Linked Lists
Operations on a linked list Search a list for a particular element.struct linked_list* lookup(char data, struct linked_list *pHead);
If the element is found, a pointer to that element is returned; otherwise the NULL pointer is returned.
The value of lookup(‘b’, pHead) is a pointer pointing to the second elements
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’
NULL
struct linked_list{ char data; struct linked_list *pNext;};
18
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_array(char data_array[], int n);struct linked_list* lookup(char data, struct linked_list *pHead);struct linked_list* lookup(char data, struct linked_list *pHead){ while(pHead!=NULL){ if (pHead->data == data) return pHead; pHead = pHead->pNext; } return pHead;}
#include "list.h"int main(){ struct linked_list *pHead; struct linked_list *p; char data_array[]={'a', 'b', 'c', 'd', 'e'}; pHead = create_array(data_array, 5); p = lookup(‘c', pHead); if (p!=NULL) printf("Look UP: ‘c' is found \n"); else printf("Look UP: ‘c' is not found \n");}
list.h
main.c
list.c
19
Linear Linked Lists
Operations on a linked list inserting an element
void insert(struct linked_list *p, char d);
Insert the data after the element pointed at by pBefore
After insert(p, ‘A’);
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’NUL
Lp
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’NUL
Lp
‘A’
struct linked_list{ char data; struct linked_list *pNext;};
20
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_array(char data_array[], int n);void insert(struct linked_list *p, char data);
void insert(struct linked_list *p, char data){/*Insert the data after the element pointed at by p*/
struct linked_list *pData; pData = (struct linked_list *) malloc(sizeof(struct linked_list)); pData -> data = data; pData->pNext = p->pNext; p->pNext = pData;}
#include "list.h"int main(){ struct linked_list *pHead = NULL; struct linked_list *p; char data_array[]={'a', 'b', 'c', 'd', 'e'}; pHead = create_array(data_array, 5); insert(pHead, 'A'); ………}
list.c
main.c
list.h
21
Linear Linked Lists
Operations on a linked list Deleting an element
void delete (struct linked_list *p);
delete the element pointed at by p->pNext.Before
After delete(p)
pHead
‘a’ ‘b’ ‘c’ ‘d’ ‘e’NUL
Lp
pHead
‘a’ ‘b’ ‘c’ ‘e’NUL
Lp
struct linked_list{ char data; struct linked_list *pNext;};
22
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_array(char data_array[], int n);void delete(struct linked_list *p);
void delete(struct linked_list *p){ /* delete the element pointed at by p->pNext. */ struct linked_list *q; q = p->pNext; p -> pNext = q -> pNext;}
#include "list.h"int main(){ struct linked_list *pHead; struct linked_list *p; char data_array[]={'a', 'b', 'c', 'd', 'e'}; pHead = create_array(data_array, 5); delete(pHead->pNext); delete(pHead); …………}
main.c
list.c
list.h
23
Linear Linked Lists
Operations on a linked list Release memory allocated to the list Dynamic Memory Allocation: space allocated
by calloc() and malloc() remains in use for the duration of the program unless it is released by the programmer.void release_list(struct linked_list *pHead)
o release the space allocated to the elements in the list.
struct linked_list{ char data; struct linked_list *pNext;};
24
#include <stdio.h>struct linked_list{ char data; struct linked_list *pNext;};struct linked_list *create_array(char data_array[], int n);void release_list(struct linked_list *pHead);
void release_list(struct linked_list *pHead){ struct linked_list *p; while (pHead !=NULL){ p = pHead; pHead = pHead->pNext; p -> pNext = NULL; free(p); }}
#include "list.h"int main(){ struct linked_list *pHead; struct linked_list *p; char data_array[]={'a', 'b', 'c', 'd', 'e'}; pHead = create_array(data_array, 5); ………… release_list(pHead); …………}
25
Linear Linked Lists
Summary What is linear Linked List How to implement linear linked lists
create a linked listcounting and lookupinsertion and deletion
struct linked_list{ char data; struct linked_list *pNext;};
26
Chapter 12: Structures and ADTs
Outline Declaring Structures Accessing a Member in a structure variable Initialization of Structures Self-Referential Structures Linear Linked Lists The use of typedef
27
The Use of typedef
typedef an identifier can be associated with a specific type Example:
typedef char DATA;DATA a, b, c; /* char a,b,c; */
struct linked_list{ char data; struct linked_list *pNext;
};typedef struct linked_list ELEMENT;typedef ELEMENT * LINK;LINK pHead;