- 论坛徽章:
- 0
|
:oops: :oops: 链表!创建的问题!
- # include <stdio.h>;
- # include <stdlib.h>;
- # include <string.h>;
- typedef struct st {
- int a;
- struct st *next;
- } ST;
- int main (int argc, char *argv[] )
- {
- int n;
- struct st *p,*q,*head;
- head=p=q=NULL;
- printf ( "Input no:" );
- scanf ("%d",&n);
- // clrscr();
- while (n!=0)
- {
- p=(struct st*)malloc(sizeof(struct st));
- memset ( p, 0, sizeof(ST) );
- p->;a=n;
- p->;next = NULL;
- if (head==NULL) head = q = p;//NOT : p = head;
- else // q->;p->;
- {
- q->;next = p;
- q=p;
- }
-
- //p->;next=NULL; WRONG
- printf ( "Input no:" );
- scanf ("%d",&n);
- }
- ST *tmp = NULL;
- p=head;
- while (p!=NULL) { // 空间申请了别忘了释放
- printf ("%2d",p->;a);
- tmp = p;
- p=p->;next;
- free(tmp);
- }
- return 0;
- }
复制代码 |
|