标签:des style blog http color os java io strong
Time Limit: 1000 MS Memory Limit: 32768 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
3 3 1 2 4 0 0
1 2 3 4
#include<stdio.h> #include<stdlib.h> struct node { int num; node *next; }; int main() { int n,m,i; while(scanf("%d%d",&n,&m)!=EOF&&(m!=0||n!=0)) { node * root=(node *)malloc(sizeof(node)); //定义一个头指针 root root->next=NULL; node *p=root; //定义用于连接的指针p for(i=1;i<=n;i++) //依次开辟新空间,存入数据,并且一节一节的连接 { scanf("%d",&p->num); node *temp=(node *)malloc(sizeof(node)); temp->next=NULL; p->next=temp; p=temp; } p=root; while(p->next!=NULL) //这里开始插入数字。 { if(p->next->num>=m) { node *temp=(node *)malloc(sizeof(node)); //为新数字开辟内存。并连接。 temp->num=m; temp->next=p->next; p->next=temp; break; } p=p->next; } p=root; while(p->next!=NULL) //输出链表的数据。最后一节的next指针为空。用来结束。 { if(p==root) printf("%d",p->num); else printf(" %d",p->num); p=p->next; } printf("\n"); } return 0; } /*~~~~~~~~~~~~~~~~~~~还不太会链表 不知道自己哪里错了 过几天看~~~~~~~~~~~~~~~~~~~~~~~~~~*/ #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; struct node { node *next; int num; }; int main() { int n,m; while(scanf("%d%d",&n,&m),n,m) { node *root=(node *)malloc(sizeof(node)); ///定义一个头指针 名为root 给node分配一块空间 node *p=root; ///表头指针 定义一个链接的指针p 指向分配的空间的开始部位 root->next=NULL; ///初始化吧~~~~~~~~~~~~ for(int i=0; i<n; i++) { scanf("%d",&p->num); node *temp=(node *)malloc(sizeof(node)); ///给temp分配一块空间 temp->next=NULL; p->next=temp; p=temp; } p=root; while(p->next!=NULL) { if(p->next->num>m) { node *temp=(node *)malloc(sizeof(node)); ///为新数字开辟内存并链接 temp->num=m; temp->next=p->next; break; } p=p->next; } p=root; while(p->next!=NULL) ///输出链表的数据。最后一节的next指针为空。用来结束。 { if(p==root) printf("%d",p->num); else printf(" %d",p->num); p=p->next; } printf("\n"); } return 0; }
标签:des style blog http color os java io strong
原文地址:http://www.cnblogs.com/zhangying/p/3942234.html