各種基本演算法實現小結(一)—— 單鏈表
(均已測試通過)
============================================================
單鏈表(測試通過)
測試環境: Win-TC
#include <stdio.h><br />struct _node<br />{<br /> int data;<br /> struct _node *next;<br />};<br />typedef struct _node list;<br />void display(list *l)<br />{<br /> list *p;<br /> p=l;<br /> while(p->next)<br /> {<br /> printf("%5d", p->next->data);<br /> p=p->next;<br /> }<br />}<br />void main()<br />{<br /> int i, n;<br /> list *h, *p, *s;<br /> printf("Enter num n:");<br /> scanf("%d", &n);<br /> h=(list*)malloc(sizeof(list));<br /> h->data=-1;<br /> h->next=NULL;<br /> s=p=h;<br /> for(i=n;i>0;i--)<br /> {<br /> p=(list*)malloc(sizeof(list));<br /> scanf("%d", &(p->data));<br /> p->next=h->next;<br /> h->next=p;<br /> h=h->next;<br /> }<br /> display(s);<br /> getch();<br />}
運行結果:
=================================================
單鏈表各種操作(測試通過)
測試環境: Win-TC
#include <stdio.h><br />#include <malloc.h><br />#include <stdlib.h><br />struct _node<br />{<br /> int data;<br /> struct _node *next;<br />};<br />typedef struct _node node, *plist;<br />plist init_list()<br />{<br /> plist pl;<br /> pl=(plist)malloc(sizeof(node));<br /> if(NULL==pl)<br /> {<br /> printf("init list, malloc is fail.../n");<br /> return NULL;<br /> }<br /> pl->data=-1;<br /> pl->next=NULL;<br /> return pl;<br />}<br />int isempty_list(plist pl)<br />{<br /> if(NULL==pl || NULL!=pl->next)<br /> return 1;<br /> else<br /> return 0;<br />}<br />plist clear_list(plist pl)<br />{<br /> pl=NULL;<br /> return pl;<br />}<br />void destroy_list(plist pl)<br />{<br /> plist p, s;</p><p> p=pl->next;<br /> while(p)<br /> {<br /> s=p;<br /> p=p->next;<br /> free(s);<br /> }</p><p> pl=NULL;<br />}<br />void insert_item(plist pl, int i, int e)<br />{<br /> int j=1;<br /> plist p, s;<br /> p=pl;<br /> while(p && j<i)<br /> {<br /> p=p->next;<br /> j++;<br /> }<br /> if(!p || j>i) /* >len or <1 */<br /> printf("Insert fail.../n");<br /> s=(plist)malloc(sizeof(node));<br /> s->data=e;<br /> s->next=p->next;<br /> p->next=s;<br />}<br />void display(plist pl)<br />{<br /> plist p;<br /> p=pl->next;<br /> while(pl && p)<br /> {<br /> printf("%5d", p->data);<br /> p=p->next;<br /> }<br /> printf("/n/n");<br />}<br />int getbyid_item(plist pl, int i)<br />{<br /> plist p=pl->next;<br /> int j=1;<br /> while(p && j<i)<br /> {<br /> p=p->next;<br /> j++;<br /> }<br /> if(!p || j>i) /* >len or <1 */<br /> {<br /> printf("fail.../n");<br /> exit(1);<br /> }<br /> return p->data;<br />}<br />int locate_item(plist pl, int e)<br />{<br /> plist p=pl->next;<br /> int j=1;<br /> while(p->data != e && p->next)<br /> {<br /> p=p->next;<br /> j++;<br /> }<br /> if(p->data == e)<br /> return j;<br /> else<br /> {<br /> printf("There is n %d in list/n", e);<br /> return -1;<br /> }<br />}<br />void delete_item(plist pl, int i, int *e)<br />{<br /> plist p=pl;<br /> plist q;<br /> int j=1;<br /> while(p->next && j<i)<br /> {<br /> p=p->next;<br /> j++;<br /> }<br /> if(!p->next || j>i) /* >len or <1 */<br /> {<br /> printf("fail..../n");<br /> return;<br /> }<br /> q=p->next;<br /> p->next=q->next;<br /> *e=q->data;<br /> free(q);<br />}<br />int len_list(plist pl)<br />{<br /> int j=0;<br /> plist p=pl;<br /> while(pl && p->next)<br /> {<br /> j++;<br /> p=p->next;<br /> }<br /> return j;<br />}<br />plist traverse_list(plist pl)<br />{<br /> plist h, p, s;</p><p> if(!pl || !pl->next)<br /> return pl;<br /> h=pl->next;<br /> s=h;<br /> p=s->next;<br /> h->next=NULL;<br /> while(p)<br /> {<br /> s=p;<br /> p=p->next;<br /> s->next=h;<br /> h=s;<br /> }<br /> pl->next=h;</p><p> return pl;<br />}<br />void main()<br />{<br /> int len, pos, *del;<br /> plist pl=NULL;<br /> del=(int *)malloc(sizeof(int));<br /> pl=init_list();<br /> isempty_list(pl);<br /> insert_item(pl, 1, 1);<br /> insert_item(pl, 2, 3);<br /> insert_item(pl, 3, 5);<br /> insert_item(pl, 4, 7);<br /> insert_item(pl, 5, 9);<br /> insert_item(pl, 6, 11);<br /> display(pl);<br /> len=len_list(pl);<br /> printf("link list len: %d/n", len);<br /> pos=locate_item(pl, 7);<br /> printf("num 7 pos: %d/n", pos);<br /> delete_item(pl, 3, del);<br /> printf("delete pos 3 num: %d/n", *del);<br /> display(pl);<br /> printf("link list traverse.../n");<br /> pl=traverse_list(pl);<br /> display(pl);<br /> destroy_list(pl);<br /> getch();<br />}
運行結果:
=================================================
單向迴圈鏈表(測試通過)
測試環境: Win-TC
#include <stdio.h><br />#include <malloc.h><br />struct _node<br />{<br /> int data;<br /> struct _node *next;<br />};<br />typedef struct _node node, *plist;<br />plist init_list()<br />{<br /> plist pl=(plist)malloc(sizeof(node));<br /> if(!pl)<br /> {<br /> printf("error malloc fail.../n");<br /> return NULL;<br /> }<br /> pl->data=-1;<br /> pl->next=pl; /* pl->next=NULL */<br /> return pl;<br />}<br />void insert_item(plist pl, int pos, int data)<br />{<br /> int j=0;<br /> plist p,s;<br /> s=p=pl;<br /> while(p && j<pos-1)<br /> {<br /> p=p->next;<br /> j++;<br /> }<br /> if(!p || j>pos-1)<br /> {<br /> printf("Error insert fail.../n");<br /> return;<br /> }<br /> s=(plist)malloc(sizeof(node));<br /> if(!s)<br /> {<br /> printf("Error malloc fail.../n");<br /> return;<br /> }<br /> s->data=data;<br /> s->next=p->next;<br /> p->next=s;<br />}<br />int find_item(plist pl, int data)<br />{<br /> plist s,p;<br /> s=p=pl;<br /> p=p->next;<br /> while(s != p)<br /> {<br /> if(data==p->data)<br /> return 1;<br /> p=p->next;<br /> }<br /> return 0;<br />}<br />void delete_item(plist pl, int data)<br />{<br /> plist p,s;<br /> s=p=pl;<br /> if(data == p->data) /* first item is equal with data, then last item = second item */<br /> {<br /> s=p;<br /> while(s != p->next)<br /> p=p->next;<br /> p->next=s->next;<br /> return;<br /> }<br /> while(s != p->next) /* first item is not equal with data */<br /> {<br /> if(data == p->next->data)<br /> {<br /> p->next=p->next->next;<br /> return;<br /> }<br /> p=p->next;<br /> }<br />}<br />void display(plist pl)<br />{<br /> plist s,p;<br /> s=p=pl;<br /> printf("%5d", p->data); /* print first item */<br /> p=p->next;<br /> while(s != p)<br /> {<br /> printf("%5d", p->data);<br /> p=p->next;<br /> }<br /> printf("/n/n");<br />}<br />void main()<br />{<br /> int f;<br /> plist pl;<br /> pl=init_list();<br /> insert_item(pl, 1, 1);<br /> insert_item(pl, 2, 3);<br /> insert_item(pl, 3, 5);<br /> insert_item(pl, 4, 7);<br /> insert_item(pl, 5, 9);<br /> display(pl);<br /> printf("Finding 3.../n");<br /> f=find_item(pl, 3);<br /> if(f)<br /> printf("True find 3/n");<br /> else<br /> printf("False find 3.../n");<br /> printf("/nDeleting 1.../n");<br /> delete_item(pl->next, 1);<br /> display(pl->next);<br /> getch();<br />}
運行結果:
=================================================
雙向迴圈鏈表(測試通過)
測試環境: Win-TC
#include <stdio.h><br />#include <malloc.h><br />struct _node<br />{<br /> int data;<br /> struct _node *prior;<br /> struct _node *next;<br />};<br />typedef struct _node node, *plist;<br />plist init_list()<br />{<br /> plist p;<br /> p=(plist)malloc(sizeof(node));<br /> if(!p)<br /> {<br /> printf("Error, malloc fail.../n");<br /> return NULL;<br /> }<br /> p->data=-1; /* head->data = -1 */<br /> p->prior=p;<br /> p->next=p;<br /> return p;<br />}<br />void insert_item(plist pl, int pos, int data)<br />{<br /> int j=0;<br /> plist s,p;<br /> p=pl;<br /> while(p && j<pos-1)<br /> {<br /> p=p->next;<br /> j++;<br /> }<br /> if(!p || j>pos-1) /* pos is less than 1 or pos larger than len_list+1 */<br /> {<br /> printf("Error %d is invalide num.../n", pos);<br /> return;<br /> }<br /> s=(plist)malloc(sizeof(node));<br /> if(!s)<br /> {<br /> printf("Error, malloc fail.../n");<br /> return NULL;<br /> }<br /> s->data=data;<br /> s->prior=p;<br /> s->next=p->next;<br /> p->next->prior=s;<br /> p->next=s;<br />}<br />int find_item(plist pl, int data)<br />{<br /> plist s,p;<br /> s=p=pl;<br /> if(data == p->data)<br /> return 1;<br /> p=p->next;<br /> while(s != p)<br /> {<br /> if(data == p->data)<br /> return 1;<br /> p=p->next;<br /> }<br /> return 0;<br />}<br />void delete_item(plist pl, int data)<br />{<br /> plist s,p;<br /> s=p=pl;<br /> if(data == p->data) /* first check equal */<br /> {<br /> p->prior->next=p->next;<br /> p->next=p->prior;<br /> return;<br /> }<br /> while(s != p->next)<br /> {<br /> if(data == p->next->data)<br /> {<br /> p->next=p->next->next;<br /> p->next->next->prior=p;<br /> }<br /> p=p->next;<br /> }<br />}<br />void display(plist pl)<br />{<br /> plist s,p;<br /> s=p=pl;<br /> printf("%5d", p->data); /* first item, such as head->data is -1 */<br /> p=p->next;<br /> while(s != p)<br /> {<br /> printf("%5d", p->data);<br /> p=p->next;<br /> }<br /> printf("/n/n");<br />}<br />void main()<br />{<br /> int f;<br /> plist pl;<br /> pl=init_list();<br /> insert_item(pl, 1, 1);<br /> insert_item(pl, 2, 3);<br /> insert_item(pl, 3, 5);<br /> insert_item(pl, 4, 7);<br /> insert_item(pl, 5, 9);<br /> display(pl);<br /> printf("Finding 3.../n");<br /> f=find_item(pl->next->next, 3);<br /> if(f)<br /> printf("True find 3/n");<br /> else<br /> printf("Fail find 3.../n");<br /> printf("Finding 6.../n");<br /> f=find_item(pl->prior->prior, 6);<br /> if(f)<br /> printf("True find 6/n");<br /> else<br /> printf("Fail find 6.../n");<br /> printf("/nDeleting 3.../n");<br /> delete_item(pl->next->next, 3);<br /> display(pl);<br /> getch();<br />}<br />
運行結果:
======================================================
以上代碼,均在Win-TC 1.9.1 編譯器環境測試通過(測試部分用例)
由於剛剛學習演算法,編程水平有限,以上代碼多有考慮不周或錯誤
歡迎大家在下面留言,改正或最佳化代碼,給予指點或建議,謝謝!
參考推薦:
學習演算法之路
各種基本演算法實現小結(一)—— 鏈 表
各種基本演算法實現小結(二)—— 堆 棧
各種基本演算法實現小結(三)—— 樹與二叉樹
各種基本演算法實現小結(四)—— 圖及其遍曆
各種基本演算法實現小結(五)—— 排序演算法
各種基本演算法實現小結(六)—— 尋找演算法
各種基本演算法實現小結(七)—— 常用演算法
12個有趣的C語言面試題