數據結構 C語言實現循環單鏈表的實例。本站提示廣大學習愛好者:(數據結構 C語言實現循環單鏈表的實例)文章只能為提供參考,不一定能成為您想要的結果。以下是數據結構 C語言實現循環單鏈表的實例正文
投稿:lqh
這篇文章主要介紹了數據結構 C語言實現循環單鏈表的實例的相關資料,需要的朋友可以參考下數據結構 C語言實現循環單鏈表的實例
實例代碼:
//=========楊鑫========================//
//循環單鏈表的實現
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
//定義結點類型
typedef struct Node
{
ElemType data;
struct Node *next;
}Node,*LinkedList;
int count = 0;
//1、單循環鏈表的初始化
LinkedList init_circular_linkedlist()
{
Node *L;
L = (Node *)malloc(sizeof(Node));
if(L == NULL)
printf("申請內存空間失敗\n");
L->next = L;
}
//2、循環單鏈表的建立
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、循環單鏈表的插入,在循環鏈表的第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、循環單鏈表的刪除,在循環鏈表中刪除值為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("請輸入循環單鏈表的數據, 以0結束!\n");
list = creat_circular_linkedlist();
printf("循環單鏈表的元素有:\n");
for(start = list->next; start != NULL; start = start->next)
{
if(count== 0)
{
break;
}
printf("%d ", start->data);
count--;
}
printf("\n");
return 0;
}
如圖:
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!