美文网首页
C语言实现一个动态链表

C语言实现一个动态链表

作者: 长安猎人 | 来源:发表于2018-10-26 00:35 被阅读0次

    代码:

    #include <stdio.h>
    #include <malloc.h>
    struct weapon {
      int price;
      int atk;
      struct weapon * next;
    };
    struct weapon * create() {
      struct weapon * head;
      struct weapon * p1, * p2;
      int n = 0;//记录节点个数
      p1 = p2 = (struct weapon*)malloc(sizeof(struct weapon));
      scanf("%d,%d", &p1->price, &p1->atk);
      head = NULL;
      while(p1->price != 0) {
        n++;
        if(n == 1) {
          head = p1;
        } else {
          p2->next = p1;
        }
    
        p2 = p1;
        p1 = (struct weapon*) malloc(sizeof(struct weapon));
        scanf("%d,%d", &p1->price, &p1->atk);
      }
      p2->next = NULL;
      return (head);
    }
    int main() {
      struct weapon *p;
      p = create();
      printf("%d,%d", p->price, p->atk);
      return 0;
    }
    

    相关文章

      网友评论

          本文标题:C语言实现一个动态链表

          本文链接:https://www.haomeiwen.com/subject/xtbhtqtx.html