简单的无头节点单链表

来源:互联网 发布:ios 判断app网络权限 编辑:程序博客网 时间:2024/04/30 03:07
#include <stdio.h>#include <stdlib.h>typedef struct student{    int num;    struct student *next;}student;#define NODE_SIZE sizeof(struct student)/**************************************************** *  函数功能:创建链表 *  函数参数:无 *  函数输出:头指针*****************************************************/static struct student* create_list(){    struct student* head=NULL;  /* 指向第一个节点的头指针 */    struct student* p1=NULL;    /* 用于新建的节点 */    struct student* p2=NULL;    /* 用于指向最后一个节点 */    p1 = (struct student*)malloc(NODE_SIZE);    /* 开始时都指向新申请的第一个节点 */    head = p2 = p1;    scanf("%d", &p1->num); /* 对新节点数据域进行赋值 */    /* 如果输入不为0,则新建节点 */    while( p1->num != 0)    {        p1 = (struct student*)malloc(NODE_SIZE);        scanf("%d", &p1->num);        p2->next = p1;        p2 = p1;    }    p2->next = NULL;    return head;}/**************************************************** *  函数功能:遍历链表中每一个数据域 *  函数参数:head头指针 *  函数输出:无*****************************************************/void show_list(struct student* head){    struct student* p = head;    while (p != NULL)    {        printf("%d\n", p->num);        p = p ->next;    }}/**************************************************** *  函数功能:计算链表的元素个数 *  函数参数:head头指针 *  函数输出:元数个数*****************************************************/int how_maney(struct student** head){    struct student* p = *head;    int iCount = 0;    while (p!=NULL)    {        p = p->next;        ++iCount;    }    return iCount;}/**************************************************** *  函数功能:指定位置插入节点 *  函数参数:head头指针,index插入位置,num插入节点的值 *  函数输出:指定位置节点的值*****************************************************/static int insert_list(struct student** head, int index, int num){    int iCount = how_maney(head);    /*  新申请的待插入节点 */    struct student* p = (struct student*)malloc(sizeof(struct student));    p->num = num;    /* index == 1,则在第一个节点前插入 */    if (index == 1)    {        p->next = *head;        *head = p;        return (*head)->num;    }    else if (index > iCount)/* 说明在链表尾部插入节点 */    {        struct student* p1 = *head;        while (p1->next != NULL)        {            p1 = p1->next;        }        p1->next = p;        p->next = NULL;        return iCount;    }    else    /* 在中间位置插入新节点 */    {        struct student* p1 = *head;        int i = 1;        while (p1!=NULL && i < index-1)        {            p1 = p1->next;            ++i;        }        p->next = p1->next;        p1->next = p;        return i;    }    return -1;}/**************************************************** *  函数功能:删除指定位置节点 *  函数参数:head头指针,index插入位置,num插入节点的值 *  函数输出:指定位置节点的值*****************************************************/static void delete_list(struct student** head, int index){    struct student* p = *head;    struct student* p1 = NULL;    int iCount = how_maney(head);    /* 首先判断删除是否为头结点 */    if(index == 1)      {           printf("删除第一个节点\n");        *head = (*head)->next;        free(p);        return ;    }    else if (index >= iCount ) /* 如果删除的是尾部节点 */    {        int i = 1;        printf("删除尾部节点\n");        /* 找到要删除的前一个节点 */        while(p != NULL && i < iCount-1)        {            p = p->next;            i++;        }        /* 释放节点空间 */        free(p->next);        p->next = NULL;         return ;    }    else /* 删除的是中间节点 */    {        int i = 1;        printf("删除中间节点\n");        while (p!= NULL && i < index-1)        {            p = p->next;            ++i;        }        p1 = p->next;        p->next = p1->next;        free(p1);        return ;    }    return ;}int main(){    struct student* head = NULL;    struct student* node;    head = create_list(node);    printf("bbbb\n");    show_list(head);    printf("aaaa\n");    printf("链表元素个数:%d\n", how_maney(&head));    /*insert_list(&head, 1, 100);    show_list(head);    printf("链表元素个数:%d\n", how_maney(&head));    delete_list(&head, 3);    show_list(head);    printf("链表元素个数:%d\n", how_maney(&head));    */    return 0;}
0 0
原创粉丝点击