c++将用户输入的每个数按从小到大顺序放入有序链表,并输出显示,用户输入-1表示输入结束。
2条回答 默认 最新
关注
#include<iostream> using namespace std; #include <stdio.h> #include <stdlib.h> struct node{ int n; struct node *pNext; }; int main(){ struct node *pHead = NULL, *pEnd = NULL, *pNode = NULL; int i = 1; printf("Please input a integer:\n"); printf("end by inputing 0:"); do{ scanf("%d",&i); if(i != 0){ pNode = (struct node *)malloc(sizeof(struct node)); if(pNode != NULL){ pNode -> n = i; pNode -> pNext = NULL; if(pHead == NULL){ pHead = pNode; pEnd = pNode; } else{ pEnd -> pNext = pNode; pEnd = pNode; }//end of if(pHead == NULL) }//end of if(pNode != NULL) }//end of if(i == 0) }while(i != -1); pNode = pHead; while(pNode != NULL){ printf("%d\t", pNode -> n); pHead = pNode; pNode = pNode -> pNext; free(pHead); } printf("\n"); }评论 打赏 举报解决 1无用