关于C链表的实现

学习了数据结构后,自己学习写了一个链表的程序。
初步功能是实现了。但是不知道会不会有一些隐含的问题。所以希望大佬指导指导
/******************/
/*一个小的链表程序*/
/******************/
#include <stdio.h>
#include <stdlib.h>
//结点的结构体
typedef struct node
{
    char data;
    struct node*next;
}Node;
//链表的结构体
typedef struct
{
    Node *head;
    Node *list1;
    int num;
}NList;
//生成一个链表
void CreateList(NList *list)
{
    list->head = NULL;
    list->list1 = NULL;
    list->num = 0;
}
//向链表中添加数据
void AddList(NList *list)
{
    char data;
    scanf("%c",&data);
    while(data != '#')
    {
        if(list->head == NULL)
        {
            list->head = (Node *)malloc(sizeof(Node));
            list->head->data = data;
            list->head->next = NULL;
            list->list1 = list->head;
            list->num = 1;
        }
        else
        {
            Node *p = (Node *)malloc(sizeof(Node));
            list->list1->next = p;
            p->data = data;
            list->list1 = p;
            list->list1->next = NULL;
            list->num++;
        }
        scanf("%c",&data);
    }
}
//显示链表中的数据
void ShowList(NList list)
{
    Node *P1;
    P1=list.head;
    while(P1 != NULL)
    {
        printf("%c",P1->data);
        P1 = P1->next;
    }
}
//删除链表头的数据
void DelList_head(NList *list)
{
    if(list->head != NULL)
    {
        Node *p2 = list->head;
        list->head = p2->next;
        list->num--;
        free(p2);
    }
    else
    {
        printf("链表中不存在数据\n");
    }
}
int main()
{
    NList list2;
    CreateList(&list2);
    AddList(&list2);
    ShowList(list2);
    printf("\n链表中的数据个数是:%d个\n",list2.num);
    DelList_head(&list2);
    ShowList(list2);
    printf("\n链表中的数据个数是:%d个\n",list2.num);
    return 0;
}

 

程序的执行结果如下:

 

另一个更加优化的链表代码,注释思路更加清晰:

#include <stdio.h>
#include <stdlib.h>
//结点的结构
typedef struct Node
{
    int data;
    struct Node *link;
}node;
//创建一个头结点
node * create_list(int x)
{
    node *p = (node *)malloc(sizeof(node));
    p->data = x;
    p->link = NULL;
    return p;
}
//向链表中添加数据
void add_list(node * head,int x)
{
    if(!head)return;
    while(head->link)
        head = head->link;
    node *p = (node *)malloc(sizeof(node));
    p->data = x;
    p->link = NULL;
    head->link = p;
}
//在链表中查找数据
node * find_list(node * head,int x)
{
    while(head && head->data != x)
        head = head->link;
    if(head) return head;
    return NULL;
}
//打印链表中的数据
void show_list(node * head)
{
    while(head)
    {
        printf("%d->",head->data);
        head = head->link;
    }
    printf("null\n");
}
//反转链表
node * reverse(node * head)
{
    node * new = NULL,*old = head,*temp;
    while(old)
    {    
        temp = old->link;
        old->link = new;
        new = old;old = temp;
    }
    return new;
}
//删除链表头节点
node * del_list(node * head)
{
    if(!(head))return NULL;
    node *p = (head);
    (head) = (head)->link;
    free(p);
    return head;
}
//删除链表,需要修改head的指向,所以使用二级指针
void del_list_all(node **head)
{
    while((*head))
        (*head) = del_list((*head));
}
//测试程序
int main()
{
    node *ptr = create_list(1);
    add_list(ptr,2);
    add_list(ptr,3);
    add_list(ptr,4);
    show_list(ptr);
    if(find_list(ptr,5)) printf("查找到3的节点\n");
    else printf("未找到3的节点\n");
    if((ptr = del_list(ptr)) == NULL) printf("该链表为空链表,不可删除\n");
    show_list(ptr);
    del_list_all(&ptr);
    show_list(ptr);
    return 0;
}

 

posted on 2018-08-27 21:15  Air-Liu  阅读(2687)  评论(0编辑  收藏  举报

导航