单链表基本操作有创建(从输入读取元素,输入字母则表示“输入结束”),查找,插入,删除,排序,销毁等。代码如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode* next;
}LinkNode;
void CreatList(LinkNode* &L)//头插法建立链表
{
int n;
L = (LinkNode*)malloc(sizeof(LinkNode));
L -> next = NULL;
L -> data = 0;
while(scanf("%d", &n) == 1)
{
LinkNode* s = (LinkNode* )malloc(sizeof(LinkNode));
s -> data = n;
s -> next = L -> next;
L -> next = s;
}
return ;
}
void CreatList1(LinkNode* &L)//尾插法建立链表
{
int n;
L = (LinkNode*)malloc(sizeof(LinkNode));
LinkNode* r = L;
L -> next = NULL;
L -> data = 0;
while(scanf("%d", &n) == 1)
{
LinkNode* s = (LinkNode* )malloc(sizeof(LinkNode));
s -> data = n;
r -> next = s;
r = s;
}
r -> next = NULL;//少了这句要老命地滚屏!
return;
}
int ListEmpty(LinkNode* &L)// 判断链表是否为空
{
if(L -> next == NULL)
return (0);
else
return 1;
}
bool GetElem(LinkNode* L, int i, int &e)//链表的按位查找;少了&无法传值
{
int j = 0;
LinkNode*p = L;
while(j < i && p != NULL)
{
j ++;
p = p -> next;
}
if(p == NULL)
return false;
else
e = p -> data;
return true;
}
int GetElem1(LinkNode* L, int e)//链表的按值查找
{
LinkNode* p = L -> next;
int i = 1;
while(p)
{
if(p -> data == e)
break;
else
{
p = p -> next;
i ++;
}
}
if(!p)
return 0;
else
return i;
}
int ListDelete(LinkNode* &L, int i)//链表的按位删除
{
LinkNode* p = L, *q = L -> next;
i --;
while(i && q)
{
p = p -> next;
q = q -> next;
i --;
}
if(!q)
return 0;
else
{
p -> next = q -> next;
return 1;
}
}
int ListDelete1(LinkNode* &L, int e)//链表的按值删除
{
LinkNode*p = L, *q = L -> next;
while((q -> data != e) && q)
{
p = p -> next;
q = q -> next;
}
if(!q)
return 0;
else
{
p -> next = q -> next;
return 1;
}
}
void reverList(LinkNode* &L, int i, int j) //将链表的第i位与第j位换位置。
{
LinkNode*m, *n, *p, *q;
m = p = L;
n = q = L -> next;
i --;
j --;
while(i)
{
p = p -> next;
q = q -> next;
i --;
}
while(j)
{
m = m -> next;
n = n -> next;
j --;
}
if(q == m)
{
LinkNode* k = q -> next;
p -> next = n;
q -> next = n -> next;
n -> next = q;
}
else
{
LinkNode* k = q -> next;
m -> next = q;
q -> next = n -> next;
p -> next = n;
n -> next = k;
}
}
void sortList(LinkNode* &L)//链表的升序排列(码完才发现可以直接交换data值,不必换指针)
{
int i = 1, j = 2;
LinkNode* temp = NULL;
LinkNode*p = L -> next;
LinkNode*q = p -> next;
while(p)
{
while(q)
{
if(p -> data > q -> data)
{
reverList(L, i, j);
temp = p;
p = q;
q = temp;
}
q = q -> next;
j ++;
}
i ++;
j = i + 1;
p = p -> next ;
if(p)
q = p -> next;//少了这两行,升序无法完成。debug了好久才找到这个问题:原来是此时p已经是NULL,无法获得next值,程序错误。
}
return ;
}
void DispLink(LinkNode* &L)//链表的输出
{
LinkNode* p = L -> next;
while(p != NULL)
{
printf("%d ", p -> data);
p = p -> next;
}
printf("\n");
return ;
}
void DestoryLink(LinkNode* &L)//链表的销毁
{
LinkNode*pre = L, *p = L -> next;
while(p != NULL)
{
free(pre);
pre = p;
p = p -> next;
}
free(pre);
}
int main(void)//主函数是这样滴。
{
LinkNode* L;
CreatList1(L);
DispLink(L);
int x;
int value;
printf("请输入您要查找的第几个数?\n");
while(getchar() != '\n');
scanf("%d", &x);
GetElem(L, x, value);
printf("该数为%d", value);
printf("请输入您要查找的数值?\n");
scanf("%d", &x);
printf("该数为第%d个", GetElem1(L, x));
printf("您想删除第几个数?\n");
scanf("%d", &x);
ListDelete(L, x);
DispLink(L);
printf("您想删除哪个数值?\n");
scanf("%d", &x);
ListDelete1(L, x);
DispLink(L);
sortList(L);
DispLink(L);
DestoryLink(L);
return 0;
}