头文件
#ifndef _LINKLIST1_H #define _LINKLIST1_H #define FAILURE 10000 #define SUCCESS 10001 #define TRUE 10002 #define FALSE 10003 typedef int Elemtype; struct node { Elemtype data; //数据域 struct node * next;//指针域 }; typedef struct node Node; //函数申明 int Linklist_init(Node **l); //初始化一个链表的头结点 int Linklist_insert(Node *l, int n, Elemtype e);//在p位置插入rand产生的随机数(重点,背下来) int Linklist_traverse(Node *l, void (*p)(Elemtype));//遍历输出 int Linklist_length(Node *l);//求链表长度 int Linklist_empty(Node *l);//检测链表是否为空 int Linklist_locate(Node *l, Elemtype e, int (*q)(Elemtype, Elemtype));//在链表中查找e,若成功返回序号 int Linklist_delete(Node *l, int n, Elemtype *e);//删除链表中第n个位置的元素,并用e返回值 int Linklist_clear(Node *l);//清空链表元素 int Linklist_destory(Node **l);//销毁链表 int Linklist_reverse(Node *l);//反转链表!(背下来) #endif
自定义函数代码
#include "linklist1.h" #include <stdio.h> #include <stdlib.h> //初始化! //要改变first的值,在传值时应传first的地址来达到改变l的值就改变了first的值 int Linklist_init(Node **l) { *l = (Node *) malloc (sizeof(Node) * 1); //入参判断,若*l为空,返回FAILURE if(NULL == *l) { return FAILURE; } (*l)->next = NULL; //初始化:头结点的指针域为空 return SUCCESS; } //插入操作(!重点!) int Linklist_insert(Node *l, int n, Elemtype e) { Node *p = l; int k = 1; // k 是移动次数 if(NULL == l) { return FAILURE; } while(n > k && p != NULL) { p = p->next; k++; } if(k > n || NULL == p) { return FAILURE; } Node *q = (Node *) malloc (sizeof(Node) * 1); if(NULL == q) { return FAILURE; } q->data = e; q->next = p->next; p->next = q; return SUCCESS; } //遍历输出 int Linklist_traverse(Node *l, void (*p)(Elemtype)) { if(NULL == l) { return FAILURE; } Node *q = l; while(q->next) { q = q->next; p(q->data); } return SUCCESS; } //求链表长度 int Linklist_length(Node *l) { if(NULL == l) { return FAILURE; } int len = 0; Node *p = l->next; while(p) { len++; p = p->next; } return len; } //检测链表是否为空 int Linklist_empty(Node *l) { return (l->next == NULL) ? TRUE : FALSE; } //将链表中第n个元素返回给e int Linklist_getelem(Node *l, int n,Elemtype *e) { if(NULL == l) { return FAILURE; } Node *q = l; int i; for(i = 0; i < n && q != NULL; i++) //循环p次,同时满足q不为空 { q = q->next; } if(!q) { return FAILURE; } *e = q->data; return SUCCESS; } //在链表中查找e,若成功返回序号 int Linklist_locate(Node *l, Elemtype e, int (*q)(Elemtype, Elemtype)) { if(NULL == l) { return FAILURE; } Node *p = l->next; int len = 1; while(p) { if(q(e, p->data) == TRUE) { return len; } p = p->next; len++; } return FAILURE; } //删除链表中第n个位置的元素,并用e返回值 int Linklist_delete(Node *l, int n, Elemtype *e) { if(NULL == l) { return FAILURE; } Node *p = l; int k = 1; while(n > k && p != NULL) { p = p->next; k++; } if(n < k || p == NULL) { return FAILURE; } Node *a = p->next; *e = a->data; p->next = a->next; free(a); return SUCCESS; } //清空链表元素 int Linklist_clear(Node *l) { if(NULL == l) { return FAILURE; } Node *p = l->next; while(p) { l->next = p->next; free(p); p = l->next; } return SUCCESS; } //销毁链表 int Linklist_destory(Node **l) { if(NULL == l) { return FAILURE; } free(*l); (*l)= NULL; return SUCCESS; } //!!!!!重点!!!!!! //反转链表! int Linklist_reverse(Node *l) { if(NULL == l) { return FAILURE; } Node *p = l->next; l->next = NULL; while(p != NULL) { Node *q = p; p = p->next; q->next = l->next; l->next = q; } return SUCCESS; }
main函数代码
#include "linklist1.h" #include <stdio.h> #include <time.h> void print(Elemtype e) { printf("%d ",e); } int equal(Elemtype e1, Elemtype e2) { return (e1 == e2) ? TRUE : FALSE; } int main() { int ret; Node *first = NULL; //定义一个头指针 srand(time(NULL)); //产生一个随机数 printf("/**********初始化一个链表!*************/\n"); ret = Linklist_init(&first); if(FAILURE == ret) { printf("Init the linklist failure!\n"); } else { printf("Init the linklist success!\n"); } printf("/***************************************/\n"); printf("/**********在链表中用rand在指定位置i+1随机插入数!*************/\n"); int i; for(i = 0; i < 10; i++) { ret = Linklist_insert(first, i + 1, rand() % 20); if(FAILURE == ret) { printf("Insert Failure!\n"); } else { printf("Insert Success!\n"); } } printf("/***************************************/\n"); printf("/**********遍历输出一个链表!*************/\n"); ret = Linklist_traverse(first, print); if(FAILURE == ret) { printf("\nTraverse Failure!\n"); } else { printf("\nTraverse Success!\n"); } printf("/***************************************/\n"); printf("/**********反转链表元素*************/\n"); ret = Linklist_reverse(first); if(FAILURE == ret) { printf("Reverse the linklist Failure!\n"); } else { printf("Reverse the linklist Success!\n"); } //遍历下反转后的链表 ret = Linklist_traverse(first,print); //遍历 查看反转后的链表中的节点元素 if(FAILURE == ret) { printf("\nTraverse Failure!\n"); } else { printf("\nTraverse Success!\n"); } printf("/***************************************/\n"); printf("/**********求一个链表的长度!*************/\n"); ret = Linklist_length(first); if(FAILURE == ret) { printf("Get Length Failure!\n"); } else { printf("Length: %d\n",ret); } printf("/***************************************/\n"); printf("/**********检测链表是否为空!*************/\n"); ret = Linklist_empty(first); if(TRUE == ret) { printf("Linklist is empty!\n"); } else if(FALSE == ret) { printf("Linklist is not empty\n"); } printf("/***************************************/\n"); printf("/**********将链表中第n个元素返回给e!*************/\n"); int n = 5; Elemtype e; ret = Linklist_getelem(first, n, &e); if(FAILURE == ret) { printf("Get element Failure!\n"); } else { printf("Get Success,The %dth element is %d", n, e); } printf("/***************************************/\n"); printf("/***********在链表中查找e,若成功返回序号************/\n"); e = 5; ret = Linklist_locate(first, e, equal); if(FAILURE == ret) { printf("Locate the element %d Failure!\n",e); } else { printf("The element %d is the %dth number!\n", e, ret); } printf("/***************************************/\n"); printf("/***********删除链表中第n个位置的元素,并用e返回值************/\n"); n = 8; ret = Linklist_delete(first, n, &e); if(FAILURE == ret) { printf("Delete the %dth element Failure!\n",n); } else { printf("Delete the %dth's element %d Success!\n", n, e); } ret = Linklist_traverse(first,print); //遍历 查看删除后的链表中的节点元素 if(FAILURE == ret) { printf("\nTraverse Failure!\n"); } else { printf(" Traverse Success!\n"); } printf("/***************************************/\n"); printf("/**********清空链表元素*************/\n"); ret = Linklist_clear(first); if(FAILURE == ret) { printf("Clear the linklist Failure!\n"); } else { printf("Clear the linklist Success!\n"); } ret = Linklist_traverse(first,print); //遍历 查看清空后的链表中的节点元素 if(FAILURE == ret) { printf("\nTraverse Failure!\n"); } else { printf("\nTraverse Success!\n"); } printf("/***************************************/\n"); printf("/**********销毁链表元素*************/\n"); ret = Linklist_destory(&first); if(FAILURE == ret) { printf("Destory Failure!\n"); } else { printf("Destory Success!\n"); } //销毁链表后,无法向链表中插入元素 for(i = 0; i < 3; i++) { ret = Linklist_insert(first, i + 1, rand() % 20); if(FAILURE == ret) { printf("Insert Failure!\n"); } else { printf("Insert Success!\n"); } } ret = Linklist_traverse(first,print); if(FAILURE == ret) { printf("\nTraverse Failure!\n"); } else { printf("\nTraverse Success!\n"); } printf("/***************************************/\n"); return 0; }