Files
wahyd4.github.com/source/_posts/2010-11-13-sort-node.md
T
2013-01-14 21:14:28 +08:00

116 lines
2.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: 数据结构作业:顺序表简单应用
author: wahyd4
comments: true
excerpt: >
今天算是比较用心的在电脑勉强做作业吧,但是毕竟不怎么喜欢写c/c++的代码,已经习惯用java了,觉得java写起来很舒服,所以做这个作业做了很久,至少3个小时吧。
layout: post
permalink: /2010/11/sort-node/
categories:
- 我说
---
今天算是比较用心的在电脑勉强做作业吧,但是毕竟不怎么喜欢写c/c++的代码,已经习惯用java了,觉得java写起来很舒服,所以做这个作业做了很久,至少3个小时吧。这学期现在还是第一次用心地做数据结构作业,代码经过调试可以完美运行,可是没有考虑运行的效率,和时间复杂度等等,等我有空的时候再来调试吧。可能写的不太符合规范,我本来想分模块话写,可是怎么也实现不了想要的功能,于是我就全部写在main函数里面了,算是完全面向过程吧,技术大牛不要笑哦,想要借鉴的同学完全可以拿去,代码保证正确。
#include <iostream>
using namespace std;
struct list
{
int tlist[50];
int size;
};
int main()
{
int temp;
int innum,delnum;
int flag;
void printList(list l);
list t;
cout< <”请输入你想要的链表长度:”< cin>>t.size;
cout< <”请输入链表”< for(int i=0;i cin>>t.tlist[i];
}
for(int j=0;j< (t.size-1);j++){
for(int k=j+1;k if(t.tlist[j]>t.tlist[k]){
temp=t.tlist[j];
t.tlist[j]=t.tlist[k];
t.tlist[k]=temp;
}
}
}
cout< <”排序后的链表为”< printList(t);
cout<<”情输入你想插入的数:”< cin>>innum;
if(innum>t.tlist[t.size-1]){
//要插入的数最大
t.tlist[t.size]=innum;
}else{
//要插入的数最小
if(innum flag=-1;
}else{
//要插入的数在中间
for( int m=0;m if(innum>t.tlist[m]&&innum flag=m;//取得要插入数所在位置,flag为比插入数的前一个位置
break;
}
}
}
for(int k=t.size-1;k>flag;k){
t.tlist[k+1]=t.tlist[k];
}
t.tlist[flag+1]=innum;
}
t.size=t.size+1;//将顺序链表长度加1
printList(t);
cout< <”请输入你想删除的数”< cin>>delnum;
for(int a=0;a if(delnum==t.tlist[a]){
cout<<”发现你想要删除的数,正在删除…”< if(delnum==t.tlist[t.size]){
t.size=t.size-1;
}else {
for(int b=a;b t.tlist[a]=t.tlist[a+1];
}
t.size=t.size-1;//删除成功长度减一
}
}
}
printList(t);//输出最终数据;
system(“pause”);
return 0;
}
<div id="_mcePaste">
void printList(list l)
</div>
<div id="_mcePaste">
{
</div>
<div id="_mcePaste">
cout<<”顺序链表为”<<endl;
</div>
<div id="_mcePaste">
for(int j=0;j<l.size;j++){
</div>
<div id="_mcePaste">
cout<<l.tlist[j]<<endl;
</div>
<div id="_mcePaste">
}
</div>
<div id="_mcePaste">
}
</div>