#include<stdlib.h>
#define null 0

struct Node
{
    
int value;
    
struct Node *next;
};


int main(int argc, char* argv[])
{
    Node 
*p, *q, *r;
    
int temp;
    Node 
*h;
    
/* 定义头节点 */
    h 
= (struct Node*)malloc(sizeof(struct Node));
    h
->value = -1;
    h
->next = null;

    
while(1)
    {
        scanf(
"%d"&temp);
        
if(temp < 0)
            
break;
        
/* 为新输入的数据定义节点 */
        p 
= (struct Node*)malloc(sizeof(struct Node));
        p
->value = temp;
        
/* 寻找插入位置 */
        q 
= h; 
        r 
= h;
        
while(q != null && q->value < p->value)
        {
            r 
= q;
            q 
= q->next;
        }
        
/* 插入 */
        r
->next = p;
        p
->next = q;
    }
    
/* 输出链表中的数据, 并释放申请的节点空间 */
    q 
= h->next;
    free(h);
    
while(q)
    {
        printf(
"%5d", q->value);
        r 
= q;
        q 
= q->next;
        free(r);
    }

    
return 0;
}

相关文章:

  • 2022-01-23
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
  • 2021-07-20
  • 2021-11-28
  • 2022-12-23
猜你喜欢
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案