数据结构代码第七章
代码1
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(int argc, char **argv) 5 { 6 int size; 7 printf("please input the list size: "); 8 scanf("%d", &size); 9 int *list[size]; 10 for(int i = 0; i < size; i++){ 11 list[i] = (int *)calloc(size, sizeof(int)); 12 for(int j = 0; j < size; j++){ 13 *(list[i] + j) = i + j + 10; 14 } 15 } 16 17 printf("Displaying the values of items in list\n"); 18 for(int i = 0; i < size; i++){ 19 printf("List[%d]: \t", i); 20 for(int j = 0; j < size; j++){ 21 printf("%d \t", *(list[i] + j)); 22 } 23 printf("\n"); 24 } 25 26 return 0; 27 }
第二个:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct _memb{ 6 char name[20]; 7 struct _memb *next; 8 }; 9 10 typedef struct _memb Memb; 11 12 void displayList(Memb *start){ 13 int flag = 1; 14 15 do{ 16 printf("%s\n", start->name); 17 if(NULL == start->next){ 18 flag = 0; 19 } 20 start = start->next; 21 }while(flag); 22 23 return; 24 } 25 26 int main(int argc, char **argv) 27 { 28 Memb *temp; 29 Memb *head; 30 static Memb *start; 31 const char *name[] = {"lina", "mina", "nina", "bina", "tina"}; 32 int size = sizeof(name)/sizeof(name[0]); 33 //int size = 5; 34 35 for(int i = 0; i < size; i++){ 36 37 } 38 temp->next = NULL; 39 40 printf("Names of all the members: \n"); 41 displayList(start); 42 43 return 0; 44 }
第三个:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 typedef struct _stu{ 6 char name[10]; 7 struct _stu *next; 8 } Stu; 9 10 int main(int argc, char **argv) 11 { 12 Stu *head; 13 Stu *tmp; 14 head = tmp = (Stu *)malloc(sizeof(Stu)); 15 tmp->next = NULL; 16 17 char *stuName[] = {"lina", "mina", "bina", "tina", "dina"}; 18 int size = sizeof(stuName)/sizeof(stuName[0]); 19 20 for(int i = 0; i < size; i++){ 21 strcpy(tmp->name, stuName[i]); 22 Stu *tmpN = (Stu *)malloc(sizeof(Stu)); 23 tmpN->next = NULL; 24 tmp->next = tmpN; 25 tmp = tmpN; 26 } 27 28 for(int i = 0; i < size; i++){ 29 printf("%s\n", head->name); 30 head = head->next; 31 } 32 33 return 0; 34 }
第四个:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 struct _stu{ 6 char name[10]; 7 struct _stu *next; 8 }; 9 10 typedef struct _stu Stu; 11 12 void display(Stu *head){ 13 int flag = 1; 14 15 do{ 16 printf("%s\n", head->name); 17 if(NULL == head->next){ 18 flag = 0; 19 } 20 head = head->next; 21 }while(flag); 22 } 23 24 int main(int argc, char **argv) 25 { 26 Stu *head; 27 28 head = (Stu *)malloc(sizeof(Stu)); 29 strcpy(head->name, "lina"); 30 head->next = (Stu *)malloc(sizeof(Stu)); 31 strcpy(head->next->name, "mina"); 32 head->next->next = (Stu *)malloc(sizeof(Stu)); 33 strcpy(head->next->next->name, "bina"); 34 head->next->next->next = (Stu *)malloc(sizeof(Stu)); 35 strcpy(head->next->next->next->name, "tina"); 36 head->next->next->next->next = (Stu *)malloc(sizeof(Stu)); 37 strcpy(head->next->next->next->next->name, "dina"); 38 head->next->next->next->next->next = NULL; 39 40 display(head); 41 42 return 0; 43 }
第五个:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 typedef struct _stu{ 6 char name[10]; 7 struct _stu *next; 8 } Stu; 9 10 int main(int argc, char **argv) 11 { 12 Stu *head; 13 Stu *tmp; 14 head = tmp = (Stu *)malloc(sizeof(Stu)); 15 tmp->next = NULL; 16 17 char *stuName[] = {"lina", "mina", "bina", "tina", "dina"}; 18 int size = sizeof(stuName)/sizeof(stuName[0]); 19 20 for(int i = 0; i < size; i++){ 21 strcpy(tmp->name, stuName[i]); 22 Stu *tmpN = (Stu *)malloc(sizeof(Stu)); 23 tmpN->next = NULL; 24 tmp->next = tmpN; 25 tmp = tmpN; 26 } 27 28 printf("delete before: \n"); 29 Stu *head1 = head; 30 for(int i = 0; i < size; i++){ 31 printf("%s\n", head1->name); 32 head1 = head1->next; 33 } 34 35 printf("delete 'bina' after: \n"); 36 Stu *head2 = head; 37 tmp = head2; 38 while(tmp->next != NULL){ 39 if(!(strcmp(tmp->next->name, "bina"))){ 40 tmp->next = tmp->next->next; 41 } 42 tmp = tmp->next; 43 } 44 while(NULL != head2->next){ 45 printf("%s\n", head2->name); 46 head2 = head2->next; 47 } 48 49 return 0; 50 }
第六个:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 typedef struct _stu{ 6 char name[10]; 7 struct _stu *next; 8 } Stu; 9 10 int main(int argc, char **argv) 11 { 12 Stu *head; 13 Stu *tmp; 14 head = tmp = (Stu *)malloc(sizeof(Stu)); 15 tmp->next = NULL; 16 17 char *stuName[] = {"lina", "mina", "bina", "tina", "dina"}; 18 int size = sizeof(stuName)/sizeof(stuName[0]); 19 20 for(int i = 0; i < size; i++){ 21 strcpy(tmp->name, stuName[i]); 22 Stu *tmpN = (Stu *)malloc(sizeof(Stu)); 23 tmpN->next = NULL; 24 tmp->next = tmpN; 25 tmp = tmpN; 26 } 27 28 printf("insert before: \n"); 29 Stu *head1 = head; 30 for(int i = 0; i < size; i++){ 31 printf("%s\n", head1->name); 32 head1 = head1->next; 33 } 34 35 printf("insert 'pina' after 'binA': \N"); 36 Stu *head2 = head; 37 Stu *tmpP = head2; 38 39 Stu *new = (Stu *)malloc(sizeof(Stu)); 40 strcpy(new->name, "pina"); 41 new->next = NULL; 42 43 int flag = 1; 44 while(NULL != tmpP && flag){ 45 if(strcmp(tmpP->name, "bina")){ 46 tmpP = tmpP->next; 47 }else{ 48 new->next = tmpP->next; 49 tmpP->next = new; 50 flag = 0; 51 size++; 52 } 53 } 54 55 for(int i = 0; i < size; i++){ 56 printf("%s\n", head2->name); 57 head2 = head2->next; 58 } 59 60 return 0; 61 }
第七个:
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct _stu{ char name[10]; struct _stu *next; } Stu; int main(int argc, char **argv) { Stu *head; Stu *tmp; head = tmp = (Stu *)malloc(sizeof(Stu)); tmp->next = NULL; char *stuName[] = {"lina", "mina", "bina", "tina", "dina"}; int size = sizeof(stuName)/sizeof(stuName[0]); for(int i = 0; i < size; i++){ strcpy(tmp->name, stuName[i]); Stu *tmpN = (Stu *)malloc(sizeof(Stu)); tmpN->next = NULL; tmp->next = tmpN; tmp = tmpN; } printf("delete before: \n"); Stu *head1 = head; for(int i = 0; i < size; i++){ printf("%s\n", head1->name); head1 = head1->next; } printf("delete tail after: \n"); Stu *head2 = head; tmp = head2; while(tmp != NULL){ if(tmp->next->next == NULL){ tmp->next = NULL; } tmp = tmp->next; } while(NULL != head2->next){ printf("%s\n", head2->name); head2 = head2->next; } printf("delete head after: \n"); Stu *head3 = head; head3 = head3->next; while(NULL != head3->next){ printf("%s\n", head3->name); head3 = head3->next; } return 0; }
第八个:
#include <stdio.h> #include <string.h> typedef struct _memb{ char name[20]; struct _memb *forward; struct _memb *backward; } Memb; void showForward(Memb *start){ int flag = 1; do{ printf("%s\n", start->name); if(start->forward == NULL){ flag = 0; } start = start->forward; }while(flag); return; } void showBackward(Memb *end){ int flag = 1; do{ printf("%s\n", end->name); if(end->backward == NULL){ flag = 0; } end = end->backward; }while(flag); return; } int main(int argc, char **argv) { Memb memb1; Memb memb2; Memb memb3; Memb *start; Memb *end; Memb membNum[] = {memb1, memb2, memb3}; char *membName[] = {"lina", "mina", "bina"}; int size = sizeof(membName)/sizeof(membName[0]); for(int i = 0; i < size; i++){ strcpy(membNum[i].name, *(membName + i)); } start = &membNum[0]; start->forward = &membNum[1]; start->forward->forward = &membNum[2]; start->forward->forward->forward = NULL; end = &membNum[2]; end->backward = &membNum[1]; end->backward->backward = &membNum[0]; end->backward->backward->backward = NULL; printf("Names of members (forward traversing): \n"); showForward(start); printf("\nNames of members (backward traversing): \n"); showBackward(end); return 0; }
代码非常简单,就不多说了
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。