12-15链表学习

一.动态分配内存realloc

  void*//返回重新分配的内存空间的首地址 realloc(void*//void*指针指向的内存区域必须是使用malloc分配过的,size_t//现在总共需要多少内存空间);

二.链表:单链表,双链表

结点node

           Data              Data

node->next指针       next指针

在定义结构体时,要明确内存空间。

三.保存一组年龄的数据。

#include <stdio.h>

#include <stdlib.h>

 

typedef struct student{

    int age;

    struct student *next;

}Student;

 

int main(int argc, const char * argv[]) {

    Student *pHead = NULL;

    Student *pTail = NULL;//创建一个结点

    

    for (int i = 0; i < 4; i ++) {

         Student *Ptemp = (Student *)malloc(1 * sizeof(Student));

        if (Ptemp == NULL) {

            exit(EXIT_FAILURE);

        }

        

        printf("请输入年龄:");

        scanf("%d",&(Ptemp->age));

        

        Ptemp->next = NULL;

        

        if (pHead == NULL) {

            pHead = Ptemp;

            pTail = Ptemp;

            

        }

        else {

            pTail->next = Ptemp;

            pTail = Ptemp;

        }

    }

    Student *pTemp = pHead;

    while (pTemp != NULL) {

        printf("%d ",pTemp->age);

        pTemp = pTemp->next;

    }

    printf("\n");

    return 0;

}

 

posted @ 2015-12-15 20:22  liuzhicen  阅读(116)  评论(0编辑  收藏  举报