The Code in Data book (5th Edition) from the 49 page to 52 page
#define MaxSize 50
typedef int ElemType;
typedef struct LNode {
ElemType data; //Saved element values
struct LNode* next; //Push to the next node
} LinkNode; //Single-list node type
//Head-inserting
void CreateListF(LinkNode *&L, ElemType a[], int n) {
LinkNode *s;
L = (LinkNode * )malloc(sizeof(LinkNode));
L->next = NULL; //Create a head node, it's next domain is set to NULL
for (int i = 0; i < n; i++) { //Loop create data node s
s = (LinkNode * )malloc(sizeof(LinkNode));
s->data = a[i]; //Assignment
s->next = L->next; //Insert node s before the original first node, after the head node
L->next = s;
}
}
//Tail-insertion
void CreateListR(LinkNode *&L, ElemType a[], int n) {
LinkNode *s, *r;
L = (LinkNode *)malloc(sizeof(LinkNode)); //Create a head node
r = L; //r Always point to the end node
for (int i = 0; i < n; i++) { //Loop create node s
s = (LinkNode *)malloc(sizeof(LinkNode));
s->data = a[i]; //Assignment
r->next = s; //Insert node s after node r
r = s;
}
r->next = NULL; //Tail-insertion it's next domain set to NULL
}
//Initialization Linknode
void InitList(LinkNode*& L) {
L = (LinkNode*)malloc(sizeof(LinkNode));
L->next = NULL; //Create Head-inserting, it's next domain set to NULL
}
//Destroyed Linknode
void DestroyList(LinkNode*& L) {
LinkNode *pre = L, * p = L->next; //pre points to node p's forward node
while (p != NULL) { //Scan Linknode L
free(pre); //Release pre node
pre = p; //pre,p sync and move a node
p = pre->next;
}
free(pre); //At the end of the loop p is NULL, pre points to the end node to release it
}
//Determine if a linear table is empty
bool ListEmpty(LinkNode* L) {
return L->next == NULL;
}
//Calculation length of the Linknode
int ListLength(LinkNode* L) {
int n = 0;
LinkNode *p = L; //p point to Head-inserting,set n is 0
while (p->next != NULL) { //Not empty, traverse is turn
n++;
p = p->next;
}
return n; //return L's length n
}
//Output Linknode
void Display(LinkNode* L) {
LinkNode* p = L->next; //p point to Head-inserting
while (p != NULL) { //Not empty, traverse is turn
printf("%d ", p->data);
p = p->next; //p move to next node
}
printf("\n");
}
//Find a data element value
bool GetData(LinkNode *L, int i, int &e) {
int j = 0;
LinkNode *p = L; //p point to Head-inserting
if (i <= 0) { //If i error return false
return false;
}
while (j < i && p != NULL) { //find the ith node p
j++;
p = p->next;
}
if (p == NULL) { //If doesn't
return false;
}
else { //If exust return true
e = p->data;
return true;
}
}
//Find by element value
int LocateData(LinkNode *L, ElemType e) {
int i = 1;
LinkNode* p = L->next; //p point to Head-insterting
while (p != NULL && p->data != e) { //Find for the data value with the serial number i
p = p->next;
i++;
}
if (p == NULL) { //If doesn't
return 0;
}
else { //If exuse return number i
return i;
}
}
//Insert data elements
bool ListInsert(LinkNode *&L, int i, ElemType e) {
int j = 0;
LinkNode *p = L, *s; //p point to Head-inserting
if (i <= 0) { //If i error return false
return false;
}
while (j < i - 1 && p != NULL) { //find the number i-1 node p
j++;
p = p->next;
}
if (p == NULL) { //If not found
return false;
} else { //Find number i-1 node p,insert new node and return true
s = (LinkNode * )malloc(sizeof(LinkNode));
s->data = e; //Create new node s, 创建新结点 s,value is data
s->next = p->next; //Head-inserting
p->next = s;
return true;
}
}
//Delete data elements
bool ListDelete(LinkNode*& L, int i, ElemType &e) {
LinkNode* p = L, * q; //p point to Head-inserting
if (i <= 0) { //if i error return false
return false;
}
int j = 0; //set j is 0
while (j < i - 1 && p != NULL) { //Find number i node p
j++;
p = p->next;
}
if (p == NULL) { //Not found
return false;
}
else { //Find number i-1 node p
q = p->next; //q point to number i th node
if (q == NULL) { //If number i node it doesn't
return false;
}
e = q->data;
p->next = q->next; //Delete q node,number i-1 node point to number i+1 node
free(q); //Release q node
return true;
}
}
By Honework 2如有侵权,请联系作者删除