用c写的一个单链表程序,在VC6.0环境中运行总是出错!

grn1985bear grn1985bear 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/*单链表的各种操作*/

# define null 0

typedef char ElemType; /* 字符型数据*/

typedef struct LNode
{
ElemType data;
struct LNode *next;
};

setnull(struct LNode **p);
int length (struct LNode **p);
ElemType get(struct LNode **p,int i);
void insert(struct LNode **p,ElemType x,int i);
int delete(struct LNode **p,int i);
void display(struct LNode **p);

main()
{
struct LNode *head,*q; /*定义静态变量*/
int select,x1,x2,x3,x4;
int i,n;
int m,g;
char e,y;

head=setnull(&head); /*建议链表并设置为空表*/
printf("请输入数据长度: ");
scanf("%d",&n);
for(i=1;i<n;i++);
{
printf("将数据插入到单链表中: ");
scanf("%d",&y);
insert(&head,y,i);} /*插入数据到链表*/
display(&head); /*显示链表所有数据*/

printf("select 1 求长度 length()\n");
printf("select 2 取结点 get()\n");
printf("select 3 求值查找 locate()\n");
printf("select 4 删除结点 delete()\n");
printf("input your select: ");
scanf("%d",&select);
switch(select)
{
case 1:
{
x1=length(&head);
printf("输出单链表的长度%d ",x1);
display(&head);
}break;

case 2:
{
printf("请输入要取得结点: ");
scanf("%d",&m);
x2=get(&head,m);
printf(x2);
display(&head);
}break;

case 3:
{
printf("请输入要查找的数据: ");
scanf("%d",&e);
x3=locate(&head,e);
printf(x3);
display(&head);
}break;

case 4:
{
printf("请输入要删除的结点: ");
scanf("%d",&g);
x4=delete(&head,g);
printf(x4);
display(&head);
}break;
}
}
}


setnull(struct LNode **p)
{
*p=null;
}

int length (struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=null)
{
n++;
q=q->next;
}
return(n);
}

ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (j<i&&q!=null)
{
q=q->next;
j++;
}
if(q!=null)
return(q->data);
else
printf("位置参数不正确!\n");
}

int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=null&&q->data!=x)
{
q=q->next;
n++;
}
if(q==null)
return(-1);
else
return(n+1);
}

void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode));
s->data=x;
q=*p;
if(i==1)
{
s->next=q;
p=s;
}
else
{
while(j<i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(j==i-1)
{
s->next=q->next;
q->next=s;
}
else
printf("位置参数不正确!\n");
}
}

int delete(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if(i==1)
{
t=q;
*p=q->next;
}
else
{
while(j<i-1&&q->next!=null)
{
q=q->next;
j++;
}
if(q->next!=null&&j==i-1)
{
t=q->next;
q->next=t->next;
}
else
printf("位置参数不正确!\n");
}
if(t=null)
free(t);
}

void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf("单链表显示: ");
if(q==null)
printf("链表为空!");
else if (q->next==null)
printf("%c\n",q->data);
else
{
while(q->next!=null)
{
printf("%c->",q->data);
q=q->next;
}
printf("%c",q->data);
}
printf("\n");
}

--------------------Configuration: 单链表 - Win32 Debug--------------------
Compiling...
单链表.c
C:\Documents and Settings\guozi\桌面\单链表.c(28) : warning C4047: '=' : 'struct LNode *' differs in levels of indirection from 'int '
C:\Documents and Settings\guozi\桌面\单链表.c(29) : warning C4013: 'printf' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(30) : warning C4013: 'scanf' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(66) : warning C4013: 'locate' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(22) : warning C4101: 'q' : unreferenced local variable
C:\Documents and Settings\guozi\桌面\单链表.c(81) : error C2059: syntax error : '}'
C:\Documents and Settings\guozi\桌面\单链表.c(135) : warning C4013: 'malloc' undefined; assuming extern returning int
C:\Documents and Settings\guozi\桌面\单链表.c(141) : warning C4047: '=' : 'struct LNode ** ' differs in levels of indirection from 'struct LNode *'
C:\Documents and Settings\guozi\桌面\单链表.c(185) : warning C4013: 'free' undefined; assuming extern returning int
执行 cl.exe 时出错.

单链表.obj - 1 error(s), 0 warning(s)
...全文

相关推荐

如何用VC6.0编写C语言程序

本文将详细介绍如何用VC6.0编写C语言程序,为初学者提供一个清晰的指南。首先,安装VC6.0是开启编程之旅的第一步。

wwfphd

增强版贪吃蛇游戏开发:基于VC6.0与Win32 API的完整实战

Windows桌面编程中,Win32 API 是理解底层消息机制与绘图流程的基础。消息循环驱动事件响应,而定时器(Timer)能周期性触发游戏逻辑,结合双缓冲技术,可在高频重绘下避免画面闪烁,确保动画流畅。这些原理不仅适用于贪吃蛇之类的小游戏,也是商业GUI框架的通用基石。状态机管理与文件持久化进一步提升了程序的健壮性与用户体验。在VC6.0环境下,通过合理的数据结构和碰撞检测顺序,可实现一个功能完整的增强版贪吃蛇:包含双区食物、动态速度曲线、障碍物、暂停及最高分存档。本文从方案选型到调试实录,系统分享了W

于欣烈

VC6.0打开文件时出错的解决办法

标题“VC6.0打开文件时出错的解决办法”涉及的问题是,用户在尝试使用Microsoft Visual C++ 6.0(简称VC6.0)的“打开”功能时遇到错误,导致程序崩溃。

lilisapphire

在VC6上编写C程序

尽管有更多现代化的开发环境出现,如Visual Studio的更新版本,但许多开发者和初学者仍然钟情于使用Visual C++ 6.0(简称VC6)来编写C程序。

tonghuasu

用VC6.0写的示波器,初学VC的人界面设计的好源码!

【标题】"用VC6.0写的示波器"是一个基于Microsoft Visual C++ 6.0(简称VC6.0)开发的项目,它展示了如何使用C++编程语言和MFC(Microsoft Foundation

chenwen19702
评论 4
收藏 已收藏
分享
返回顶部
grn1985bear
C语言 70,040 成员 243,237 内容