学习了数据结构后,自己学习写了一个链表的程序。
初步功能是实现了。但是不知道会不会有一些隐含的问题。所以希望大佬指导指导
/******************/
/*一个小的链表程序*/
/******************/
#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 = ;
}
//向链表中添加数据
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 = ;
}
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 ;
}

程序的执行结果如下:

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

#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;
}

最新文章

  1. 【造轮子】打造一个简单的万能Excel读写工具
  2. 「理解HTTP」之常见的状态码segmentfault
  3. Java 基础【13】 文件(文件夹) 创建和删除
  4. T检验与F检验的区别_f检验和t检验的关系
  5. Yii2-Redis使用小记 - Cache(转)
  6. css3实现渐变的iPhone滑动解锁效果
  7. 使用Jaxb2进行xml与bean的转义时Date的format设置
  8. 【js】js方法中直接跳转到servlet
  9. Flex性能优化常用手法总结 转
  10. 使用一个小图片tile平铺到ImageView中或Activity背景
  11. UNIX网络编程--IPV4 IPV6 ICMPV4 ICMPV6
  12. Lucene简介(理论篇)
  13. Python3基础 使用 in notin 查询一个字符是否指定字典的键或者值
  14. kafka producer batch 发送消息
  15. Virtualbox 虚拟机安装Linux
  16. eclipse 等号左边代码补全
  17. shell脚本学习-变量
  18. Linux下安装uci
  19. Javascript面向
  20. ZooKeeper 集群的安装、配置---Dubbo 注册中心

热门文章

  1. 一句DOS命令搞定文件合并
  2. python 生成器&amp;迭代器
  3. February 11 2017 Week 6 Saturday
  4. 在Java中字符串是通过引用传递的?
  5. SAP S/4HANA extensibility扩展原理介绍
  6. 150行JavaScript代码实现增强现实
  7. bootstrap table footerFormatter用法 统计列求和 sum、average等
  8. python 时间和时间戳的转化
  9. 代码阅读:AFNetworking背后的思想
  10. Pollard_rho 因数分解