|
珞珈山水BBS →
电脑网络 →
程序人生 →
单文区文章阅读
|
| 单文区文章阅读 [返回] |
|---|
|
发信人: simi33 (feng), 信区: Programm 标 题: 我的痛苦链表 发信站: BBS 珞珈山水站 (Sun Apr 24 00:09:42 2005) 前几天写了一个排序的链表,有一个地方出了错误,今晚终于找出了,不容易啊。源代码 如下: // console2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<iostream.h> #include<stdio.h> class Node{ public: int data; Node *next; Node() { Node(0); } Node(int data) { this->data=data; next=NULL; } }; void ShowList(Node *head) { while(head!=NULL) { cout<<head->data ; cout<<" "; head=head->next ; } cout<<endl; } Node* BuildSortList(int *a,int length) { Node *ret=new Node(a[0]); for(int xi=1;xi<length;xi++) { Node *loc=new Node(a[xi]); Node *pre1,*pre2=NULL; pre1=ret; if(a[xi]<ret->data) { loc->next =ret; ret=loc; } else { while(pre1!=NULL&&pre1->data<a[xi])//错误出现的地 //方,我写成了:pre1->data<a[xi])&&pre1->data<a[xi]) { pre2=pre1; pre1=pre1->next ; } pre2->next =loc; loc->next =pre1; } } return ret; } void freeList(Node *head) { while(head) { Node *loc=head; head=head->next ; delete loc; } } int main(int argc, char* argv[]) { int c[]={2,21,3,9,72,91,4}; Node *Head=BuildSortList(c,sizeof(c)/sizeof(c[0])); ShowList(Head); freeList(Head); return 0; } 瞧瞧:只是前后位置交换了,就出了这么大的错误,搞地我好几天不得其解! -- ※ 来源:·珞珈山水BBS站 http://bbs.whu.edu.cn·[FROM: 222.20.197.*] |
| [返回单文区目录] |
|
|