明星斗转星移下载:C语言中关于单链表的简单问题

来源:百度文库 编辑:高校问答 时间:2024/04/29 12:59:48
书上都是些很抽象的数据结构例子,没有一个能运行的实际例子!就请大家帮个小忙给写个简单的完整的程序:单链表的创建.
非常感谢!
一定要能运行出来的哦,做为参考,使自己天天向上.

#include <stdio.h>
#include <alloc.h> /*Tc2.0为#include <alloc.h>*/
/*Tc3.0c以上为 #include <malloc.h> */
#define LEN sizeof(struct student)
#define NULL 0

struct student /*定义结构体*/
{ int data;
struct student* next;
}

struct student* creatlist()
/*单指针建立带头结点的单链表*/
{
struct student* head;/*声明头指针*/
struct student* p;
head=(struct student*)malloc(LEN);
head->next=NULL;
p=(struct student*)malloc(LEN);
printf("input the num:\n");
scanf("%d",&p->data);
while(p->data){
p->next=head->next;
head->next=p;
p=(struct student*)malloc(LEN);
scanf("%d",&p->data);
}
free(P);
return head;
}

void printlist(struct student*head)
{ struct student* p;
p=head->next;
while(p){
printf("%d",p->data);
p=p->next;
}
}
main()
{ struct student* head;
head=creatlist();
printlist(head);
}

#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;
}
数据结构作业随便写的哈,不晓得要得不?