找个例子
数据结构里的伪代码不会用,希望大大帮我搞一个能运行的关于数据结构的完整的程序,最好简单点的,要一个单链的插入程序就可以,谢谢了 问题点数:20、回复次数:3Top
1 楼zzw820626(偶要分,偶要星星)回复于 2006-03-12 10:31:58 得分 6
#include<iostream.h>
/*
实现C/C++函数:创建一个单链表和显示该单链表,单链表的每个结点有两个域:
信息域data和指针域link。
另设计函数main调用这两个函数完成单链表的建立和打印工作。
*/
//要加上申明
//每一行不要写两条以上的语句,不太容易看.
template <class T>
class SingleList;
template <class T>
struct Node
{
T data;
Node<T> *link;
Node(T& v,Node<T>* next=NULL):data(v),link(next){}
};
template <class T>
class SingleList
{
public:
SingleList()
{ first=NULL; length=0; } //构造函数
~SingleList();
bool IsEmpty() {return length==0;}
int Length() {return length;}
void Output(ostream& out)const ;
void push_front(T v);
private:
Node<T>* first;
int length;
};
template <class T>//要加这个
SingleList<T>::~SingleList()
{
Node<T> *p;
while ( first )
{
p=first->link;
delete first;
first=p;
}
}
template <class T>//要加这个
void SingleList<T>::push_front(T v)
{
Node<T>*new_node=new Node<T>(v);
new_node->link=first;
first=new_node;
length++;
}
template <class T>//要加这个
void SingleList<T>::Output(ostream& out)const//拼写错误
{
Node<T> *p=first;
while (p)
{
out<<p->data<<" ";
p=p->link;
}
}
int main()
{
SingleList<int> list;
for(int i=0;i<10;i++)
{
list.push_front(i);
}
list.Output(cout);
}Top
2 楼llf_hust()回复于 2006-03-12 11:26:39 得分 10
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct Lnode)
typedef struct Lnode{
int data;
struct Lnode *next;
}*Lnode1;
struct Lnode *Create()
{
Lnode1 head,tail,p;
int e;
head=(struct Lnode *)malloc(LEN);
tail=head;
scanf("%d", &e);(
while(e)
{
p=(struct Lnode *)malloc(sizeof(LEN));
p->data = e;
tail->next=p;
tail=p;
scanf("%d",&e);
}
tail->next=NULL;
return head;
}
void display(struct Lnode *head)
{
Lnode1 p;
p=head->next;
while(p != NULL)
{
printf(" %d ",p->data);
p = p->next;
}
printf("\n");
}
int length(struct Lnode *head)
{
int len=0;
Lnode1 p;
p=head->next;
while(p != NULL)
{
len++;
p=p->next;
}
return len;
}
float AveValue(struct Lnode *head)
{
int len;
float Ave_1=0.0,Sum=0.0;
Lnode1 p;
len=length(head);
p=head->next;
while(p != NULL)
{
Sum += p->data;
p = p->next;
}
Ave_1 = Sum /len ;
return Ave_1;
}
int MaxValue(struct Lnode *head)
{
int Max_1=0;
Lnode1 p;
p=head->next;
while(p != NULL)
{
if(p->data>Max_1)
Max_1=p->data;
p = p->next;
}
return Max_1;
}
int MinValue(struct Lnode *head)
{
int Min_1;
Lnode1 p;
p=head->next;
Min_1 = p->data;
while(p != NULL)
{
if(p->data<Min_1)
Min_1=p->data;
p = p->next;
}
return Min_1;
}
main()
{
Lnode1 head;
float Ave1;
int Max1,Min1;
// clrscr();
printf("please input data end of 0.\n");
head=Create();
display(head);
Min1=MinValue(head);
Max1=MaxValue(head);
printf("Min = %d, Max=%d\n ",Min1, Max1);
Ave1 = AveValue(head);
printf("Ave=%.2f",Ave1);
//getche();
}Top
3 楼ykzhujiang(朱朱)回复于 2006-03-13 02:52:20 得分 4
一楼的用了模版,估计楼主还不能理解
其实数据结构还是多看一些原理的比较好,只要你原理清楚了(包括结构的运作原理,实现的原理等),并且对某种语言的基础知识比较清楚,编程的问题就不大了
建议选择一本比较好的数据结构及算法书籍,这样帮助会比较大的Top




