標籤:
頭指標存放鏈表元素數量,前端節點開始存放資料,尾節點指向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單鏈表操作