小編給大家分享一下C語言鏈表的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

我們提供的服務(wù)有:網(wǎng)站建設(shè)、成都網(wǎng)站制作、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、姑蘇ssl等。為上1000家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的姑蘇網(wǎng)站制作公司
頭文件:link_0505.h
/*
鏈表演示
*/
#ifndef __LINK_0505
#define __LINK_0505
typedef struct node{
int num;
struct node* p_next;
}node;
typedef struct
{
node head,tail;
}link;
//鏈表的初始化函數(shù)
void link_init(link *);
//鏈表的清理函數(shù)
void link_deinit(link *);
//判斷鏈表是否空的函數(shù)
int link_empty(link *);
//判斷鏈表是否滿的函數(shù)
int link_full(link *);
//統(tǒng)計(jì)有效數(shù)字個(gè)數(shù)的函數(shù)
int link_size(link *);
//在最前邊插入數(shù)字的函數(shù)
int link_add_head(link *, int );
//在最后邊插入新的數(shù)字的函數(shù)
int link_append(link *, int );
//把數(shù)字按照順序插入到鏈表的函數(shù)
int link_insert(link *, int);
//刪除最前面數(shù)字的函數(shù)
int link_remove_head(link *);
//刪除最后一個(gè)有效數(shù)字
int link_remove_tail(link *);
//刪除某個(gè)給定數(shù)字的函數(shù)
int link_remove(link *, int );
//獲得第一個(gè)有效數(shù)字的函數(shù)
int link_get_head(link *, int *);
//獲得最后一個(gè)有效數(shù)字的函數(shù)
int link_get_tail(link *, int *);
//獲得指定編號(hào)數(shù)字的函數(shù)
int link_get(link *, int *, int );
#endif實(shí)現(xiàn)代碼: link_0505.cpp
/*
鏈表演示
*/
#include "stdlib.h"
#include "link_0505.h"
//鏈表的初始化函數(shù)
void link_init(link *p_link)
{
p_link->head.p_next = &(p_link->tail);
}
//鏈表的清理函數(shù)
void link_deinit(link *p_link)
{
while(p_link->head.p_next != &(p_link->tail))
{
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
}
//判斷鏈表是否空的函數(shù)
int link_empty(link *p_link)
{
return p_link->head.p_next == &(p_link->tail);
}
//判斷鏈表是否滿的函數(shù)
int link_full(link *p_link)
{
return 0;
}
//統(tǒng)計(jì)有效數(shù)字個(gè)數(shù)的函數(shù)
int link_size(link *p_link)
{
int cnt = 0;
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail))
{
cnt++;
}
}
return cnt;
}
//在最前邊插入數(shù)字的函數(shù)
int link_add_head(link *p_link, int num)
{
node *p_temp = (node *)malloc(sizeof(node));
if (!p_temp)
{
return 0;
}
p_temp->num = num;
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_temp;
p_temp->p_next = p_mid;
return 1;
}
//在最后邊插入新的數(shù)字的函數(shù)
int link_append(link *p_link, int num)
{
node *p_tmp = (node *)malloc(sizeof(node));
node *p_node = NULL;
if (!p_tmp)
{
return 0;
}
p_tmp->num = num;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &(p_link->tail))
{
p_first->p_next = p_tmp;
p_tmp->p_next = p_mid;
break;
}
}
return 1;
}
//把數(shù)字按照順序插入到鏈表的函數(shù)
int link_insert(link *p_link, int num)
{
node* p_temp = (node *)malloc(sizeof(node));
node* p_node = NULL;
if (!p_temp)
{
return 0;
}
p_temp->num = num;
p_temp->p_next = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid == &(p_link->tail) || p_mid->num > p_temp->num)
{
p_first->p_next = p_temp;
p_temp->p_next = p_mid;
break;
}
}
return 0;
}
//刪除最前面數(shù)字的函數(shù)
int link_remove_head(link *p_link)
{
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
}
//刪除最后一個(gè)有效數(shù)字
int link_remove_tail(link *p_link)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node !=&(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
//刪除某個(gè)給定數(shù)字的函數(shù)
int link_remove(link *p_link, int num)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && p_mid->num == num)
{
p_first->p_next = p_last;
free(p_mid);
p_mid = NULL;
return 1;
}
}
return 0;
}
//獲得第一個(gè)有效數(shù)字的函數(shù)
int link_get_head(link *p_link, int *p_num)
{
if (p_link->head.p_next == &(p_link->tail))
{
return 0;
}
node *p_first = &(p_link->head);
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
p_first->p_next = p_last;
*p_num = p_mid->num;
return 1;
}
//獲得最后一個(gè)有效數(shù)字的函數(shù)
int link_get_tail(link *p_link, int *p_num)
{
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_last == &(p_link->tail))
{
*p_num = p_mid->num;
return 1;
}
}
return 0;
}
//獲得指定編號(hào)數(shù)字的函數(shù)
int link_get(link *p_link, int *p_num, int num)
{
int cnt = 0;
node *p_node = NULL;
for (p_node = &(p_link->head);p_node != &(p_link->tail);p_node = p_node->p_next)
{
node *p_first = p_node;
node *p_mid = p_first->p_next;
node *p_last = p_mid->p_next;
if (p_mid != &(p_link->tail) && cnt == num)
{
*p_num = p_mid->num;
return 1;
}
cnt++;
}
return 0;
}測(cè)試函數(shù):
/*
* 鏈表測(cè)試
* */
#include <stdio.h>
#include "link_0505.h"
int main() {
int size = 0, num = 0, val = 0;
link lnk = {0};
link_init(&lnk);
link_add_head(&lnk, 30);
link_add_head(&lnk, 20);
link_append(&lnk, 90);
link_append(&lnk, 100);
link_insert(&lnk, 50);
link_insert(&lnk, 60);
link_insert(&lnk, 40);
link_insert(&lnk, 80);
link_insert(&lnk, 70);
size = link_size(&lnk);
for (num = 0;num <= size - 1;num++) {
link_get(&lnk, &val, num);
printf("%d ", val);
}
printf("\n");
printf("------------------");
link_remove_head(&lnk);
link_remove_tail(&lnk);
link_remove(&lnk, 70);
size = link_size(&lnk);
for (num = 0;num <= size - 1;num++) {
link_get(&lnk, &val, num);
printf("%d ", val);
}
printf("\n");
link_get_head(&lnk, &val);
printf("最前面的數(shù)字是%d\n", val);
link_get_tail(&lnk, &val);
printf("最后面的數(shù)字是%d\n", val);
link_deinit(&lnk);
return 0;
}以上是“C語言鏈表的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
當(dāng)前標(biāo)題:C語言鏈表的示例分析
分享鏈接:http://chinadenli.net/article32/ppsssc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、搜索引擎優(yōu)化、網(wǎng)站制作、標(biāo)簽優(yōu)化、網(wǎng)頁設(shè)計(jì)公司、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)