您的位置:首页 > 编程语言 > C语言/C++

c语言链式存储实现

2015-10-24 19:47 423 查看
之前写了一个关于线性表的顺序存储,大家都知道,在链表操作中,为了方便的删除,增加元素而无须大量地移动元素,通常来说,链表都是链式存储,今天我就写一个这个关于链表的链式存储的一些相关操作。包括创建链表,头插和尾插法创建链表,求链表的长度,插入元素和删除元素,还有就是下查找固定位置的链表并返回其元素的值。
#include<stdio.h>
#include<malloc.h>

#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int Status;
/*******
结点定义
*/
typedef struct Node
{
ElemType data;
struct Node *next;
} Node,*pNode;
/********
创建带头结点的空链表
*/
pNode CreateList()
{
pNode L;
L=(pNode)malloc(sizeof(Node));
L->next=NULL;
return L;
}
/************
头插法
*/
void headInsertList(pNode L,int e)
{
pNode p=(pNode)malloc(sizeof(Node));
p->data=e;
p->next=L->next;
L->next=p;
}
/**********
链表的输出
*/
void printList(pNode L)
{
pNode p=L->next;
while(p)
{
printf("%d  ",p->data);
p=p->next;
}
printf("\n");
}
/************
链表元素的个数
*/
int lengthList(pNode L)
{
int length=0;
pNode p=L->next;
while(p)
{
++length;
p=p->next;
}
return length;
}
/********
链表的尾插法
*/
void tailInsertList(pNode L,ElemType e)
{
pNode p=L->next;
pNode q=(pNode)malloc(sizeof(Node));
q->data=e;
while(p->next)
{
p=p->next;
}
q->next=NULL;
p->next=q;
}
/*******
在链表的第i个位置插入元素e
*/
Status InsertList(pNode L,int pos,ElemType e)
{
pNode p=L;
pNode s;//待插入结点
int j=1;
while(p && j<pos)
{
p=p->next;
++j;
}
if(!p || j>pos)
return ERROR;
s=(pNode)malloc(sizeof(Node));
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
/**********
删除链表第i个位置的元素并返回该元素
*/
Status deleteList(pNode L,int pos,ElemType *e)
{
pNode p=L;
pNode q;
int j=1;
while(p->next && j<pos)
{
p=p->next;
++j;
}
if(!(p->next) || j>pos)
return ERROR;
q=p->next;
*e=q->data;
p->next=q->next;
free(q);
return OK;
}
/**********
获得第 i 个位置的元素
*/
Status GetElem(pNode L,int i,ElemType *e)
{
pNode p;
int j=1;
p=L->next;//p指向第一个元素

while(p && j<i)
{
p=p->next;
++j;
}
if(!p || j>i)
{
return ERROR;
}
*e=p->data;
return OK;
}
int main()
{
/*       pNode L;
int i=1;
int m;//获得某个位置元素的值
int length;//保存链表长度
int n;//保存删除后结点的值
L=CreateList();
headInsertList(L,i);
tailInsertList(L,2);
headInsertList(L,3);
//    length=lengthList(L);

printList(L);
InsertList(L,4,4);
printList(L);
deleteList(L,4,&n);
printf("%d\n",n);
printList(L);
GetElem(L,2,&m);
printf("%d\n",m);

//    printf("%d\n",length);*/
int i;
pNode L;
printf("创建链表请输入1:\n增加链表元素请输入2:\n删除元素请输入3:\n查找元素请输入4\n输出链表请按5\n输出链表长度请输入6:\n结束请输入0:\n");
while(1)
{
scanf("%d",&i);
switch(i)
{
case 0:
return 0;
break;
case 1:
{

int n,i;
ElemType e;
printf("请输入创建链表元素的个数:\n");
scanf("%d",&n);
if(n<=0)
{
return -1;
}

L=CreateList();
for(i=0; i<n; i++)
{
printf("请输入第%d个要插入的数字:",(i+1));
scanf("%d",&e);
headInsertList(L,e);
}
}
break;
case 2:
{

int pos;
ElemType e;
printf("请输入要插入的位置:\n");
scanf("%d",&pos);
printf("请输入要插入的元素:\n");
scanf("%d",&e);
InsertList(L,pos,e);
}
break;
case 3:
{
int pos;
ElemType e;
printf("请输入要删除元素的位置:\n");
scanf("%d",&pos);
deleteList(L,pos,&e);
}
break;
case 4:
{
int  pos;
int r;//接收状态返回值
ElemType e;
printf("请输入要查找元素的位置:\n");
scanf("%d",&pos);
r=GetElem(L,pos,&e);
printf("%d\n",e);
}
break;
case 5:
printList(L);
break;
case 6:
printf("%d\n",lengthList(L));
break;
default:
printf("输入数据有误,请重新输入!\n");
break;
}
}

return 0;
<p>}
</p><p><span style="font-family: monospace; white-space: pre; background-color: rgb(240, 240, 240);">上面的代码已亲自测试过,如有哪不妥的地方,请大神指出。共同学习,一起进步。</span>
</p>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: