單鏈表

單鏈表

線性表的鏈式存儲方式可以動態創建結點,邏輯相鄰的元素在物理位置上不一定也要相鄰,從而能夠更好的利用內存空間。而且,對於插入和刪除操作來講,鏈表的效率要遠遠高於順序表。

不同鏈表示意圖:

danlianb.gif

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

運行截圖:1366643366_4719.PNG

功能列表:

  1. 頭插法初始化鏈表
  2. 尾插法初始化鏈表
  3. 打印鏈表所有元素
  4. 打印鏈表長度
  5. 向鏈表中插入元素
  6. 從鏈表中刪除元素
  7. 按索引查詢鏈表
  8. 退出程序

好吧,其實只是實現了單鏈表最最簡單的幾個功能,其他的還包括按值索引啊,還有在執行查找、插入和刪除操作的的時候都沒有執行檢查,看看所給出的索引是否越界等等。這些功能可以在後面慢慢去完善。

關於在寫這個小程序的時候的幾點思考:

  • 其實很早之前就翻過《算法導論》這本書,也在網上陸陸續續看了公開課的一些視頻。但是因爲本身不是計算機專業出身,C語言也好長時間沒有碰過了,數據結構上來就是各種結構體、指針這種複雜艱深的東西,看的雲裏霧裏,不明就裏,也根本沒有把代碼自己寫一寫,所以沒過幾天就扔下了。最近重新看了下C語言和計算機基礎理論方面的書,覺得好像有點懂了,於是就跑去圖書館拎了幾本書回來,重新把數據結構拿出來看看,把代碼寫寫,感覺比之前好多了。
  • 其實你們看大部分的數據結構的書都是把功能實現寫成函數了,對於基礎不好的人來說有點語焉不詳,只是拼湊成能夠運行的小程序就要費很大的力氣。所以這次除了拿了幾本“權威”作爲參考,還找了幾本專爲“業餘”愛好者寫的書。兩種書結合起來看效果其實很好,“業餘”可以用來入門,而且趣味性很強。學算法嘛,本來就是因爲”思考的樂趣“啊 O(∩_∩)O~
  • ”紙上得來終覺淺,絕知此事要躬行。“只是看看可能覺得不是太難,但是即使是最爲普通的小程序,想好了思路再付諸實踐的過程都有可能會錯誤百出,調試的你頭昏腦脹,更不要說數據結構裏面涉及的指針、結構體什麼的了。所以,眼高手低是不對滴。
  • 看書的時候很難靜得下心來。其實看算法啊,數據結構啊還是需要大片的時間投入去好好思考的,但是總是會有零散的事情打斷,專注力和記憶力是很重要的能力。
  • 告訴自己不忘初心,方得始終。於任何人都是,懷着赤子之心的人看上去總是與衆不同。
  • 多寫寫有趣的小程序。現在總是愛看實踐的書,寫些有趣的小玩意特有成就感,嗯,很快樂。
  • 堅持。這種一個人趴在電腦前寫代碼的日子一點也不好熬,技術的卓越背後是逼着自己堅持前行。

    • e.g. 代碼量與編程水平關係的傳說:
      • 微軟要求應聘程序員在大學四年間至少寫10萬行代碼
      • 100萬行 -- 高手??

    你能寫多少代碼呢?當然,多少並不是衡量的唯一標準,就像有些人無聊的就只剩下向你炫耀他的忙碌了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章