请教用VC++6.0怎样建立一个单链表

 我来答
左右TU
2017-09-25 · TA获得超过157个赞
知道答主
回答量:57
采纳率:75%
帮助的人:40.1万
展开全部
#include<stdio.h>
#include<stdlib.h>

typedef struct Node
{
int element;
struct Node *link;
} Node;
typedef struct SingleList
{
struct Node *first;
int n;
} SingleList;


void mulu()
{
printf("请根据以下提示操作:\n");
printf("1. 检索一个位置上的元素\n");
printf("2. 定位插入一个元素\n");
printf("3. 定位删除一个元素\n");
printf("4. 撤销\n");
printf("ATTENTION:本链表序号从0开始!!!!!!\n");
}

void Init(SingleList *list)
{
list->first=NULL;
list->n=0;
}

void Find(SingleList L,int i,int* x)
{
Node *p;
if(i<0 || i>L.n-1)
{
printf("超出范围!\n");
return;
}
p=L.first;
for(int j=0;j<i;j++)
{
*x=p->element;
p=p->link;
}
}

void Insert(SingleList *L,int i,int x)
{
Node *p,*q;
if(i<-1 || i>L->n)
{
printf("超出范围!\n");
return;
}
p=L->first;
for(int j=0;j<i;j++)
{
p=p->link;
}
q=(Node*)malloc(sizeof(Node));
q->element=x;
if(i>-1 && i<L->n)
{
q->link=p->link;
p->link=q;
}
else if(i==L->n)
{
q->link=NULL;
p->link=q;
}
else if(i==-1)
{
q->link=L->first;
L->first=q;
}
L->n++;
}

void Delete(SingleList *L,int i)
{
Node *p;
if(i<0 || i>L->n-1)
{
printf("超出范围!\n");
return;
}
p=L->first;
for(int j=0;j<i;j++)
{
p=p->link;
}
if (i==0)
{
L->first=p->link;

else
{
p->link=p->link->link;
}
L->n--;
}

void Output(SingleList L)
{
Node *p;
if(!L.n)
{
return;
}
p=L.first;
while(p)
{
printf("%d, ",p->element);
p=p->link;
}
}

void Destroy(SingleList *L)
{
Node *p;
while(L->first)
{
p=L->first->link;
free(L->first);
L->first=p;
}
}


int main()
{
int i=0,n=0;
int x=0,size=0;
SingleList list;
printf("请输入线性表的数据个数:");
scanf("%d",&size);
Init(&list);
list.n=size;
list.first=(Node*)malloc(sizeof(Node));
for(int j=0; j<size; j++)
{
Insert(&list, j, 0);
list.n--;
}
printf("\n");
mulu();
while(1)
{
printf("请按照目录的提示完成操作:");
scanf("%d",&n);
switch(n)
{
case(1):
{
printf("请输入要查询的位置:");
scanf("%d",&i);
Find(list,i,&x);
printf("%d\n",x);
system("pause");
break;
}
case(2):
{
printf("请输入位置和值,中间用一个空格间隔:");
scanf("%d %d",&i,&x);
Insert(&list,i,x);
system("pause");
break;
}
case(3):
{
printf("请输入要删除的位置:");
scanf("%d",&i);
Delete(&list,i);
system("pause");
break;
}
case(4):
{
Destroy(&list);
system("pause");
break;
}
}
}
return 0;
}

这是一个简单单链表的实例,用的不是类是结构体,但是总体思路是和结构是相同的。

希望可以帮到你

本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
归悦欣cf
2017-09-24 · TA获得超过1444个赞
知道大有可为答主
回答量:4250
采纳率:59%
帮助的人:1387万
展开全部
和使用C语言是一样的,找一个c语言的例子拿来用就可以。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式