首页后端开发其他后端知识c语言函数单独运行吗 c语言函数单独运行吗

c语言函数单独运行吗 c语言函数单独运行吗

时间2023-07-08 15:01:02发布访客分类其他后端知识浏览1117
导读:C语言实现数据结构的线性链表,插入和删除函数单独运行的时候都正确,为什么先插入后删除就运行不了? //我只能说,代码逻辑混乱,特橘棚别是索引,怎么是从1开始的,与一般逻辑不符(从0开始)//按照你的意图修改如下,能正常运行了。#includ...

C语言实现数据结构的线性链表,插入和删除函数单独运行的时候都正确,为什么先插入后删除就运行不了?

//我只能说,代码逻辑混乱,特橘棚别是索引,怎么是从1开始的,与一般逻辑不符(从0开始)

//按照你的意图修改如下,能正常运行了。

#includeiostream

#includestdlib.h

using namespace std;

#define null 0

typedef char ElemType;

typedef struct LNode

{

ElemType data;

struct LNode *next;

} LNode,*LinkList;

int ListLength(LinkList L) {

int n=0;

struct LNode*q=L;

if(L==NULL)

cout"链表为空"endl;

while(q!=NULL)

{

q=q-next;

n++;

}

return n;

}

ElemType GetElem(LinkList L,int i)

{

int j=1;

struct LNode*q=L;

while(jiq!=NULL)

{

q=q-next;

j++;

}

if(q!=NULL)

return (q-data);

else

cout"位置参数不正确"endl;

return 0;

}

void InsertList (LinkList L,ElemType x,int i)

{

int j=1;

struct LNode*q,*s;

s=new LNode;

s-data=x;

q=L;

if(i==1)

{

s-next=q;

L=s;

}

else

{

while(jiq-迅桐next!=NULL)

{

q=q-next;

j++;

}

if(j==i-1)

{

s-next=q-next;

q-next=s;

}

else

cout"位置参数不正确"endl;

}

}

int DeleteElem(LinkList L,int i)

{

int j=1;

struct LNode *q=L,*t;

if(i==1)

{

t=q;

L=q-next;

}

else

{

while(ji-1q-next!=NULL)

{

q=q-next;

j++;

}

if(q-next!=NULLj==i-1)

{

t-next=q-next;

q-next=t;

}

else

cout"位置参数不正圆昌则确"endl;

}

if(t)

delete t;

return 0;

}

void DisplayList(LinkList L)

{

struct LNode*q;

q=L;

cout"链表元素: ";

if(q==NULL)

cout"链表为空"endl;

else

{

while(q!=NULL)

{

coutq-data" ";

q=q-next;

}

}

coutendl;

}

void main()

{

LinkList LA = NULL;

ElemType cs;

int len=0;

InsertList(LA, 'a', 1);

InsertList(LA, 'B', 2);

InsertList(LA, 'C', 3);

cs=GetElem(LA, 3);

len=ListLength(LA);

DisplayList(LA);

}

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: c语言函数单独运行吗 c语言函数单独运行吗
本文地址: https://pptw.com/jishu/296175.html
c语言退出循环函数 c语言可以退出的菜单循环 c语言的头文件库函数 c语言头文件介绍

游客 回复需填写必要信息