单链表的建立

来源:互联网 发布:oracle灾备数据库 编辑:程序博客网 时间:2024/05/17 01:21
 

/*******************************/
/*tn10000@126.com/             */
/*******************************/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct node
{
 int num;
 struct node* next;
};

struct node * create(struct node *head);
void print(struct node *head);

int main(){
 struct node *head;
 head = NULL;
 head = create(head);
 print(head);
 return 0;
}

struct node *create(struct node *head){
 int i=0;
 struct node *p1, *p2;

 p1 = p2 = (struct node*)malloc(sizeof(struct node));
 memset(p1, 0, sizeof(struct node));
 scanf("%d", &p1->num);
 p1->next=NULL;
 while(p1->next>0){
  if (head==NULL){
   head=p1;
  }else{
   p2->next=p1;
  }

  p2=p1;
  p1=(struct node*)malloc(sizeof(struct node));
  memset(p1, 0, sizeof(struct node));
  scanf("%d", &p1->num);
  p1->next=NULL;
  i++;
 }

 printf("\n total:%d\n", i);
 return head;
}


void print(struct node *head){
 struct node *temp;
 temp = head;
 while(temp != NULL){
  printf("%6d\n", temp->num);
  temp=temp->next;
 }
}

原创粉丝点击