学习参考: 严蔚敏: 《数据结构-C语言版》
基本操作:
代码实现
链式表结点定义:
typedef struct node
{
int data;
struct node* next;
}Node,* pNode;
链式表定义:
typedef struct
{
int len;
pNode node;
}LinkList,* pList;
单链表初始化:
int initList(pList list)
{
if(!list)
return -1;
list->len = 0;
list->node = NULL;
return 1;
}
添加结点(头插法)的实现:
int addDataHead(pList list, int data)
{
pNode pre =NULL, p = NULL;
if(!list)
return 0;
pre = list->node;
p = (pNode) malloc(sizeof(int));
p->data = data;
p->next = pre;
list->node = p;
list->len ++;
return 1;
}
添加结点(尾插法)的实现:
int addDataTail(pList list, int data)
{
pNode pre = NULL, p = NULL;
if(!list)
return 0;
p = list->node;
while(p)
{
pre = p;
p=p->next;
}
p = (pNode)malloc(sizeof(Node));
if(!p)
return 0;
p->next = NULL;
p->data = data;
if(!pre)
list->node = p;
else
pre->next = p;
list->len ++;
return 1;
}
单链表的输出的实现:
int display(pList list)
{
pNode p = NULL;
if(!list->node)
return 0;
p = list->node;
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 1;
}
单链表的修改的实现:
int updata(pList list, int pos, int data)
{
pNode p = NULL;
int count = 0;
if(!list || !list->node || pos<1)
return 0;
p = list->node;
while (p)
{
count++;
if(count >= pos)
break;
p = p->next;
}
if(count >= pos)
p->data = data;
return 1;
}
单链表的插入实现:
int insertData(pList list, int pos, int data)
{
pNode q = NULL, p = NULL, pre=NULL;
int count = 1;
if(!list || pos<1 || pos>list->len+1)
return 0;
p = list->node;
while (countnext;
}
if(!pre)
{
addDataHead(list, data);
return 1;
}
q = (pNode) malloc(sizeof(Node));
q->data = data;
pre->next = q;
q->next = p;
list->len ++;
return 1;
}
单链表的删除代码实现:
int deleteData(pList list, int pos, int* e)
{
pNode pre = NULL, p = NULL;
int count = 0;
if(!list || pos<1 || pos>list->len || !list->node)
return 0;
p = list->node;
while(countnext;
}
if( !pre)
list->node = p->next;
else
pre->next = p->next;
*e = p->data;
free(p);
p = NULL;
list->len--;
return 1;
}
单链表按序号查找的代码实现:
int getData(pList list, int pos, int* data)
{
pNode p = NULL;
int count = 1;
if(!list || pos<1 || pos >list->len || !list->node)
{
*data = -1;
return 0;
}
p = list->node;
while( countnext;
}
*data = p->data;
return 1;
}
单链表按值查找的实现;
int getLocate(pList list, int data, int* index)
{
pNode p = NULL;
int count = 0;
if(!list || !list->node)
{
*data = -1;
return 0;
}
p = list->node;
while(p)
{
count++;
if(p->data == data)
break;
p = p->next;
}
if(!p)
{
*index = -1;
return 0;
}
*index = count;
return 1;
}
单链表销毁的代码实现:
int destroy(pList list)
{
pNode pre = NULL, p = NULL;
if(!list || !list->node)
return 0;
p = list->node;
while(p)
{
pre = p;
p = pre->next;
free(pre);
pre = NULL;
}
list->len =0;
list->node =NULL;
return 1;
}
单链表的长度
int getLenght(pList list, int* len)
{
if(!list)
return 0;
*len = list->len;
return 1;
}
测试代码
#include
#include "LinkList.h"
int main()
{
int val = -1;
LinkList list;
initList(&list);
addDataHead(&list, 1);
addDataHead(&list, 2);
addDataHead(&list, 3);
display(&list);
addDataTail(&list ,4);
display(&list);
updata(&list, 2, 6);
display(&list);
deleteData(&list, 1, &val);
printf("删除的数据为:%d \n", val);
display(&list);
getData(&list, 2, &val);
printf("第2位的值为:%d \n", val);
getLocate(&list, 2, &val);
printf("数字2的位置%d \n", val);
destroy(&list);
return 0;
}