數(shù)據(jù)結構 C語言實現(xiàn)循環(huán)單鏈表的實例
更新時間:2017年05月10日 11:39:11 投稿:lqh
這篇文章主要介紹了數(shù)據(jù)結構 C語言實現(xiàn)循環(huán)單鏈表的實例的相關資料,需要的朋友可以參考下
數(shù)據(jù)結構 C語言實現(xiàn)循環(huán)單鏈表的實例
實例代碼:
//=========楊鑫========================//
//循環(huán)單鏈表的實現(xiàn)
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
//定義結點類型
typedef struct Node
{
ElemType data;
struct Node *next;
}Node,*LinkedList;
int count = 0;
//1、單循環(huán)鏈表的初始化
LinkedList init_circular_linkedlist()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
if(L == NULL)
printf("申請內(nèi)存空間失敗\n");
L->next = L;
}
//2、循環(huán)單鏈表的建立
LinkedList creat_circular_linkedlist()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
L->next = L;
Node *r;
r = L;
ElemType x;
while(scanf("%d",&x))
{
if(x == 0)
break;
count++;
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
r->next = p;
r = p;
}
r->next = L;
return L;
}
//4、循環(huán)單鏈表的插入,在循環(huán)鏈表的第i個位置插入x的元素
LinkedList insert_circuler_linkedlist(LinkedList L,int i,ElemType x)
{
Node *pre;
pre = L;
int tempi = 0;
for (tempi = 1; tempi < i; tempi++)
pre = pre->next;
Node *p;
p = (Node *)malloc(sizeof(Node));
p->data = x;
p->next = pre->next;
pre->next = p;
return L;
}
//5、循環(huán)單鏈表的刪除,在循環(huán)鏈表中刪除值為x的元素
LinkedList delete_circular_linkedlist(LinkedList L,ElemType x)
{
Node *p,*pre;
p = L->next;
while(p->data != x)
{
pre = p;
p = p->next;
}
pre->next = p->next;
free(p);
return L;
}
int main()
{
int i;
LinkedList list, start;
printf("請輸入循環(huán)單鏈表的數(shù)據(jù), 以0結束!\n");
list = creat_circular_linkedlist();
printf("循環(huán)單鏈表的元素有:\n");
for(start = list->next; start != NULL; start = start->next)
{
if(count== 0)
{
break;
}
printf("%d ", start->data);
count--;
}
printf("\n");
return 0;
}
如圖:

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關文章
C++中Overload,Override,Hide之間的區(qū)別
重載overload,這個概念是大家熟知的。在同一可訪問區(qū)內(nèi)被聲名的幾個具有不同參數(shù)列的(參數(shù)的類型、個數(shù)、順序不同)同名函數(shù),程序會根據(jù)不同的參數(shù)列來確定具體調(diào)用哪個函數(shù),這種機制就是重載2013-09-09
C程序?qū)崿F(xiàn)整數(shù)的素數(shù)和分解問題
這篇文章主要介紹了C程序?qū)崿F(xiàn)整數(shù)的素數(shù)和分解問題,對于算法的學習有不錯的借鑒價值,需要的朋友可以參考下2014-09-09
C++ Boost Serialization庫超詳細獎金額
Boost是為C++語言標準庫提供擴展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標準庫的后備,是C++標準化進程的開發(fā)引擎之一,是為C++語言標準庫提供擴展的一些C++程序庫的總稱2022-12-12
淺析C++中strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法
這篇文章主要介紹了strlen函數(shù)的使用與模擬實現(xiàn)strlen的方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
win10環(huán)境下C++ vs2015編譯opencv249的教程
這篇文章主要介紹了win10環(huán)境下C++ vs2015編譯opencv249的教程,本文分步驟給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

