链表学了两星期了,像这种题还是做起来还是好费劲,真是~~~
自学那么难吗?我不就快了一点,就看不懂了,心里很是失落!
好在今天女神来找我了,顿时其他的就无所谓了!!!
上个代码吧,等我真正理解的时候还是会再把它拿过来AC!
#include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data; struct node *next; }; int main() { int n,m,i,j; while(scanf("%d%d",&n,&m)!=EOF&&(n||m)) { node *p1,*p2,*root; root=(struct node*)malloc(sizeof(node)); root->next=NULL; p1=root; for(i=1;i<=n;i++) { scanf("%d",&p1->data); node *temp=(node *)malloc(sizeof(node)); temp->next=NULL; p1->next=temp; p1=temp; } p1=root; while(p1->next!=NULL) { if(p1->next->data>=m) { node *temp=(node *)malloc(sizeof(node)); temp->data=m; temp->next=p1->next; p1->next=temp; break; } p1=p1->next; } p1=root; while(p1->next!=NULL) { if(p1==root) printf("%d",p1->data); else printf(" %d",p1->data); p1=p1->next; } printf("\n"); } return 0; }
接着上面的博客,直到今天,链表才算入门,,,,,,,
自己写了代码,1A,,,,,这话我的代码。。。。。。。
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct Lnode { int data; struct Lnode *next; }Lnode; int Listinsert(Lnode **L,int n); void CreatList(int n,Lnode **head); int main() { int i,j,n,m; Lnode *p,*q; while(scanf("%d%d",&n,&m)!=EOF&&n||m) { CreatList(n,&p); Listinsert(&p,m); p=p->next; for(i=0; i < n; i++) { printf("%d ",p->data); p=p->next; } printf("%d\n",p->data); } return 0; } void CreatList(int n,Lnode **head) { Lnode *p,*q; *head=p=(struct Lnode *)malloc(sizeof(struct Lnode)); p->next=NULL; for(int i=n; i>0; i--) { q=(struct Lnode *)malloc(sizeof(struct Lnode)); scanf("%d",&q->data); q->next = p->next;// p->next=q; p=q;//printf("%d**",q->data); } // return (*head); } int Listinsert(Lnode **L , int n) { Lnode *p,*q,*r; p=*L; r=*L; r=r->next; int j=0; while(r->next!=NULL) { if(r->data >= n) { break; } else { j++; r=r->next; p=p->next; } } q=(Lnode *)malloc(sizeof(Lnode)); q -> data = n; q->next = p->next; p->next = q; return 1; }
其中参数传递问题困扰我好多许久许久,,,,,要用双指针,指向指针的指针才行,AC之路实为不易!!!