FS81 2022-03-15 20:02 采纳率: 0%
浏览 19

数据结构单链表的基本操作

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
//定义一个结构体student
typedef struct
{
int age;
char name[10];
int number;
}student;
typedef student Etype;
//用结构体定义结点的数据类型
struct chainnode
{
Etype data;
struct chainnode *link;
};
typedef struct
{ chainnode *first;
int length;
}chainhead;
//初始化链表为空链表
chainhead * L;
chainhead * Creat(chainhead *L)
{
L->first=NULL;
return L;
};
//实现单链表元素的后插操作
bool chainInsert(chainhead *L,int i,Etype &e)
{ if(i<1)
return false;
if(i==1)
{
chainnode *q=new chainnode;
q->data=e;
q->link=L->first;
L->first=q;
}
chainnode *p=L->first;
int j=1;
while(p!=NULL&&j<i-1)//循环找到第i-1个结点
{
j++;
p=p->link ;

}
if(p==NULL)
    return false;//i值不合法
chainnode *q=new chainnode;
q->data=e;
q->link=p->link ;
p->link =q;
return true;

};

//按位查询并返回结点
chainnode *Getelement(chainhead * L,int i)
{
int j=1;
chainnode *p=L->first ;
if(i<=0)
return NULL;
while(p!=NULL&&j<i)
{
j++;
p=p->link;
}
return p;
};
//按位删除结点
bool chainDelete(chainhead * L,int k)
{ if(k<1||L->first ==NULL)
return false;
chainnode *p=L->first ;
if(k==1)
{
L->first =p->link ;
return true;
}
int j=1;
while(p!=NULL&&j<k-1)
{ p=p->link ;
j++;
}
if(p==NULL||p->link ==NULL)
return false;
chainnode *s=p->link;
p->link =s->link ;
delete s;
return true;
};
//按值查找结点
chainnode *Locateelement(chainhead *L,int a)
{ chainnode *p=L->first ;
while(p!=NULL&& p->data.number !=a)
p=p->link ;
if(p==NULL)
{ cout<<"没有查询到结果"<<endl;
return NULL;
}

else
    return p;

};
//删除单链表
void Destroy(chainhead *L)
{ chainnode *current=L->first ;
while(L->first )
{
current=current->link ;
delete L->first ;
L->first =current;
}
delete L;
};
void CSH(Etype Student[10])
{
int number[10]={1,2,3,4,5,6,7,8,9,10};
int age[10]={18,18,18,18,18,18,18,18,18,18};
char name[10][10]={"李白","杜甫","白居易","欧阳修","苏轼","曾巩","苏洵","苏辙","王安石","张九龄"};
for(int i=0;i<10;i++)
{
strcpy(Student[i].name,name[i]);
Student[i].number=number[i];
Student[i].age=age[i];
}
};
//设置新同学信息
void Insertelement(Etype &e)
{ cout<<"请输入新同学的姓名 :"<<endl;
cin>>e.name ;
cout<<"请输入新同学的学号 :"<<endl;
cin>>e.number ;
cout<<"请输入新同学的年龄 :"<<endl;
cin>>e.age ;
};
int count(int length)
{
chainnode *current=L->first ;
while(current)
length++;
return length;
};

int main()
{
Creat(L);
Etype Student[10];
Etype e;
CSH(Student);
for(int i=0;i<10;i++)
chainInsert(L,i,Student[i]);
Insertelement(e);
cout<<"请输入想要插入的位置"<<endl;
int k;
cin>>k;
chainInsert(L,k,e);
cout<<"请输入想要查询的学生的学号:"<<endl;
int a;
cin>>a;
cout<<"该学生的信息如下 :"<<endl;
cout<<Locateelement(L,a);
cout<<"输入你想找到的同学的位序 :"<<endl;
int b;
cin>>b;
cout<<"该学生的信息如下 :"<<endl;
cout<<Getelement(L,b);
cout<<"请输入想要删除的元素的位序 :"<<endl;
int c;
cin>>c;
chainDelete(L,c);
int length=1;
cout<<"链表的长度为:"<<endl;
count(length);
Destroy(L);
return 0;
};

最后的结果是这个,不知道是哪个位置不对

img

  • 写回答

1条回答 默认 最新

  • _雪菜肉丝面_ 2022-03-15 20:03
    关注

    我的想法:
    1,测试按部就班。不要一万行测试一下。

    评论

报告相同问题?

问题事件

  • 创建了问题 3月15日

悬赏问题

  • ¥15 原子操作+O3编译,程序挂住
  • ¥15 使用STM32F103C6微控制器设计两个从0到F计数的一位数计数器(数字),同时,有一个控制按钮,可以选择哪个计数器工作:需要两个七段显示器和一个按钮。
  • ¥15 写51单片机程序代码,代码好难,需要外部中断还有电子时钟的,还有按键修改电子时钟,请大家积极讨论
  • ¥15 在yolo1到yolo11网络模型中,具体有哪些模型可以用作图像分类?
  • ¥15 AD9910输出波形向上偏移,波谷不为0V
  • ¥15 淘宝自动下单XPath自动点击插件无法点击特定<span>元素,如何解决?
  • ¥15 曙光1620-g30服务器安装硬盘后 看不到硬盘
  • ¥15 抖音直播广场scheme
  • ¥15 为什么我明明有这个文件调试器还显示错误?
  • ¥15 软件工程用例图的建立(相关搜索:软件工程用例图|画图)