浪琴军旗手表l4.774.3:c++链表排序,急用

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/12 07:57:08
#include"iostream"
using namespace std;
struct Node
{ int num;
Node *next;
};
int main()
{
// input the numbers;
int n,count=1;
Node *first,*last;
first=last=new Node;
cout<<"please input a number(using -1 as the end):";
cin>>n;
if (n==-1)
{ cout<<"no number iuput!"<<endl;
return 0;
}
else first->num=n;
while(n!=-1)
{
cout<<"please input a number(using -1 as the end):";
cin>>n;
if(n==-1)
{cout<<"ÊäÈëÍê±Ï";
break;
}
else
{Node *p=new Node;
p->num=n;
last->next=p;
last=p;
count++;}
}
last->next=0;

//sort the numbers;
Node *to_be_sorted, *have_been_sorted, *p;
have_been_sorted=first;
p=first;
to_be_sorted=first->next;

while (to_be_sorted != 0)
{
have_been_sorted->next=0;
Node *temp=to_be_sorted->next;
if(to_be_sorted->num <= p->num)
{
to_be_sorted->next=first;
first=to_be_sorted;
}
else
{
Node *q=p->next;
while ((to_be_sorted->num > q->num) && (p->next!=have_been_sorted))
{ p=p->next;}
if ((p->next==have_been_sorted)&&(to_be_sorted->num > q->num) )
{ have_been_sorted->next=to_be_sorted;
have_been_sorted=to_be_sorted;
}
else{
to_be_sorted=p->next;
p->next=to_be_sorted;
}
}
to_be_sorted=temp;
}

//output the new number sequence;
Node *pp=first;
for(int m=1;m<=count;m++)
{

cout<<pp->num<<' ';
pp=pp->next;
}
return 0;
}
调试出现错误,望指正
急用,谢谢
谢谢回答,但是我们尚不会用类与对象。
如果能就着我的程序修改一下,我将非常感激

// linked list.cpp : Defines the entry point for the console application.
//

#include <stdio.h>
#include <iostream.h>
enum Error_code {success, overflow, underflow, range_error};
template <class Node_entry>
struct Node
{
//data members
Node_entry entry;
Node<Node_entry> *next;
//constructors
Node();
Node(Node_entry item, Node<Node_entry> *link = NULL);
};
template <class Node_entry>
Node<Node_entry>::Node()
{
next = NULL;
}

template <class Node_entry>
Node<Node_entry>::Node(Node_entry item, Node<Node_entry> *link)
{
entry = item;
next = link;
}

template<class List_entry>
class List
{
public:
~List();
List();
List(const List<List_entry> ©);
void operator=(const List<List_entry> ©);
Error_code insert(long position, const List_entry &x);
Error_code clear(Node<List_entry> * head);
Error_code remove(long position);
Error_code retrieve(long position, List_entry &item);
Error_code replace(List_entry &x,long position);
bool empty()const;
long size()const;
bool Write();
protected:
long count;
Node<List_entry> *head;
Node<List_entry> *set_position(long position)const;
private:
};
template<class List_entry>
List<List_entry>::List()
{
count = 0;
head = NULL;
}
template<class List_entry>
List<List_entry>::List(const List<List_entry> ©)
{
Node *new_copy, *copy_node = copy.head;
count = copy.count;
if(copy_node == NULL) head = NULL;
else
{
head = new_copy = new Node<Node_entry>(copy_node->entry);
while(copy_node->next != NULL)
{
copy_node = copy_node->next;
new_copy->next = new Node<Node_entry>(copy_node->entry);
new_copy = new_copy->next;
}
}
}
template<class List_entry>
List<List_entry>::~List()
{
while(!empty())
remove(0);
}
template<class List_entry>
void List<List_entry>::operator =(const List<List_entry> ©)
{
Node *new_head, *new_copy, *copy_node = copy.head;
count = copy.count;
if(copy_node == NULL) new_head = NULL;
else
{
new_copy = new_head = new Node(copy_node->entry);
while(copy_node->next != NULL)
{
copy_node = copy_node->next;
new_copy->next = new Node(copy_node->entry);
new_copy = new_copy->next;
}
}
while(!empty())
remove(0);
head = new_head;
}
template<class List_entry>
Error_code List<List_entry>::insert(long position, const List_entry &x)
{
if(position < 0 || position > count)
return range_error;
Node<List_entry> * new_node, *previous, *following;
if(position > 0)
{
previous = set_position(position - 1);
following = previous->next;
}
else
following = head;
new_node = new Node<List_entry>(x,following);
if(new_node == NULL)
return overflow;
if(position == 0)
head = new_node;
else
previous->next = new_node;
count ++;
return success;
}
template<class List_entry>
Node<List_entry> * List<List_entry>::set_position(long position)const
{
Node<List_entry> *p = head;
for(int i = 0; i < position; i ++)
p = p->next;
return p;
}
template<class List_entry>
Error_code List<List_entry>::clear(Node<List_entry> * head)
{
while(!empty())
remove(0);
return success;
}
template<class List_entry>
Error_code List<List_entry>::remove(long position)
{
if(position < 0 || position >= count)
return range_error;
Node<List_entry> *p, *current;
if(position == 0)
{
p = head;
head = head->next;
}
else
{
current = set_position(position - 1);
p = current->next;
current->next = p->next;
}
delete(p);
count --;
return success;
}

template<class List_entry>
Error_code List<List_entry>::replace(List_entry &x,long position)
{
Node<List_entry> *current = NULL;
if(position < 0 || position >= count)
return range_error;
current = set_position(position);
current->entry = x;
return success;
}
template<class List_entry>
Error_code List<List_entry>::retrieve(long position,List_entry &item)
{
Node<List_entry> *current = NULL;
if(position < 0 || position >= count)
return range_error;
current = set_position(position);
item = current->entry;
return success;
}
template<class List_entry>
long List<List_entry>::size()const
{
return count;
}
template<class List_entry>
bool List<List_entry>::empty()const
{
return (count == 0);
}
template<class List_entry>
bool List<List_entry>::Write()
{
Node<List_entry> *p = head;
while(p != NULL)
{
cout << p->entry << " ";
p = p->next;
}
cout << endl;
return true;
}
int main()
{
List<long> test;
Node<long> p;
long i, item = 0;
for(i = 0; i < 10; i ++)
{
scanf("%ld",&p.entry);
test.insert(0,p.entry);
}
test.Write();
for(i = 0; i < 10; i ++)
{
test.retrieve(i,item);
cout << "item" << i << " " << item << endl;
}
test.Write();
for(i = 0; i < 10; i += 2)
{
test.remove(0);
}
test.Write();
return 0;
}