哪位大哥帮忙解解这道题目?

hzg_1998 2008-03-03 09:39:31
1. 打开一个ASCII文件input_data_file.txt, 在这个文件中,每行都有一个整数(integer),把他们存入到一个链表中(linked list).
2. 用冒泡排序算法(bubble sort algorithm)将这些整数排序。
3. 把有序列表写到文件output_file_1.txt中。
4. 打开一个ASCII文件delete_data_file.txt,在这个文件中有想要从第一个列表中消除(delete) 的整数。
5. 把结果的列表写到output_file_2.txt中。
...全文
237 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
canybox 2008-03-04
  • 打赏
  • 举报
回复
//struct definition for single-direction list.
typedef struct s_node{
int data;
s_node* next;
}s_node_t;


void bubble(s_node_t* node);

int _tmain(int argc, _TCHAR* argv[])
{

FILE* in = fopen("input_data_file.txt", "r");
s_node_t* head = NULL;

if (fpFile != 0)
{
int data;
//s_node_t* head = NULL;
s_node_t* prev = NULL;
s_node_t* pos = NULL;

while (fscanf(in, "%d", &data) == 1)
{
pos = (s_node_t*)malloc(sizeof(s_node_t));
pos->data = data;
pos->next = NULL;

if (head == 0)
head = pos;

if (prev != 0)
prev->next = pos;

prev = pos;
}

bubble(head);

fclose(in);
}

FILE* out = fopen("output_file_1.txt","w");
if ( head && out )
{
s_node_t* node = head;
while(node)
{
fprintf(out,"%d\n",node->data);
node = node->next;
}
fclose(out);
}



return 0;
}

void bubble(s_node_t* node)
{
int data;
s_node_t* p=NULL,*q=NULL;

for(p=node;p->next!=NULL;p=p->next)
{
s_node_t* temp = p;
for(q=p->next;q;q=q->next)
if(q->data < temp->data)
temp = q;

if (temp!=p)
{
data = p->data;
p->data = temp->data;
temp->data = data;
}
}

}
hzg_1998 2008-03-04
  • 打赏
  • 举报
回复
谢谢各位
结贴
dubiousway 2008-03-04
  • 打赏
  • 举报
回复
output_file_1.txt
41
491
2995
4827
5436
5705
6334
9961
11478
11942
15724
16827
18467
19169
23281
24464
26500
26962
28145
29358

delete_data_file.txt
15724
29358
26962
5705
23281
168

output_file_2.txt
41
491
2995
4827
5436
6334
9961
11478
11942
16827
18467
19169
24464
26500
28145

--------------------
以上为随即生成20个数的建表、排序、删除、结果写入文件的结果。
dubiousway 2008-03-04
  • 打赏
  • 举报
回复

#include <stdio.h>

struct node{
int data;
node* next;
};

int main(){

FILE* fp1, *fp2, *fp3, *fp4;
fp1= fopen("input_data_file.txt", "r");
fp2= fopen("output_file_1.txt", "w");
fp3= fopen("delete_data_file.txt", "r");
fp4= fopen("output_file_2.txt", "w");
if(!(fp1 && fp2 && fp3 && fp4)){
printf("Error: Cannot open files for reading or writing!");
return 0;
}

node *head=NULL, *tail, *temp;

//create chain:
int Int;
while(!feof(fp1)){
if(fscanf(fp1,"%d", &Int)==EOF)
break;
if(!head){
head= tail= new node;
head->data= Int;
head->next= NULL;
}
else{
temp= new node;
temp->data= Int;
temp->next= NULL;
tail->next= temp;
tail= temp;
}
}

//sort:
node *temp2;
while(tail!=head){
temp= temp2= head;
while(temp!=tail){
if(temp->data > temp->next->data){
if(temp==head){
temp2= head->next->next;
head= head->next;
head->next->next= temp;
temp->next= temp2;
temp2= head;
}
else{
temp2->next= temp->next;
temp2= temp->next;
temp->next= temp2->next;
temp2->next= temp;
}
if(tail==temp2)
break;
}
else{
if(temp!=head)
temp2=temp2->next;
temp= temp->next;
}
}
tail= temp2;
}

//write output_file_1.txt
temp= head;
while(temp){
fprintf(fp2, "%d\n", temp->data);
temp= temp->next;
}


//delete nodes:
while(!feof(fp3)){
if(fscanf(fp3,"%d", &Int)==EOF)
break;
temp2= head;
temp= head->next;
while(head && head->data== Int){
head= head->next;
delete temp2;
temp2= head;
}
while(temp && temp->data!=Int){
temp2= temp;
temp= temp->next;
}
while(temp && temp->data == Int){
temp2->next= temp->next;
delete temp;
temp= temp2->next;
}
}

//write output_file_2.txt
temp= head;
while(temp){
fprintf(fp4, "%d\n", temp->data);
temp= temp->next;
}



fclose(fp1);
fclose(fp2);
fclose(fp3);
fclose(fp4);

return 0;
}



input_data_file.txt
41
18467
6334
26500
19169
15724
11478
29358
26962
24464
5705
28145
23281
16827
9961
491
2995
11942
4827
5436


星羽 2008-03-03
  • 打赏
  • 举报
回复

自己多想想了,比如


typedef struct _Node {
int data;
struct _Node* pNext; // 为了冒泡方便,可以采用双连表
struct _Node* pPrev;
}Node, *Link;

int main()
{
/*
1. 打开一个ASCII文件input_data_file.txt, 在这个文件中,
每行都有一个整数(integer),把他们存入到一个链表中(linked list
*/

FILE* fpFile = fopen("input_data_file.txt", "r");

if (fpFile != 0)
{
Link pHead = 0;
Link pCurr = 0;
Link pPrev = 0;
int data = 0;

while (fscanf(fpFile, "%d", &data) == 1)
{
pCurr = (Link)malloc(sizeof(Node));
pCurr->data = data;
pCurr->pNext = 0;
pCurr->pPrev = pPrev;

if (pHead == 0)
pHead = pCurr;

if (pPrev != 0)
pPrev->pNext = pCurr;

pPrev = pCurr;
}
fclose(fpFile);
}

return 0;
}


。。。。。

后面自己写咯,冒泡算法应该懂吧,不难,多动手
hzg_1998 2008-03-03
  • 打赏
  • 举报
回复
好久没写代码了,今天人家问我,不好意思说不会,只有在这请教各位GGJJ了
bonny95 2008-03-03
  • 打赏
  • 举报
回复
看着题目好眼熟啊,很有计算机等级考试题目的味道……
建议楼主自己解决吧,这题目本身并不难做。
chlaws 2008-03-03
  • 打赏
  • 举报
回复
struct NODE{
int value;
struct NODE *next;
};
typedef struct NODE node;

结构很简单
思路也很简单,ASCI文件就是文本文件 用fprintf()/fscanf()
fgetc()/fputc()
这些函数操作下

导入链表就OK了.OK了后就是对链表的查找删除的操作.这不难的吧

操作完成再fprintf到文件,保存下.
hzg_1998 2008-03-03
  • 打赏
  • 举报
回复
能做出就行
厦门王五 2008-03-03
  • 打赏
  • 举报
回复
想知道你要什么水平的,
我是刚学完C不久的
chlaws 2008-03-03
  • 打赏
  • 举报
回复
先坐SF

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧