单链表

单链表

线性表的链式存储方式可以动态创建结点,逻辑相邻的元素在物理位置上不一定也要相邻,从而能够更好的利用内存空间。而且,对于插入和删除操作来讲,链表的效率要远远高于顺序表。

不同链表示意图:

#include <stdio.h>
#include <stdlib.h>

#define LEN sizeof(ListNode)

int main(){

    typedef char DataType;
    typedef struct node{
        DataType data;
        struct node *next;
}ListNode;
    typedef ListNode *LinkList; 

    int n;

    printf("Choose a function:\n");
    printf("1. To create a list from head\n");
    printf("2. To create a list from tail\n");
    printf("3. Print all the elements in the list\n");
    printf("4. print the length of the list\n");
    printf("5. To insert an element to the list\n");
    printf("6. To delete an element from the list\n");
    printf("7. Search the list\n");
    printf("8. Esc this program\n");

    LinkList head;
    head = ( LinkList )malloc( LEN ); // init a list
    head->next = NULL;

    scanf("%d", &n);

    while( n != 8){
    getchar();

    if( n == 1){ // To create a list from head
        ListNode *p, *temp;
        char ch;
        while( (ch = getchar()) != '\n'){

            p = ( LinkList)malloc( LEN );
            p->data = ch;
            temp = head->next;
            head->next = p;
            p->next = temp;

        }

    }

    if ( n ==2) {
        char ch;
        ListNode *temp, *p;
        temp = head;

        while( ( ch = getchar() ) != '\n'){
            p = ( LinkList  )malloc( LEN );
            p->data = ch;
            temp->next = p;
            p->next = NULL;
            temp = p;
        }
    }

    if( n ==3 ){
        ListNode *temp;
        temp = head;
        while( temp->next){
            printf("%c  ", temp->next->data);
            temp = temp->next;
        }
        printf( "\n");
    }

    if ( n ==4){
        int num=0;
        ListNode *temp;
        temp = head;

        while( temp->next){
            num++;
            temp = temp->next;
    }
    printf("%d\n", num);
    }

    if ( n ==5 ){
        int posi;
        char ch;
        ListNode *temp, *p;
        temp = head;
        printf("Please enter the char you want to insert:");
        scanf("%c", &ch);
        printf("Please enter the position you want to insert the char: ");
        scanf("%d", &posi);
        p = ( LinkList )malloc( LEN );
        p->data = ch;
        int i = 0;
        for( i = 0; i < posi - 1; i++)
            temp = temp->next;

        p->next = temp->next;
        temp->next = p;


    }

    if ( n ==6){
        printf("Please enter the position of the char you want to delete: ");
        int posi;
        ListNode *temp;
        temp = head;
        scanf("%d", &posi);
        int i = 0;
        for(i = 0; i < posi - 1; i++)
            temp = temp->next;
        temp->next = temp->next->next;
    }

    if ( n ==7){
        printf("Please enter the position number:");
        int posi;
        ListNode *temp;
        temp = head;
        scanf("%d", &posi );
        int i=0;
        for(i=0; i < posi; i++)
            temp = temp->next;
        printf("The char at position %d is %c\n", posi, temp->data);

    }

    scanf("%d", &n);
}



return 0;
}

运行截图:

功能列表:

  1. 头插法初始化链表
  2. 尾插法初始化链表
  3. 打印链表所有元素
  4. 打印链表长度
  5. 向链表中插入元素
  6. 从链表中删除元素
  7. 按索引查询链表
  8. 退出程序

好吧,其实只是实现了单链表最最简单的几个功能,其他的还包括按值索引啊,还有在执行查找、插入和删除操作的的时候都没有执行检查,看看所给出的索引是否越界等等。这些功能可以在后面慢慢去完善。

关于在写这个小程序的时候的几点思考:

  • 其实很早之前就翻过《算法导论》这本书,也在网上陆陆续续看了公开课的一些视频。但是因为本身不是计算机专业出身,C语言也好长时间没有碰过了,数据结构上来就是各种结构体、指针这种复杂艰深的东西,看的云里雾里,不明就里,也根本没有把代码自己写一写,所以没过几天就扔下了。最近重新看了下C语言和计算机基础理论方面的书,觉得好像有点懂了,于是就跑去图书馆拎了几本书回来,重新把数据结构拿出来看看,把代码写写,感觉比之前好多了。
  • 其实你们看大部分的数据结构的书都是把功能实现写成函数了,对于基础不好的人来说有点语焉不详,只是拼凑成能够运行的小程序就要费很大的力气。所以这次除了拿了几本“权威”作为参考,还找了几本专为“业余”爱好者写的书。两种书结合起来看效果其实很好,“业余”可以用来入门,而且趣味性很强。学算法嘛,本来就是因为”思考的乐趣“啊 O(∩_∩)O~
  • ”纸上得来终觉浅,绝知此事要躬行。“只是看看可能觉得不是太难,但是即使是最为普通的小程序,想好了思路再付诸实践的过程都有可能会错误百出,调试的你头昏脑胀,更不要说数据结构里面涉及的指针、结构体什么的了。所以,眼高手低是不对滴。
  • 看书的时候很难静得下心来。其实看算法啊,数据结构啊还是需要大片的时间投入去好好思考的,但是总是会有零散的事情打断,专注力和记忆力是很重要的能力。
  • 告诉自己不忘初心,方得始终。于任何人都是,怀着赤子之心的人看上去总是与众不同。
  • 多写写有趣的小程序。现在总是爱看实践的书,写些有趣的小玩意特有成就感,嗯,很快乐。
  • 坚持。这种一个人趴在电脑前写代码的日子一点也不好熬,技术的卓越背后是逼着自己坚持前行。

    • e.g. 代码量与编程水平关系的传说:
      • 微软要求应聘程序员在大学四年间至少写10万行代码
      • 100万行 -- 高手??

    你能写多少代码呢?当然,多少并不是衡量的唯一标准,就像有些人无聊的就只剩下向你炫耀他的忙碌了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值