您的位置:首页 > 其它

有些类似于MSDOS命令行方式的单链表操作

2005-10-17 21:13 288 查看
学校的VC++实验课程设计,是关于单链表的操作的,觉得应该做的与众不同一些...就想做一个类似于MSDOS的命令行方式操作的东东.所以就有了这堆代码,其实这个小程序很简陋的.因为没有时间去继续构思和补充它了,或许真应该像老师说的那样,研究一下编译原理.所以说,不要把以前DOS之类的操作系统看得太easy...高深得很呢!有机会应该研究下LINUX,因为人家是开源的,在那里应该会比较快的得到类似解决用户输入问题的答案.很希望将来也有机会在很多人都使用的操作系统中留下自己的名字.
VC++6.0下好使hoho~~~
linkstruct.h文件:
struct RECORD
{
int data;
RECORD *next;
};
class linkstruct
{
private:
RECORD *HEAD;
public:
void creatlist();
void insertlist();
void deletelist();
void editlist();
void findlist();
void viewlist();
//void sortlist();
linkstruct();
~linkstruct();
};
list.cpp文件:
#include <iostream.h>
#include "linkstruct.h"
void linkstruct::creatlist()
{
int i=10;
RECORD *p;
while(i--)
{
p=new RECORD;
p->data=i;
p->next = HEAD->next;
HEAD->next = p;
}
}
void linkstruct::insertlist(int x,int y)
{
RECORD *p,*s;
s=new RECORD;
p=HEAD->next;
int i=0;
while(p)
{
if(x+1==++i)
{
s->data=y;
s->next=p->next;
p->next=s;
}
p=p->next;
}
}
void linkstruct::deletelist(int x)
{
RECORD *p;
p=HEAD->next;
int i=0;
while(p)
{
if(x==++i)
{
p->next->data=NULL;
p->next=p->next->next;
}
p=p->next;
}
}
void linkstruct::editlist(int x,int y)
{
RECORD *p;
p=HEAD->next;
int i=0;
while(p)
{
if(x+1==++i)
{
p->data=y;
}
p=p->next;
}
}
void linkstruct::findlist(int x)
{
RECORD *p;
p=HEAD->next;
int i=0;
while(p)
{
if(x+1==++i)
{
cout<<"该节点的值:"<<p->data<<endl;
}
p=p->next;
}
}
void linkstruct::viewlist()
{
RECORD *p;
p=HEAD->next;
p=p->next;
cout<<"当前链表结构如下:";
while(p)
{
cout<<p->data;
if(p->next!=NULL)
cout<<"->";
p=p->next;
}
cout<<endl;
}

linkstruct::linkstruct()
{
HEAD = new RECORD;
HEAD->next = NULL;

}
linkstruct::~linkstruct()
{
RECORD *p,*q;
p=HEAD;
while (p)
{
q=p->next ;
delete p;
p=q;
}
}
main.cpp文件:
#include <iostream.h>
#include "linkstruct.h"
#include <string.h>
linkstruct aList;
void check(char,int,int);
void main()
{
char key;
int x=0,y=0;
aList.creatlist();
cout<<"类似于MSDOS命令行方式的单链表操作"<<endl<<endl<<endl;
cout<<"链表已经建立,请按照下面的帮助输入指令!"<<endl<<endl;
cout<<"插入节点格式:i 位置 数值"<<endl;
cout<<"删除节点格式:d 位置 0"<<endl;
cout<<"修改节点格式:e 位置 数值"<<endl;
cout<<"查询节点格式:f 位置 0"<<endl;
cout<<"推出链表系统:q 0 0"<<endl<<endl;
aList.viewlist();
cout<<endl<<endl<<endl;
do
{
cout<<"List://>";
cin>>key>>x>>y;
check(key,x,y);
aList.viewlist();
}while(key!='q');
cout<<"谢谢使用!"<<endl;
}
void check(char key,int x,int y)
{
if(key!='q')
{
switch (key)
{
case 'i':aList.insertlist(x,y);break;
case 'd':aList.deletelist(x);break;
case 'e':aList.editlist(x,y);break;
case 'f':aList.findlist(x);break;
default:cout<<"指令输入有误!"<<endl;
}
}
}
在乏味的程序设计中寻找快乐,寻找答案.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: