C單鏈表操作

來源:互聯網
上載者:User

標籤:

  頭指標存放鏈表元素數量,前端節點開始存放資料,尾節點指向NULL

  list.h

#ifndef _LIST_H#define _LIST_H#include <stdio.h>#include <stdlib.h>#define DEBUG 0typedef struct node{    int val;    struct node *next;}Node;Node * l_malloc();        // 分配記憶體Node * init();            // 初始化void show(Node *list);    // 顯示鏈表元素int lsearch_pos(Node *list,int n);    // 左搜尋int rsearch_pos(Node *list,int n);    // 右搜尋int find_val(Node *list,int val);    // 值搜尋Node * insert(Node *list,int pos,int n);    // 插入節點Node * del_node(Node *list,int n);            // 指定位置刪除節點Node * del_val(Node *list,int val);        // 指定值刪除節點#endif

 

  list.c

#include "list.h"/************************************* * 分配記憶體,當失敗時重新嘗試1次  *************************************/Node * l_malloc(){    Node *p = NULL;    int cnt = 3;    do{        p = (Node *)malloc( sizeof(Node) );    }while( NULL == p && --cnt);  // 當失敗時重新嘗試    if( 0 > cnt ){        perror("malloc");        exit(-1);                  // 記憶體配置失敗,退出    }    return p;}/************************************* * 初始化 *************************************/Node * init(){    const int num = 8;    int cnt = 0;    Node *head = NULL;    Node *pcur = NULL;    Node *pnew = l_malloc();        head = pcur = pnew;            //     head->val = 0;                // 頭指標,值記錄鏈表元素數量    for( ; cnt < num; cnt++ ){        pnew = l_malloc();        pnew->val = cnt;        pcur->next = pnew;        pcur = pnew;        head->val++;    }    pcur->next = NULL;    return head;    }/************************************* * 顯示 *************************************/void show(Node *list){    while( NULL != list ){        printf("%d->",list->val);        list = list->next;    }    printf("end\n\n");}/************************************* * 從左開始搜尋 *************************************/int lsearch_pos(Node *list,int n){    if( n < 1 || n > list->val ){        // 超出鏈表值數量        printf("exceed length of list\n");        return -1;    }    for( int cnt = 0; cnt < n ; cnt++ ){        list = list->next;    }    return list->val;    }/************************************* * 從右開始搜尋 *************************************/int rsearch_pos(Node *list,int n){    if( n < 1 || n > list->val ){        printf("exceed length of list\n");        return -1;    }    Node *pcur = list->next;        // 定義2個指標:當前位置    Node *ptag = list->next;        // 目標位置    for( int cnt = 0; cnt < n ; cnt++ ){        pcur = pcur->next;    }                                // 使當前位置與目標位置間隔 n     while( NULL != pcur ){            // 當前位置先移動到表尾,此時 ptag 即為要搜尋的位置        pcur = pcur->next;            // 同時移動2個指標        ptag = ptag->next;            //    }    return ptag->val;}/************************************* * 從左開始搜尋指定值 * 有值返回位置,無值返回-1 *************************************/int find_val(Node *list,int val){    if( DEBUG ) printf("in find_val:\n");    int cnt = 1;    int len = list->val;    for( ; cnt <= len; cnt++ ){        list = list->next;        if( DEBUG ) printf("    list->val:%d\n",list->val);        if( val == list->val ){            return cnt;        }    }    return -1;}/************************************* * 指定位置插入新值 *************************************/Node * insert(Node *list,int pos,int n){    if( DEBUG ) printf("in insert:\n");    Node *pcur = list;    Node *pnew = l_malloc();    pnew->val = n;    for( int cnt = 1; cnt < pos && pcur->next ; cnt++ ){    // 到達指定位置或鏈表尾時退出        pcur = pcur->next;        if( DEBUG ) printf("    pcur->val:%d\n",pcur->val);    }    if( NULL == pcur->next ){    // 到達鏈表尾部,測試空鏈表或負數運行正常化        pnew->next = NULL;        // 當pos大於鏈表長度時,插入表尾        pcur->next = pnew;    }    else{        pnew->next = pcur->next;        pcur->next = pnew;    }    list->val++;    return list;}/************************************* * 指定位置刪除值 *************************************/Node * del_node(Node *list,int n){    if( n < 1 || n > list->val ){        printf("exceed length of list\n");        return list;    }    Node *pcur = NULL;    Node *ptag = list;    for( int cnt = 0; cnt < n ; cnt++ ){        pcur = ptag;        ptag = ptag->next;    }    pcur->next = ptag->next;    free(ptag);    list->val--;    return list;}/************************************* * 指定值刪除節點 *************************************/Node * del_val(Node *list,int val){    int n = find_val(list,val);    if( -1 == n ){        // 無指定值,不刪除        return list;    }    return del_node(list,n);}

  main.c

#include "list.h"int main(){    Node *list = init();    show(list);    int num = lsearch_pos( list, 2 );    printf("the 2th value is %d\n",num);    show(list);        num = rsearch_pos( list, 2 );    printf("the value from right 2th is %d\n",num);    show(list);        num = find_val( list, 5 );    printf("the value 5 is in %dth\n",num);    show(list);    list = del_node( list, 1 );        printf("after delete 1th value,the list is:\n");        show(list);            list = del_val( list, 3 );        printf("after delete value 3,the list is:\n");        show(list);            list = del_val( list, 100 );        printf("after delete value 100,the list is:\n");        show(list);    for( int cnt = 10;cnt > 0;cnt -- ){        list = del_node( list, cnt );        printf("after delete %dth value,the list is:\n",cnt);        show(list);    }    for( int cnt = 0;cnt <20 ;cnt ++ ){        list = insert( list, cnt % 5 ,rand() );        printf("after insert a value at %dth,the list is:\n",cnt);        show(list);    }    list = insert( list, 6 ,99 );    printf("after insert a value at 6th,the list is:\n");    show(list);        list = insert( list, -6 ,99 );    printf("after insert a value at -6th,the list is:\n");    show(list);}

 

C單鏈表操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.