邓肯视频集锦:c数据结构的问题,高手帮忙

来源:百度文库 编辑:高考问答 时间:2024/04/30 02:42:04
谁有链表排序的号算法阿?

// 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;
}

o分的问题有时候多没有人看!你把分加高点也许有人帮忙!