久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

單向鏈表應(yīng)用函數(shù)

作者:劉蕊飛   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2014年03月02日   【字體:

 注意:創(chuàng)建節(jié)點(diǎn),一定要銷毀節(jié)點(diǎn)。

 

 

#include <stdio.h>

 

#include <stdlib.h>

 

typedef struct node{

 

       intdata;

 

       structnode *next;

 

} node_t;

 

// 創(chuàng)建節(jié)點(diǎn)函數(shù)

 

void *create(int size);

 

// 初始化鏈表

 

int init(node_t *head);

 

// 頭插入法

 

int insert_head(node_t *head,node_t *pn);

 

// 尾插入法

 

int insert_end(node_t *head,node_t *pn);

 

// 打印所有節(jié)點(diǎn)內(nèi)容

 

void print(node_t *head);

 

// 銷毀所有節(jié)點(diǎn)

 

void destroy(node_t *head);

 

// 應(yīng)用函數(shù)

 

// 創(chuàng)建長(zhǎng)度為 len 的鏈表并輸入內(nèi)容

 

int create_link(node_t *head,int len);

 

 

 

int main()

 

{

 

       node_t*head = NULL;

 

       intlen = 0;

 

 

 

       if(init(head= create(sizeof(node_t))) != 0){

 

              printf("初始化鏈表失敗\n");

 

              exit(0);

 

       }

 

       printf("長(zhǎng)度:");

 

       scanf("%d",&len);

 

       create_link(head,len);

 

       print(head);

 

 

 

       destroy(head);

 

       free(head);

 

       head= NULL;

 

 

 

       return0;

 

}

 

// 創(chuàng)建節(jié)點(diǎn)函數(shù)

 

// 成功返回新節(jié)點(diǎn)首地址,失敗返回 NULL

 

void *create(int size)

 

{

 

       returncalloc(1,size);

 

}

 

// 初始化鏈表

 

// 0-成功 1-失敗

 

int init(node_t *head)

 

{

 

       if(NULL== head)

 

              return1;

 

       head->next= NULL;

 

 

 

       return0;

 

}

 

// 頭插入法

 

// 0-成功 1-失敗

 

int insert_head(node_t *head,node_t *pn)

 

{

 

       if(NULL== pn)

 

              return1;

 

       pn->next= head->next;

 

       head->next= pn;

 

 

 

       return0;

 

}

 

// 尾插入法

 

// 0-成功 1-失敗

 

int insert_end(node_t *head,node_t *pn)

 

{

 

       node_t*tail = NULL;

 

 

 

       if(NULL== pn)

 

              return1;

 

       tail= head;

 

       while(tail->next!= NULL)

 

              tail= tail->next;

 

       tail->next= pn;

 

       pn->next= NULL;

 

 

 

       return0;

 

}

 

// 打印所有節(jié)點(diǎn)內(nèi)容

 

void print(node_t *head)

 

{

 

       node_t*cur = NULL;

 

 

 

       cur= head->next;

 

       while(cur!= NULL){

 

              printf("%d",cur->data);

 

              cur= cur->next;

 

       }

 

       printf("\n");

 

}

 

// 銷毀所有節(jié)點(diǎn)

 

void destroy(node_t *head)

 

{

 

       node_t*del =NULL,*n_node = NULL;

 

 

 

       del = head->next;

 

       while(del != NULL){

 

              n_node= del->next;

 

              free(del);

 

              del = n_node;

 

       }

 

       init(head);

 

}

 

// 應(yīng)用函數(shù)

 

// 創(chuàng)建長(zhǎng)度為 len 的鏈表并輸入內(nèi)容

 

// 返回創(chuàng)建的節(jié)點(diǎn)數(shù)

 

int create_link(node_t *head,int len)

 

{

 

       inti = 0;

 

       node_t*n_node = NULL;

 

 

 

       printf("輸入 %d 個(gè)數(shù):\n",len);

 

       for(i= 0;i < len;i++){

 

              n_node= create(sizeof(node_t));//創(chuàng)建新節(jié)點(diǎn)

 

              if(NULL== n_node)

 

                     returni;

 

              scanf("%d",&n_node->data); // 輸入數(shù)據(jù)

 

              insert_end(head,n_node);      // 插入鏈表

 

       }

 

 

 

       returni;

 

}

關(guān)閉窗口

相關(guān)文章

主站蜘蛛池模板: 日韩高清中文字幕 | 天堂资源 | 精品久久久久久久久久 | 国产色网 | 国产乱码精品一区二三赶尸艳谈 | 亚洲免费在线观看 | 成人午夜电影网 | a欧美| 久久av一区二区三区 | 欧美一二三| 国产精品揄拍一区二区 | 颜色网站在线观看 | 久久亚洲欧美日韩精品专区 | 色综合一区二区三区 | 亚洲欧美一区二区三区情侣bbw | 81精品国产乱码久久久久久 | 涩涩片影院 | 亚洲精品影院 | 亚洲激情av| 欧美日本韩国一区二区三区 | 国产这里只有精品 | 成人综合久久 | 欧美日韩中文字幕在线 | 91久久精品日日躁夜夜躁国产 | 在线天堂免费中文字幕视频 | 波多野吉衣在线播放 | 中文字幕一区二区三区日韩精品 | 国产精品视频导航 | 日本在线免费看最新的电影 | 精品一区二区三区不卡 | 99re热这里只有精品视频 | 国产精品视频一区二区三区不卡 | 麻豆精品一区二区三区在线观看 | 成人福利视频网站 | 色中文在线| 97国产精品 | 毛片大全| 日韩精品免费 | 热99在线| 99久久久无码国产精品 | 国产黄色麻豆视频 |