求解:
【問題描述】輸入一整數序列,采用單鏈表進行存盤,按照非遞減的方式輸出該序列的所有值,每輸出一個值,洗掉其對應的結點,同時輸出鏈表的長度。
【輸入形式】整數序列,以空格作為分隔,序列最多包含10個整數
【輸出形式】整數序列,以空格作為分隔,
【樣例輸入】
10 90 80 20 40
【樣例輸出】
10 4 20 3 40 2 80 1 90 0
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *creat(struct node *head);
struct node *find (struct node *head);
int main(){
int n=0;
struct node *head=NULL,*p=head->next;
head=creat(head);
while(p!=NULL){
n++;
p=p->next;}
for(int i=n;i>=0;i++){
head=find(head);
cout<<i<<" ";}
system ("pause");
return 0;
}
struct node *creat(struct node *head){
head=new node;
head->next=NULL;
struct node *p1,*p2=head;
while(1){
p1=new node;
cin>>p1->data;
p2->next=p1;
p2=p1;
char c=getchar();
if(c=='\n'){break;}}
p2->next=NULL;
free(p1);
return head;}
struct node *find (struct node *head){
int min;
struct node *p=head->next;
struct node *p1,*p2;
min=p->data;
while(p!=NULL){
if(min>p->data){
min=p->data;}}
p1=head->next;
p2=head;
while(min!=p->data){
p2=p1;
p1=p1->next;}
if(min==p->data){
cout<<p1->data<<" ";
p2->next=p1->next;
free(p1);}
return head;}

uj5u.com熱心網友回復:
//檢測下 creat函式
struct node *creat(struct node *head){
head=new node;
head->next=NULL;
struct node *p1=0,*p2=head; //xx
while(1){
cin>>p2->data;//xx
char c=getchar();
if(c=='\n')
{break;}
p1=new node;//xx
p2->next=p1;//xx
p2=p1;//xx
}
p2->next=NULL;
//free(p1);//xx
return head;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/86014.html
標籤:C++ 語言
上一篇:誰幫我解釋一下這個段代碼做什么
下一篇:大佬,這個程式電腦怎么執行不了
