C语言链表
list.h
#ifndef LIST_H
#define LIST_H
#define OK 1
#define OVERFLOW 0
#define TRUE 1
#define FALSE 2
#define ERROR 0
#define ILLEGAL -1
typedef struct ListNode
{
int data;
struct ListNode *next;
}ListNode;
typedef struct LinkList
{
ListNode *phead;
ListNode *pcur;
int num;
struct LinkList *next;
}Node,*LinkList;
int InitList(LinkList *L);
int ClearList(LinkList L);
int CreateListHead(LinkList L,void (*Input)(int *));
int CreateListTail(LinkList L,void (*Input)(int *));
int DisplayList(LinkList L,void (*Visit)(int *));
int LengthofList(LinkList L,int *length);
int EmptyList(LinkList L);
int Equal(int *value1,int *value2);
int FindListValue(LinkList L, int *value, int *outposition,int(*Equal)(int *,int *));
int FindListOrder(LinkList L, const int position, int * pevalue);
int UpdateList(LinkList L, int position, void (*Input)(int *));
int InsertList(LinkList L, int position, void (*Input)(int *));
int DeleteList(LinkList L, const int position, int *x);
int SortList(LinkList L);
#endif
list.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
int InitList(LinkList *L)
{
*L=(LinkList)malloc(sizeof(Node));
if(NULL == (*L))
{
return OVERFLOW;
}
else
{
(*L)->next = NULL;
(*L)->num = 0;
(*L)->phead = NULL;
(*L)->pcur = NULL;
return OK;
}
}
int CreateListHead(LinkList L,void (*Input)(int *))
{
int i;
int value;
int Number;
printf("请输入单链表的个数:");
scanf("%d",&Number);
for(i = 0; i<Number; i++)
{
ListNode *p = NULL;
p = (ListNode*)malloc(sizeof(ListNode));
printf("请输入第%d节点的值!",i+1);
Input(&value);
if(NULL == p)
{
return OVERFLOW;
}
p->data = value;
p->next = NULL;
p->next = L->phead;
L->phead = p;
L->num++;
}
return OK;
}
int CreateListTail(LinkList L,void (*Input)(int *))
{
int Number;
int i;
int value;
printf("请输入单链表的个数:");
scanf("%d",&Number);
L->phead = L->pcur;
L->num = 0;
for(i=0; i<Number; i++)
{
ListNode *p = NULL;
p = (ListNode*)malloc(sizeof(ListNode));
printf("请输入第%d节点的值!",i+1);
Input(&value);
if(NULL == p)
{
return OVERFLOW;
}
p->data = value;
p->next = NULL;
if(0 == i)
{
L->phead = p;
}
else
{
L->pcur->next = p;
}
L->pcur = p;
L->num++;
}
return OK ;
}
int FindListOrder(LinkList L, const int position,int *pvalue)
{
int counter = 0;
if(position <1 || position > L->num)
{
return ILLEGAL;
}
L->pcur = L->phead;
while(NULL != L->pcur)
{
counter++;
if(counter == position)
{
*pvalue = L->pcur->data;
return OK;
}
L->pcur = L->pcur->next;
}
return OK;
}
int FindListValue(LinkList L, int *value, int *outposition,int(*Equal)(int *,int *))
{
int count = 1;
L->pcur = L->phead;
while(NULL != L->pcur)
{
if(TRUE == Equal(&L->pcur->data,value))
{
*outposition = count;
return OK;
}
count++;
L->pcur = L->pcur->next;
}
return ERROR;
}
int InsertList(LinkList L, int position, void (*Input)(int *))
{
int counter = 0;
int value;
ListNode *p = NULL;
p = (ListNode*)malloc(sizeof(ListNode));
if(position <0 || position > L->num)
{
return ILLEGAL;
}
L->pcur = L->phead;
while(NULL != L->pcur)
{
counter++;
if(0 == position)
{
Input(&value);
p->data = value;
p->next = L->phead;
L->phead = p;
L->num++;
return OK;
}
if(counter == position)
{
Input(&value);
p->data = value;
p->next = L->pcur->next;
L->pcur->next = p;
L->num++;
return OK;
}
L->pcur = L->pcur->next;
}
return OK;
}
int DeleteList(LinkList L, const int position, int *x)
{
int i;
ListNode *ptTemp;
L->pcur = L->phead;
ptTemp = L->phead;
if(position <1 || position > L->num)
{
return ILLEGAL;
}
if(1 == position)
{
*x = L->phead->data;
L->phead = L->phead->next;
L->num--;
free(ptTemp);
}
else
{
for(i=1; i< position-1; i++)
{
L->pcur = L->pcur->next;
ptTemp = L->pcur->next;
}
*x = L->pcur->next->data;
L->pcur->next = L->pcur->next->next;
L->num--;
free(ptTemp);
}
return OK;
}
int UpdateList(LinkList L, int position, void (*Input)(int *))
{
int counter = 0;
int value;
if(position <1 || position > L->num)
{
return ILLEGAL;
}
Input(&value);
L->pcur = L->phead;
while(NULL != L->pcur)
{
counter++;
if(counter == position)
{
L->pcur->data = value;
return OK;
}
L->pcur = L->pcur->next;
}
return OK;
}
int SortList(LinkList L)
{
ListNode *first;
ListNode *t,*p,*q;
first = L->phead->next;
L->phead->next = NULL;
while (first != NULL)
{
for (t=first, q=L->phead; ((q!=NULL) && (q->data < t->data)); p=q, q=q->next);
first = first->next;
if (q == L->phead)
{
L->phead = t;
}
else
{
p->next = t;
}
t->next = q;
}
return OK;
}
int Equal(int *value1,int *value2)
{
if(*value1 == *value2)
{
return TRUE;
}
else
{
return FALSE;
}
}
int LengthofList(LinkList L,int *length)
{
*length = L->num;
return OK;
}
int ClearList(LinkList L)
{
ListNode *ptTemp;
L->pcur = L->phead;
while( NULL != L->pcur)
{
ptTemp = L->pcur;
L->pcur = L->pcur->next;
free(ptTemp);
}
L->phead = NULL;
return OK;
}
int DisplayList(LinkList L,void (*Visit)(int *))
{
L->pcur = L->phead;
if(NULL == L->pcur)
{
return ERROR;
}
while(NULL != L->pcur)
{
Visit(&L->pcur->data);
L->pcur = L->pcur->next;
}
return OK;
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void Menue(void);
void OutputElem(int *pelnvalue)
{
printf(" %d ",*pelnvalue);
}
void InputElem(int *pelnvalue)
{
printf("请输入结点的值!");
scanf("%d",pelnvalue);
}
int main()
{
LinkList L = NULL;
int choice;
int State;
while(1)
{
Menue();
printf("\n请选择功能编号!");
scanf("%d",&choice);
switch(choice)
{
case 0:
{
system("clear");
exit(0);
}
case 1:
{
if(OVERFLOW ==InitList(&L))
{
printf("空间溢出,初始化单链表失败!");
}
if(OK == InitList(&L))
{
system("clear");
printf("初始化单链表成功!\n");
}
break;
}
case 2:
{
State = CreateListHead(L,InputElem);
if(OVERFLOW == State)
{
printf("\n单链表创建失败!\n");
}
if(OK == State)
{
system("clear");
printf("\n单链表创建成功!");
}
break;
}
case 3:
{
State = CreateListTail(L,InputElem);
if(OVERFLOW == State)
{
printf("\n单链表创建失败!\n");
}
if(OK == State)
{
system("clear");
printf("\n单链表创建成功!");
}
break;
}
case 4:
{
system("clear");
State = DisplayList(L,OutputElem);
if(State == OK)
{
printf("\n");
}
if(State == ERROR)
{
printf("\n链表为空,没有元素!");
}
break;
}
case 5:
{
ListNode *p = NULL;
int posit;
int temp;
system("clear");
printf("\n输入你要查找的位置:");
scanf("%d",&posit);
if(OK == FindListOrder(L,posit,&temp))
{
printf("单链表第%d个位置的值为:%d\n",posit,temp);
}
else
{
printf("你查找的位置不合法!\n");
}
break;
}
case 6:
{
int posit;
int temp;
system("clear");
printf("\n请输入你要查找的数!");
InputElem(&temp);
if(OK == FindListValue(L,&temp,&posit,Equal))
{
printf("单链表第%d个位置的值为:%d\n",posit,temp);
}
else
{
printf("该值不在链表中,没找到!\n");
}
break;
}
case 7:
{
int len;
system("clear");
LengthofList(L,&len);
printf("\n单链表的长度为: %d",len);
break;
}
case 8:
{
int posit;
system("clear");
printf("请输入你所要插入的位置!\n");
scanf("%d",&posit);
State = InsertList(L,posit,InputElem);
if(State == OK)
{
printf("\n插入数据成功!");
}
if(State == ILLEGAL)
{
printf("\n插入的位置不合法!");
}
break;
}
case 9:
{
int posit;
int outvalue;
system("clear");
printf("请输入你所要删除的位置!\n");
scanf("%d",&posit);
State = DeleteList(L, posit, &outvalue);
if(State == OK)
{
printf("删除成功!\n");
printf("你删除的数据为:%d\n",outvalue);
}
if(State == ILLEGAL)
{
printf("删除的位置不合法!\n");
}
break;
}
case 10:
{
int posit;
printf("请输入你要更新的位置!");
scanf("%d",&posit);
State = UpdateList(L,posit, InputElem);
if(ILLEGAL == State)
{
printf("输入的位置不合法!\n");
}
if(OK == State)
{
printf("更新数据成功!\n");
}
break;
}
case 11:
{
if(OK == SortList(L))
{
system("clear");
printf("链表已经从小到大排好序!\n");
break;
}
}
case 12:
{
system("clear");
if(OK == ClearList(L))
{
printf("链表已被清空!\n");
}
break;
}
default :
printf("输入编号不合法!");
break;
}
}
return 0;
}
void Menue(void)
{
printf("\n ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁");
printf("\n ╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱╱" );
printf("\n┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓");
printf("\n┃ 单链表子系统 ┃");
printf("\n ┃━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┃ ");
printf("\n ┃ 0 退 出 ┃ ");
printf("\n ┃ 1 初 始 化 ┃ ");
printf("\n ┃ 2 头 插 法 ┃ ");
printf("\n ┃ 3 尾 插 法 ┃ ");
printf("\n ┃ 4 显 示 ┃ ");
printf("\n ┃ 5 按位 查找 ┃ ");
printf("\n ┃ 6 按值 查找 ┃ ");
printf("\n ┃ 7 求链 表长 ┃ ");
printf("\n ┃ 8 插入 结点 ┃ ");
printf("\n ┃ 9 删除 结点 ┃ ");
printf("\n ┃ 10 更新 结点 ┃ ");
printf("\n ┃ 11 链表 排序 ┃ ");
printf("\n ┃ 12 清空 链表 ┃ ");
printf("\n┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
printf("\n┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛");
}