单链表

2018-06-17 21:27:25来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

测试内容:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include "LinkList.h"
 6 
 7 typedef struct PERSON{
 8     LinkNode nod;
 9     char name[64];
10     int age;
11 }Person;
12 
13 void MyPrint(LinkNode* data){
14     Person*  p = (Person*)data;
15     printf("Name:%s Age:%d\n", p->name, p->age);
16 }
17 
18 int MyCompare(LinkNode* node1, LinkNode* node2){
19     Person* p1 = (Person*)node1;
20     Person* p2 = (Person*)node2;
21 
22     if (strcmp(p1->name, p2->name) == 0 && p1->age == p2->age){
23         return 0;
24     }
25     return -1;
26 }
27 
28 
29 
30 
31 
32 
33 int main(void){
34 
35     LinkList* list = Init_List();
36     Person p1, p2, p3, p4, p5;
37     strcpy(p1.name, "张三");
38     strcpy(p2.name, "李四");
39     strcpy(p3.name, "王五");
40     strcpy(p4.name, "赵六");
41     strcpy(p5.name, "田七");
42 
43     p1.age = 10;
44     p2.age = 20;
45     p3.age = 30;
46     p4.age = 40;
47     p5.age = 50;
48 
49     Insert_List(list, 0, (LinkNode*)&p1);
50     Insert_List(list, 0, (LinkNode*)&p2);
51     Insert_List(list, 0, (LinkNode*)&p3);
52     Insert_List(list, 0, (LinkNode*)&p4);
53     Insert_List(list, 0, (LinkNode*)&p5);
54 
55     Print_LinkList(list, MyPrint);
56     Remove_List(list, 2);
57     printf("---------------\n");
58     Print_LinkList(list, MyPrint);
59     Person findP;
60     strcpy(findP.name, "张三");
61     findP.age = 10;
62     int pos = Find_LinkList(list, (LinkNode*)&findP, MyCompare);
63     printf("位置index:%d\n", pos);
64     Free_LinkList(list);
65     system("pause");
66     return 0;
67 }
View Code

 

头文件:

 1 #ifndef  LINKLIST_H
 2 #define LINKLIST_H
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 #define TRUE 1
 7 #define FALSE 0
 8 #define    SPECIAL -1
 9 
10 
11 typedef struct LINKNODE
12 {
13     struct LINKNODE* next;
14 }LinkNode;
15 
16 typedef struct LINKLIST
17 {
18     LinkNode head;
19     int size;
20 }LinkList;
21 
22 
23 
24 typedef void(*PRINTNODE)(LinkNode*);
25 typedef int(*COMPARENODE)(LinkNode*, LinkNode*);
26 
27 LinkList* Init_List();
28 
29 void  Insert_List(LinkList* list, int pos, LinkNode* node);
30 void Remove_List(LinkList* list, int pos);
31 //委托 
32 int Find_LinkList(LinkList* list, LinkNode* node, COMPARENODE compare);
33 int Size_LinkList(LinkList* list);
34 void Print_LinkList(LinkList* list, PRINTNODE print);
35 void Free_LinkList(LinkList* list);
36 
37 
38 
39 
40 #endif

 

实现:

  1 #include "LinkList.h"
  2 
  3 
  4 
  5 
  6 LinkList* Init_List()
  7 {
  8     LinkList* list = (LinkList*)malloc(sizeof(LinkList));
  9     if (list == NULL)
 10     {
 11         printf("Init_List-->malloc error\n");
 12         return list;
 13     }
 14     list->head.next = NULL;
 15     list->size = 0;
 16     return list;
 17 
 18 }
 19 
 20 void  Insert_List(LinkList* list, int pos, LinkNode* node)
 21 {
 22     if (node == NULL || list == NULL)
 23     {
 24         printf(" node\t // list error\n");
 25         return;
 26     }
 27     if (pos < 0 || pos > list->size)
 28     {
 29         printf(" out of range    \n");
 30         return;
 31     }
 32     LinkNode* pCurrent = &(list->head);
 33     for (int i = 0; i < pos && pCurrent->next != NULL; i++)
 34     {
 35         pCurrent = pCurrent->next;
 36     }
 37 
 38     node->next = pCurrent->next;
 39     pCurrent->next = node;
 40     list->size++;
 41 }
 42 
 43 void Remove_List(LinkList* list, int pos)
 44 {
 45     if (list == NULL)
 46     {
 47         printf(" list error\n");
 48         return;
 49     }
 50     if (pos < 0 || pos > list->size)
 51     {
 52         printf(" out of range    \n");
 53         return;
 54     }
 55     LinkNode* pCurrent = &(list->head);
 56     for (int i = 0; i <pos&& pCurrent->next != NULL; i++)
 57     {
 58         pCurrent = pCurrent->next;
 59     }
 60     LinkNode* tmp = pCurrent->next;
 61     pCurrent->next = tmp->next;
 62     tmp->next = NULL;
 63     list->size--;
 64 
 65 
 66 }
 67 int Find_LinkList(LinkList* list, LinkNode* node, COMPARENODE compare)
 68 {
 69     if (list == NULL){
 70         printf("list is null\n");
 71         return -1;
 72     }
 73 
 74     if (node == NULL){
 75         printf("node is null\n");
 76         return -1;
 77     }
 78     LinkNode* pCurrent = list->head.next;
 79     int index = 0;
 80     int flag = -1;
 81     while (pCurrent != NULL){
 82         if (compare(pCurrent, node) == 0){
 83             flag = index;
 84             break;
 85         }
 86         pCurrent = pCurrent->next;
 87         index++;
 88     }
 89 
 90     return flag;
 91 }
 92 int Size_LinkList(LinkList* list)
 93 {
 94     if (list == NULL)
 95     {
 96         printf(" list error\n");
 97         return -1;
 98     }
 99     return list->size;
100 }
101 void Print_LinkList(LinkList* list, PRINTNODE print)
102 {
103     if (list == NULL){
104         return;
105     }
106 
107     //辅助指针
108     LinkNode* pCurrent = list->head.next;
109     while (pCurrent != NULL){
110         print(pCurrent);
111         pCurrent = pCurrent->next;
112     }    
113 }
114 void Free_LinkList(LinkList* list)
115 {
116     if (list == NULL){
117         return;
118     }
119     free(list);
120 }
View Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:1507: [NOI2003]Editor(块状链表)

下一篇:实现二叉树的先序遍历、中序遍历、后序遍历