哪位高手能帮我 编写一个逐个输出单链表中所有数据元素的成员函数?急求!
我只要逐个输出单链表中所有数据元素的成员函数的程序就可以啊?这么长不知道哪个是啊?谢谢!!...
我只要逐个输出单链表中所有数据元素的成员函数的 程序就可以啊? 这么长不知道 哪个是啊?谢谢!!
展开
1个回答
展开全部
这个程序主要是单链表的操作!
#include<iostream>
#include<iomanip>
using namespace std;
struct node{
double data;
struct node *next;
};
struct node *create();
struct node *rescreate();
void printlist(struct node *head);
int len(struct node *head);
void insertnode(struct node *head,int i,double x) ;
void delnode(struct node *head,double x);
void delnode2(struct node *head,int i);
void sort(struct node *head);
int main()
{
struct node *p;
p=rescreate();
printlist(p);
//cout<<endl<<len(p)<<endl;
insertnode(p,3,-100);
cout<<endl;
printlist(p);
delnode2(p,4);
cout<<endl;
printlist(p);
sort(p);
cout<<endl<<"After sort\n";
printlist(p);
system("pause");
return 0;
}
struct node *create()
{
struct node *head,*oldp,*p,*s;
double newnum;
head=new struct node;
oldp=head;
cin>>newnum;
while(newnum!=-32768){
p=new struct node;
p->data=newnum;
oldp->next=p;
oldp=p;
cin>>newnum;
}
s=head;
head=head->next;
delete s;
p->next=head;
return head;
}
struct node *rescreate()
{
struct node *tail,*oldp,*p;
double newnum;
tail=NULL;
oldp=tail;
cin>>newnum;
while(newnum!=-32768){
p=new struct node;
p->data=newnum;
p->next=oldp;
oldp=p;
cin>>newnum;
}
return p;
}
void printlist(struct node *head)
{
while(head){
cout<<setw(5)<<head->data;
head=head->next;
}
}
int len(struct node *head)
{
int n=0;
while(head){
n++;
head=head->next;
}
return n;
}
//在单链表第 i(0=<i<=len(head)) 个结点之后插入一个新结点,其值为 x
void insertnode(struct node *head,int i,double x)
{
struct node *p,*s;
int j;
s=new struct node;
s->data=x;
if(i<0 || i>len(head))
cout<<"插入位置错误\n";
if(i==0){
s->next=head;
head=s;
}
else{
p=head;
j=1;
while(p && j<i){
j++;
p=p->next;
}
if(p){
s->next=p->next;
p->next=s;
}
}
}
//删除单链表中一个 值为 x 的结点
void delnode(struct node *head,double x)
{
struct node *p,*s;
if(head->data==x){
p=head;
head=head->next;
delete p;
}
else{
s=head;
p=head->next;
while(p && p->data!=x){
s=p;
p=p->next;
}
if(p){
s->next=p->next;
delete p;
}
else
cout<<"找不到\n";
}
}
//删除单链表中 第 i 的结点
void delnode2(struct node *head,int i)
{
struct node *p,*s;
int j;
if(i==1){
p=head;
head=head->next;
delete p;
}
else{
s=head;
p=head->next;
j=2;
while(p && j<i){
j++;
s=p;
p=p->next;
}
if(p){
s->next=p->next;
delete p;
}
else
cout<<"找不到\n";
}
}
//单链表排序---起泡
void sort(struct node *head)
{
int i;
struct node *p,*s1,*s2;
double temp;
int n;
n=len(head);
p=head;
for(i=1;i<n;i++){
s1=p;
s2=p->next;
while(s2){
if(s1->data>s2->data){
temp=s1->data;
s1->data=s2->data;
s2->data=temp;
}
s1=s1->next;
s2=s2->next;
}
}
}
还有一个主要是单链表的建立,插入,排序等!
#include<iostream>
#include<iomanip>
using namespace std;
struct node{
float data;
node *next;
};
//正序建立单链表,数据依次插入链表头之后
node * create()
{
node *head,*p,*s;
float newnum;
head=new node;;
p=head;
cin>>newnum;
while(newnum>=0){
s=new node;
s->data=newnum;
p->next=s;
p=s;
cin>>newnum;
}
head=head->next; //删除头结点
p->next=NULL; //若为p->next=head; 则为循环单链表
return(head);
}
//逆序建立单链表,数据依次插入链表尾之前
node * reversecreate()
{
node *tail,*oldptr,*ptr;
float newnum;
ptr=NULL;
tail=ptr;
cin>>newnum;
while(newnum>=0){
oldptr=ptr;
ptr=new node;
ptr->data=newnum;
ptr->next=oldptr;
cin>>newnum;
}
return(ptr);
}
//打印链表
void printlist(node *listhead)
{
while(listhead!=NULL){
cout<<setw(5)<<listhead->data;
listhead=listhead->next;
}
}
//查找某个结点
void find(node *head,float x)
{
node *p;
p=head;
while(p->data!=x && p!=NULL)
p=p->next;
if(p!=NULL)
cout<<"找到了!\n";
else
cout<<"未找到!\n";
}
//求单链表的长度(结点个数)
int length(node *head)
{
int n=0;
node *p;
p=head;
while(p!=NULL){
p=p->next;
n++;
}
return n;
}
//在单链表中插入一个结点,其值为x,插入第i(i>=0)个结点之后
void insertnode(node *head,int i,float x)
{
node *s,*p;
int j;
s=new node;
s->data=x;
if(i==0){
s->next=head;
head=s;
}
else{
p=head;
j=1; //查找第i个结点,由p指向
while(p!=NULL && j<i){
j++;
p=p->next;
}
if(p!=NULL){
s->next=p->next;
p->next=s;
}
else
cout<<"未找到!\n";
}
}
//在单链表中删除一个结点,其值为x
void deletenode(node *head,float x)
{
node *p,*s;
if(head==NULL)
cout<<"链表下溢!\n";
if(head->data==x){
p=head;
head=head->next;
delete p;
}
else{
s=head;
p=head->next;
while(p!=NULL && p->data!=x)
if(p->data!=x){
s=p;
p=p->next;
}
if(p!=NULL){
s->next=p->next;
delete(p);
}
else cout<<"未找到!\n";
}
}
int main( )
{
node *linktable;
cout<<"Input positive values, ends with a negative one\n";
cout<<"The list of input values:\n";
printlist(create());
printlist(reversecreate());
cout<<length(create());
linktable=create();
insertnode(linktable,2,100);
printlist(linktable);
cout<<endl<<length(linktable)<<endl;
deletenode(linktable,3);
printlist(linktable);
cout<<endl<<length(linktable)<<endl;
cin.get();
cin.get();
return 0;
}
#include<iostream>
#include<iomanip>
using namespace std;
struct node{
double data;
struct node *next;
};
struct node *create();
struct node *rescreate();
void printlist(struct node *head);
int len(struct node *head);
void insertnode(struct node *head,int i,double x) ;
void delnode(struct node *head,double x);
void delnode2(struct node *head,int i);
void sort(struct node *head);
int main()
{
struct node *p;
p=rescreate();
printlist(p);
//cout<<endl<<len(p)<<endl;
insertnode(p,3,-100);
cout<<endl;
printlist(p);
delnode2(p,4);
cout<<endl;
printlist(p);
sort(p);
cout<<endl<<"After sort\n";
printlist(p);
system("pause");
return 0;
}
struct node *create()
{
struct node *head,*oldp,*p,*s;
double newnum;
head=new struct node;
oldp=head;
cin>>newnum;
while(newnum!=-32768){
p=new struct node;
p->data=newnum;
oldp->next=p;
oldp=p;
cin>>newnum;
}
s=head;
head=head->next;
delete s;
p->next=head;
return head;
}
struct node *rescreate()
{
struct node *tail,*oldp,*p;
double newnum;
tail=NULL;
oldp=tail;
cin>>newnum;
while(newnum!=-32768){
p=new struct node;
p->data=newnum;
p->next=oldp;
oldp=p;
cin>>newnum;
}
return p;
}
void printlist(struct node *head)
{
while(head){
cout<<setw(5)<<head->data;
head=head->next;
}
}
int len(struct node *head)
{
int n=0;
while(head){
n++;
head=head->next;
}
return n;
}
//在单链表第 i(0=<i<=len(head)) 个结点之后插入一个新结点,其值为 x
void insertnode(struct node *head,int i,double x)
{
struct node *p,*s;
int j;
s=new struct node;
s->data=x;
if(i<0 || i>len(head))
cout<<"插入位置错误\n";
if(i==0){
s->next=head;
head=s;
}
else{
p=head;
j=1;
while(p && j<i){
j++;
p=p->next;
}
if(p){
s->next=p->next;
p->next=s;
}
}
}
//删除单链表中一个 值为 x 的结点
void delnode(struct node *head,double x)
{
struct node *p,*s;
if(head->data==x){
p=head;
head=head->next;
delete p;
}
else{
s=head;
p=head->next;
while(p && p->data!=x){
s=p;
p=p->next;
}
if(p){
s->next=p->next;
delete p;
}
else
cout<<"找不到\n";
}
}
//删除单链表中 第 i 的结点
void delnode2(struct node *head,int i)
{
struct node *p,*s;
int j;
if(i==1){
p=head;
head=head->next;
delete p;
}
else{
s=head;
p=head->next;
j=2;
while(p && j<i){
j++;
s=p;
p=p->next;
}
if(p){
s->next=p->next;
delete p;
}
else
cout<<"找不到\n";
}
}
//单链表排序---起泡
void sort(struct node *head)
{
int i;
struct node *p,*s1,*s2;
double temp;
int n;
n=len(head);
p=head;
for(i=1;i<n;i++){
s1=p;
s2=p->next;
while(s2){
if(s1->data>s2->data){
temp=s1->data;
s1->data=s2->data;
s2->data=temp;
}
s1=s1->next;
s2=s2->next;
}
}
}
还有一个主要是单链表的建立,插入,排序等!
#include<iostream>
#include<iomanip>
using namespace std;
struct node{
float data;
node *next;
};
//正序建立单链表,数据依次插入链表头之后
node * create()
{
node *head,*p,*s;
float newnum;
head=new node;;
p=head;
cin>>newnum;
while(newnum>=0){
s=new node;
s->data=newnum;
p->next=s;
p=s;
cin>>newnum;
}
head=head->next; //删除头结点
p->next=NULL; //若为p->next=head; 则为循环单链表
return(head);
}
//逆序建立单链表,数据依次插入链表尾之前
node * reversecreate()
{
node *tail,*oldptr,*ptr;
float newnum;
ptr=NULL;
tail=ptr;
cin>>newnum;
while(newnum>=0){
oldptr=ptr;
ptr=new node;
ptr->data=newnum;
ptr->next=oldptr;
cin>>newnum;
}
return(ptr);
}
//打印链表
void printlist(node *listhead)
{
while(listhead!=NULL){
cout<<setw(5)<<listhead->data;
listhead=listhead->next;
}
}
//查找某个结点
void find(node *head,float x)
{
node *p;
p=head;
while(p->data!=x && p!=NULL)
p=p->next;
if(p!=NULL)
cout<<"找到了!\n";
else
cout<<"未找到!\n";
}
//求单链表的长度(结点个数)
int length(node *head)
{
int n=0;
node *p;
p=head;
while(p!=NULL){
p=p->next;
n++;
}
return n;
}
//在单链表中插入一个结点,其值为x,插入第i(i>=0)个结点之后
void insertnode(node *head,int i,float x)
{
node *s,*p;
int j;
s=new node;
s->data=x;
if(i==0){
s->next=head;
head=s;
}
else{
p=head;
j=1; //查找第i个结点,由p指向
while(p!=NULL && j<i){
j++;
p=p->next;
}
if(p!=NULL){
s->next=p->next;
p->next=s;
}
else
cout<<"未找到!\n";
}
}
//在单链表中删除一个结点,其值为x
void deletenode(node *head,float x)
{
node *p,*s;
if(head==NULL)
cout<<"链表下溢!\n";
if(head->data==x){
p=head;
head=head->next;
delete p;
}
else{
s=head;
p=head->next;
while(p!=NULL && p->data!=x)
if(p->data!=x){
s=p;
p=p->next;
}
if(p!=NULL){
s->next=p->next;
delete(p);
}
else cout<<"未找到!\n";
}
}
int main( )
{
node *linktable;
cout<<"Input positive values, ends with a negative one\n";
cout<<"The list of input values:\n";
printlist(create());
printlist(reversecreate());
cout<<length(create());
linktable=create();
insertnode(linktable,2,100);
printlist(linktable);
cout<<endl<<length(linktable)<<endl;
deletenode(linktable,3);
printlist(linktable);
cout<<endl<<length(linktable)<<endl;
cin.get();
cin.get();
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询