通过链表实现插入数与排序,删除,销毁等功能。
头文件代码段如下:
#ifndef _LINKLIST_H
#define _LINKLIST_H
#define Success 10000
#define Failure 10001
typedef int Element;
struct node
{
Element s;
struct node *next;
};
typedef struct node SN;
int LinkInit(SN **head);
int LinkInsert(SN *head, int n, Element e);
int LinkCheck(SN *head, void (*p)(Element));
int LinkLength(SN *head);
int GetElement(SN *head, int p, Element *e);
int ElementDelete(SN *head, int n, Element *e);
int LinkClear(SN *head);
int LinkDestroy(SN **head);
int LinkReverse(SN *head);
#endif
功能函数代码段如下:
#include <stdlib.h>
#include "LinkList.h"
int LinkInit(SN **head)
{
*head = (SN *)malloc(sizeof(SN) * 1);
if(NULL == *head)
{
return Failure;
}
(*head)->next = NULL;
return Success;
}
int LinkInsert(SN *head, int n, Element e)
{
SN *p = head;
SN *q;
int k = 1;
if(NULL == head)
{
return Failure;
}
while(k < n && p != NULL)
{
p = p->next;
k++;
}
if(k > n || p == NULL)
{
return Failure;
}
q = (SN *)malloc(sizeof(SN) * 1);
q->s = e;
q->next = p->next;
p->next = q;
return Success;
}
int LinkCheck(SN *head, void (*p)(Element))
{
SN *q = head;
if(head == NULL)
{
return Failure;
}
while(q->next)
{
q = q->next;
p(q->s);
}
return Success;
}
int LinkLength(SN *head)
{
if(head == NULL)
{
return Failure;
}
int len = 0;
SN *p = head->next;
while(p)
{
len++;
p = p->next;
}
return len;
}
int GetElement(SN *head, int p, Element *e)
{
if(head == NULL)
{
return Failure;
}
int i;
SN *q = head;
for(i = 0; i < p && q != NULL; i++)
{
q = q->next;
}
if(!q)
{
return Failure;
}
*e = q->s;
return Success;
}
int Location(SN *head, Element e, int (*p)(Element, Element))
{
if(head == NULL)
{
return Failure;
}
SN *q = head->next;
int len = 1;
if(head->next == NULL)
{
return Failure;
}
while(q)
{
len++;
if(p(e, q->s))
{
return len;
}
q = q->next;
}
return Failure;
}
int ElementDelete(SN *head, int n, Element *e)
{
SN *p = head;
int k = 1;
if(NULL == head)
{
return Failure;
}
while(k < n && p != NULL)
{
p = p->next;
k++;
}
if(k > n || p == NULL)
{
return Failure;
}
SN *q = p->next;
*e = q->s;
p->next = q->next;
free(q);
return Success;
}
int LinkClear(SN *head)
{
if(head == NULL)
{
return Failure;
}
SN *p = head->next;
while(p)
{
head->next = p->next;
free(p);
p = head->next;
}
return Success;
}
int LinkDestroy(SN **head)
{
if(head == NULL)
{
return Failure;
}
free(*head);
(*head) = NULL;
return Success;
}
int LinkReverse(SN *head)
{
if(head == NULL)
{
return Failure;
}
SN *p = head->next;
head->next = NULL;
while(p != NULL)
{
SN *q = p;
p = p->next;
q->next = head->next;
head->next = q;
}
return Success;
}
主函数代码段如下:
#include "LinkList.h"
#include <stdio.h>
int equal(Element s1, Element s2)
{
return (s1 == s2) ? 1 : 0;
}
void print(Element s)
{
printf("%d ", s);
}
int main()
{
int ret, i;
SN *head = NULL;
srand(time(NULL));
ret = LinkInit(&head);
if(ret == Success)
{
printf("Init Success!\n");
}
else
{
printf("Init Failure!\n");
}
for(i = 0; i < 10; i++)
{
ret = LinkInsert(head, i+1, rand() % 20);
if(ret == Failure)
{
printf("Insert Failure!\n");
}
else
{
printf("Insert Success!\n");
}
}
ret = LinkCheck(head, print);
if(ret == Success)
{
printf("\nCheck Success!\n");
}
else
{
printf("\nCheck failure!\n");
}
ret = LinkReverse(head);
if(ret == Success)
{
printf("Reverse Success!\n");
}
else
{
printf("Reverse Failure!\n");
}
ret = LinkCheck(head, print);
if(ret == Success)
{
printf("\nCheck Success!\n");
}
else
{
printf("\nCheck failure!\n");
}
ret = LinkLength(head);
if(ret == Failure)
{
printf("Get Length Failure!\n");
}
else
{
printf("Length = %d\n", ret);
}
Element e;
int p = 3;
ret = GetElement(head, p, &e);
if(ret == Success)
{
printf("%dth is Element %d\n", p, e);
}
else
{
printf("Get element failure!\n");
}
e = 3;
ret = Location(head, e, equal);
if(ret == Failure)
{
printf("%d is not exist!\n", e);
}
else
{
printf("%d is %dth!\n", e, ret);
}
p = 4;
ret = ElementDelete(head, p, &e);
if(ret == Failure)
{
printf("Delete Failure!\n");
}
else
{
printf("%d is deleted success!\n", e);
}
ret = LinkCheck(head, print);
if(ret == Success)
{
printf("\nCheck Success!\n");
}
else
{
printf("\nCheck failure!\n");
}
ret = LinkClear(head);
if(ret == Success)
{
printf("Clear Success!\n");
}
else
{
printf("Clear Failure!\n");
}
ret = LinkDestroy(&head);
if(ret == Success)
{
printf("Destroy Success!\n");
}
else
{
printf("Destroy Failure!\n");
}
return 0;
}