C語言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡(jiǎn)單實(shí)例
更新時(shí)間:2017年06月26日 16:15:59 投稿:lqh
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
C語言數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表的簡(jiǎn)單實(shí)例
實(shí)例代碼:
# include <stdio.h>
# include <stdlib.h>
typedef struct node //定義鏈表中結(jié)點(diǎn)的結(jié)構(gòu)
{
int code;
struct node *next;
}NODE,*LinkList;
/*錯(cuò)誤信息輸出函數(shù)*/
void Error(char *message)
{
fprintf(stderr,"Error:%s/n",message);
exit(1);
}
//創(chuàng)建循環(huán)鏈表
LinkList createList(int n)
{
LinkList head; //頭結(jié)點(diǎn)
LinkList p; //當(dāng)前創(chuàng)建的節(jié)點(diǎn)
LinkList tail; //尾節(jié)點(diǎn)
int i;
head=(NODE *)malloc(sizeof(NODE));//創(chuàng)建循環(huán)鏈表的頭節(jié)點(diǎn)
if(!head)
{
Error("memory allocation error!/n");
}
head->code=1;
head->next=head;
tail=head;
for(i=2;i<n;i++)
{
//創(chuàng)建循環(huán)鏈表的節(jié)點(diǎn)
p=(NODE *)malloc(sizeof(NODE));
tail->next=p;
p->code=i;
p->next=head;
tail=p;
}
return head;
}
第二種方法:
//創(chuàng)建循環(huán)鏈表方法2(軟件設(shè)計(jì)師教程書上的方法)
LinkList createList2(int n)
{
LinkList head,p;
int i;
head=(NODE *)malloc(sizeof(NODE));
if(!head)
{
printf("memory allocation error/n");
exit(1);
}
head->code=1;
head->next=head;
for(i=n;i>1;--i)
{
p=(NODE *)malloc(sizeof(NODE));
if(!p)
{
printf("memory allocation error!/n");
exit(1);
}
p->code=i;
p->next=head->next;
head->next=p;
}
return head;
}
void output(LinkList head)
{
LinkList p;
p=head;
do
{
printf("%4d",p->code);
p=p->next;
}
while(p!=head);
printf("/n");
}
void main(void)
{
LinkList head;
int n;
printf("input a number:");
scanf("%d",&n);
head=createList(n);
output(head);
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
C語言學(xué)習(xí)進(jìn)階篇之萬字詳解指針與qsort函數(shù)
之前的指針詳解中,提到過qsort函數(shù),這個(gè)函數(shù)是用來排序的,下面這篇文章主要給大家介紹了關(guān)于C語言指針與qsort函數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
C++使用read()和write()讀寫二進(jìn)制文件
以文本形式讀寫文件和以二進(jìn)制形式讀寫文件的區(qū)別,并掌握了用重載的?>>?和?<<?運(yùn)算符實(shí)現(xiàn)以文本形式讀寫文件,在此基礎(chǔ)上,本節(jié)將講解如何以二進(jìn)制形式讀寫文件2023-10-10
實(shí)例講解C++設(shè)計(jì)模式編程中State狀態(tài)模式的運(yùn)用場(chǎng)景
這篇文章主要介紹了實(shí)例講解C++設(shè)計(jì)模式編程中State狀態(tài)模式的運(yùn)用場(chǎng)景,文章最后的適用性部分則介紹了一些State模式善于處理的情況,需要的朋友可以參考下2016-03-03
詳解C++的String類的字符串分割實(shí)現(xiàn)
這篇文章主要介紹了詳解C++的String類的字符串分割實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07
C++靜態(tài)庫與動(dòng)態(tài)庫文件的生成和使用教程
庫文件是計(jì)算機(jī)上的一類文件,可以簡(jiǎn)單的把庫文件看成一種代碼倉庫,它提供給使用者一些可以直接拿來用的變量、函數(shù)和類,下面這篇文章主要給大家介紹了關(guān)于C++靜態(tài)庫與動(dòng)態(tài)庫文件的生成和使用的相關(guān)資料,需要的朋友可以參考下2023-03-03
c++結(jié)合opencv如何實(shí)現(xiàn)讀取多張圖片并顯示
這篇文章主要介紹了c++結(jié)合opencv如何實(shí)現(xiàn)讀取多張圖片并顯示問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

