给定关键字序列49.38.65.97.76.13.27.49,建立一个有序单链表并输出

来源:百度知道 编辑:UC知道 时间:2024/05/24 18:00:27

#include<iostream>
using namespace std;
#define null 0
struct Node{
int data;
Node *next;
};
void main(){
int array[8]={49,38,65,97,76,13,27,49};
struct Node *head=null,*p,*t;
for(int i=7;i>=0;i--)
for(int k=0;k<i;k++)
if(array[k]<array[k+1]){
int temp;
temp=array[k];array[k]=array[k+1];array[k+1]=temp;
}
for(i=0;i<8;i++){
p=new Node;
p->data =array[i];
if(head==null) t=head=p;
else{
t->next=p;
t=p;
}
t->next =null;
}
for(p=head;p->next!=null;p=p->next)
cout<<p->data<<" ";
cout<<endl;
return;
}