#include<iostream.h>
#include<stdlib.h>
#include"testlist.h"
list::list()
{
head=new listnode();
cout<<"链表头节点已建立"<<endl;
size=0;
}
list::~list()
{
listnode*p=head;
listnode*q=head;
for(int i=0;i<size;i++)
{
p=q->next;
delete q;
q=p;
}
cout<<"析构函数执行完毕"<<endl;
}
int list::listsize()
{
cout<<"链表长度为"<<size<<endl;
return size;
}
int list::listempty()
{
if(size==0)
return 0;
else
return 1;
}
listnode*list::index(int pos)
{
if(pos==0)
return head;
else if(pos>size)
{
cout<<"超出范围"<<endl;
exit(0);
}
else
{
listnode*p=head;
for(int i=0;i<pos;i++)
{
p=p->next;
}
return p;
}
}
void list::insert()
{
cout<<"请输入要插入的位置"<<endl;
listnode*p=head;
listnode*q=head;
int ddata;
int pos;
cin>>pos;
cout<<endl;
while(pos==0||pos>size+1)
{
cout<<"链表的长度为"<<size<<", 有效范围为1---"<<size+1<<",输入的范围错误,请重新输入";
cin>>pos;
cout<<endl;
}
cout<<"请输入插入的数字"<<endl;
cin>>ddata;
cout<<endl;
for(int i=0;i<pos-1;i++)
{
p=p->next;
}
listnode*newcode=new listnode(ddata,p);
newcode->next=p->next;
p->next=newcode;
size++;
}
void list::coutt()
{
if(head->next!=NULL)
{
listnode*p=head;
for(int i=1;i<=size;i++)
{
cout<<"第"<<i<<"个元素为"<<p->next->data<<endl;
p=p->next;
}
}
else
{
cout<<"链表为空"<<endl;
}
}
void list::deleted()
{
if(head->next==NULL)
{
cout<<"链表为空,无法删除"<<endl;
exit(0);
}
cout<<"请输入要删除的位置"<<endl;
int pos;
cin>>pos;
cout<<endl;
while(pos==0||pos>size)
{
cout<<"链表的长度为"<<size<<", 有效范围为1---"<<size<<",输入的范围错误,请重新输入";
cin>>pos;
cout<<endl;
}
listnode*p=head;
if(head->next==NULL)
{
cout<<"链表为空,无法删除"<<endl;
exit(0);
}
if(pos<1||pos>size)
{
cout<<"有效范围是1---"<<size<<",请输入正确的范围"<<endl;
exit(0);
}
for(int i=0;i<pos-1;i++)
p=p->next;
listnode *q=p->next;
p->next=q->next;
delete q;
size--;
}
int list::getdata()
{
cout<<"请输入要查找的位置"<<endl;
int pos;
cin>>pos;
cout<<endl;
while(pos==0||pos>size)
{
cout<<"链表的长度为"<<size<<", 有效范围为1---"<<size<<",输入的范围错误,请重新输入";
cin>>pos;
cout<<endl;
}
listnode*p=head;
for(int i=1;i<=pos;i++)
{
p=p->next;
}
return p->data;
}
void list::create()
{
}
void list::reverse()
{
if(head->next==NULL)
{
cout<<"链表为空,无需倒转"<<endl;
}
else
{
listnode*p=head->next;
listnode*q=NULL;
while(p->next!=NULL)
{
listnode*r=q;
q=p;
p=p->next;
q->next=r;
}
p->next=q;
head->next=p;
}
}
c++数据结构链表的使用
5星 · 超过95%的资源 需积分: 15 2 浏览量
2008-12-21
19:35:15
上传
评论
收藏 146KB RAR 举报
chhaecnkcenr
- 粉丝: 0
- 资源: 18
最新资源
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
- 基于单片机的风力发电机转速控制源码
- 基于C++实现的风力发电气动平衡监测系统+源代码+测量数据(高分课程设计)
- 毕业设计- 基于STM32F103C8T6 单片机,物联网技术的太阳能发电装置+源代码+文档说明+架构图+界面截图
- 基于 LSTM(长短期记忆)(即改进的循环神经网络)预测风力发电厂中风力涡轮机产生的功率+源代码+文档说明
- 基于stm32f103+空心杯电机+oled按键+运动算法
- 《CKA/CKAD应试指南/从docker到kubernetes 完全攻略》学习笔记 第1章docker基础(1.1-1.4)
- 基于python实现的水下压缩空气储能互补系统建模仿真与经济效益分析+源代码+论文
- 华中科技大学-自然语言处理实验,Bi-LSTM+CRF的中文分词框架,并且利用基于深度学习的方法进行中文命名实体识别++源码报告
- 基于动态罚函数的铁路车流分配与径路优化模型python源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈