生活随笔
收集整理的這篇文章主要介紹了
单向循环链表的实现
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 單向循環(huán)鏈表的實(shí)現(xiàn)
- 單向循環(huán)鏈表的初始化
- 向鏈表中插入一個(gè)數(shù)據(jù)
- 刪除鏈表中的一個(gè)數(shù)據(jù)
- 查找鏈表中的指定的數(shù)據(jù)
單向循環(huán)鏈表的實(shí)現(xiàn)
單向循環(huán)鏈表的初始化
void ds_init(node
**pNode
)
{int item
;node
*temp
;node
*target
;printf("初始化單向循環(huán)鏈表\n");while(1){scanf("%d", &item
);fflush(stdin);printf("item = %d\n\n", item
);if(item
== 0)return;if((*pNode
) == NULL){ *pNode
= (node
*)malloc(sizeof(struct CLinkList
));if(!(*pNode
))exit(0);(*pNode
)->data
= item
;(*pNode
)->next
= *pNode
;}else{for(target
= (*pNode
); target
->next
!= (*pNode
); target
= target
->next
);temp
= (node
*)malloc(sizeof(struct CLinkList
));if(!temp
)exit(0);temp
->data
= item
;temp
->next
= *pNode
;target
->next
= temp
;}}
}
向鏈表中插入一個(gè)數(shù)據(jù)
void ds_insert(node
**pNode
, int i
)
{node
*temp
;node
*target
;node
*p
;int item
;int j
= 1;printf("向鏈表中插入數(shù)據(jù):");scanf("%d", &item
);if(i
== 1){ temp
= (node
*)malloc(sizeof(struct CLinkList
));if(!temp
)exit(0);temp
->data
= item
;for(target
= (*pNode
); target
->next
!= (*pNode
); target
= target
->next
);temp
->next
= (*pNode
);target
->next
= temp
;*pNode
= temp
;}else{target
= *pNode
;for( ; j
< (i
-1); ++j
){target
=target
->next
;}temp
= (node
*)malloc(sizeof(struct CLinkList
));if(!temp
)exit(0);temp
->data
= item
;p
= target
->next
;target
->next
= temp
;temp
->next
= p
;}
}
刪除鏈表中的一個(gè)數(shù)據(jù)
void ds_delete(node
**pNode
, int i
)
{node
*target
;node
*temp
;int j
= 1;if(i
== 1){ for(target
= *pNode
; target
->next
!= *pNode
;target
= target
->next
);temp
= *pNode
;*pNode
= (*pNode
)->next
;target
->next
= *pNode
;free(temp
);}else{target
= *pNode
;for( ; j
< i
-1; ++j
){target
= target
->next
;}temp
= target
->next
;target
->next
= temp
->next
;free(temp
);}
}
查找鏈表中的指定的數(shù)據(jù)
int ds_search(node
*pNode
, int elem
)
{node
*target
;int i
= 1;for(target
= pNode
; target
->data
!= elem
&& target
->next
!= pNode
; ++i
){target
= target
->next
;}if(target
->next
== pNode
) return 0;elsereturn i
;
}
#include <stdio.h>
#include <stdlib.h>typedef struct CLinkList
{int data
;struct CLinkList
*next
;
}node
;void ds_init(node
**pNode
);void ds_insert(node
**pNode
, int i
);void ds_delete(node
**pNode
, int i
);int ds_search(node
*pNode
, int elem
);void ds_traverse(node
*pNode
);
void ds_init(node
**pNode
)
{int item
;node
*temp
;node
*target
;printf("初始化單向循環(huán)鏈表\n");while(1){scanf("%d", &item
);fflush(stdin);printf("item = %d\n\n", item
);if(item
== 0)return;if((*pNode
) == NULL){ *pNode
= (node
*)malloc(sizeof(struct CLinkList
));if(!(*pNode
))exit(0);(*pNode
)->data
= item
;(*pNode
)->next
= *pNode
;}else{for(target
= (*pNode
); target
->next
!= (*pNode
); target
= target
->next
);temp
= (node
*)malloc(sizeof(struct CLinkList
));if(!temp
)exit(0);temp
->data
= item
;temp
->next
= *pNode
;target
->next
= temp
;}}
}void ds_insert(node
**pNode
, int i
)
{node
*temp
;node
*target
;node
*p
;int item
;int j
= 1;printf("向鏈表中插入數(shù)據(jù):");scanf("%d", &item
);if(i
== 1){ temp
= (node
*)malloc(sizeof(struct CLinkList
));if(!temp
)exit(0);temp
->data
= item
;for(target
= (*pNode
); target
->next
!= (*pNode
); target
= target
->next
);temp
->next
= (*pNode
);target
->next
= temp
;*pNode
= temp
;}else{target
= *pNode
;for( ; j
< (i
-1); ++j
){target
=target
->next
;}temp
= (node
*)malloc(sizeof(struct CLinkList
));if(!temp
)exit(0);temp
->data
= item
;p
= target
->next
;target
->next
= temp
;temp
->next
= p
;}
}void ds_delete(node
**pNode
, int i
)
{node
*target
;node
*temp
;int j
= 1;if(i
== 1){ for(target
= *pNode
; target
->next
!= *pNode
;target
= target
->next
);temp
= *pNode
;*pNode
= (*pNode
)->next
;target
->next
= *pNode
;free(temp
);}else{target
= *pNode
;for( ; j
< i
-1; ++j
){target
= target
->next
;}temp
= target
->next
;target
->next
= temp
->next
;free(temp
);}
}
int ds_search(node
*pNode
, int elem
)
{node
*target
;int i
= 1;for(target
= pNode
; target
->data
!= elem
&& target
->next
!= pNode
; ++i
){target
= target
->next
;}if(target
->next
== pNode
) return 0;elsereturn i
;
}
void ds_traverse(node
*pNode
)
{node
*temp
;printf("***********鏈表中的數(shù)據(jù)******************\n");temp
= pNode
;printf("***********鏈表中的數(shù)據(jù)******************\n");do{printf("%4d ", temp
->data
);}while((temp
= temp
->next
) != pNode
);printf("\n");
}int main()
{node
*pHead
= NULL;char opp
;int find
;printf("1.初始化單向循環(huán)鏈表 \n\n2.插入數(shù)據(jù) \n\n3.刪除一個(gè)節(jié)點(diǎn) \n\n4.搜索鏈表中指定的數(shù)據(jù) \n\n5.打印鏈表數(shù)據(jù) \n\n0.退出 \n\n請(qǐng)輸入你的選項(xiàng)");while(opp
!= '0'){scanf("%c", &opp
);switch(opp
){case '1':ds_init(&pHead
);printf("\n");break;case '2':printf("向鏈表中插入數(shù)據(jù)");scanf("%d", &find
);ds_insert(&pHead
, find
);printf("插入的數(shù)據(jù)為:%d\n", find
);ds_traverse(pHead
);printf("\n");break;case '3':printf("刪除指定的鏈表節(jié)點(diǎn)");scanf("%d", &find
);ds_delete(&pHead
, find
);printf("刪除的節(jié)點(diǎn)為: %d\n", find
);ds_traverse(pHead
);printf("\n");break;case '4':printf("搜索鏈表中指定的數(shù)據(jù)");scanf("%d", &find
);printf("要查找的數(shù)據(jù)為%d\n", find
, ds_search(pHead
, find
));printf("\n");break;case '5':ds_traverse(pHead
);printf("\n");break;case '6':exit(0);}}return 0;
}
總結(jié)
以上是生活随笔為你收集整理的单向循环链表的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。