C语言中如何实现循环链表的简单示例代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计591个文字,预计阅读时间需要3分钟。
C语言实现循环链表的简单示例及示例代码:循环链表是一种链式存储结构,其特点是最后一个节点指向头节点,形成一个环。下面是一个简单的循环链表实现示例。
c#include #include
// 定义链表节点结构体typedef struct Node { int data; struct Node* next;} Node;
// 创建新节点Node* createNode(int data) { Node* newNode=(Node*)malloc(sizeof(Node)); if (!newNode) { return NULL; } newNode->data=data; newNode->next=NULL; return newNode;}
// 创建循环链表Node* createCircularList(int* arr, int size) { if (size==0) { return NULL; } Node* head=createNode(arr[0]); Node* current=head; for (int i=1; i next=createNode(arr[i]); current=current->next; } current->next=head; // 形成环 return head;}
// 打印循环链表void printCircularList(Node* head) { if (head==NULL) { return; } Node* current=head; do { printf(%d , current->data); current=current->next; } while (current !=head); printf(\n);}
// 释放循环链表void freeCircularList(Node* head) { if (head==NULL) { return; } Node* current=head; while (current->next !=head) { Node* temp=current; current=current->next; free(temp); } free(current);}
int main() { int arr[]={1, 2, 3, 4, 5}; int size=sizeof(arr) / sizeof(arr[0]); Node* head=createCircularList(arr, size); printCircularList(head); freeCircularList(head); return 0;}
C语言数据结构之循环链表的简单实例
实例代码:
# include <stdio.h> # include <stdlib.h> typedef struct node //定义链表中结点的结构 { int code; struct node *next; }NODE,*LinkList; /*错误信息输出函数*/ void Error(char *message) { fprintf(stderr,"Error:%s/n",message); exit(1); } //创建循环链表 LinkList createList(int n) { LinkList head; //头结点 LinkList p; //当前创建的节点 LinkList tail; //尾节点 int i; head=(NODE *)malloc(sizeof(NODE));//创建循环链表的头节点 if(!head) { Error("memory allocation error!/n"); } head->code=1; head->next=head; tail=head; for(i=2;i<n;i++) { //创建循环链表的节点 p=(NODE *)malloc(sizeof(NODE)); tail->next=p; p->code=i; p->next=head; tail=p; } return head; }
第二种方法:
//创建循环链表方法2(软件设计师教程书上的方法) 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); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
本文共计591个文字,预计阅读时间需要3分钟。
C语言实现循环链表的简单示例及示例代码:循环链表是一种链式存储结构,其特点是最后一个节点指向头节点,形成一个环。下面是一个简单的循环链表实现示例。
c#include #include
// 定义链表节点结构体typedef struct Node { int data; struct Node* next;} Node;
// 创建新节点Node* createNode(int data) { Node* newNode=(Node*)malloc(sizeof(Node)); if (!newNode) { return NULL; } newNode->data=data; newNode->next=NULL; return newNode;}
// 创建循环链表Node* createCircularList(int* arr, int size) { if (size==0) { return NULL; } Node* head=createNode(arr[0]); Node* current=head; for (int i=1; i next=createNode(arr[i]); current=current->next; } current->next=head; // 形成环 return head;}
// 打印循环链表void printCircularList(Node* head) { if (head==NULL) { return; } Node* current=head; do { printf(%d , current->data); current=current->next; } while (current !=head); printf(\n);}
// 释放循环链表void freeCircularList(Node* head) { if (head==NULL) { return; } Node* current=head; while (current->next !=head) { Node* temp=current; current=current->next; free(temp); } free(current);}
int main() { int arr[]={1, 2, 3, 4, 5}; int size=sizeof(arr) / sizeof(arr[0]); Node* head=createCircularList(arr, size); printCircularList(head); freeCircularList(head); return 0;}
C语言数据结构之循环链表的简单实例
实例代码:
# include <stdio.h> # include <stdlib.h> typedef struct node //定义链表中结点的结构 { int code; struct node *next; }NODE,*LinkList; /*错误信息输出函数*/ void Error(char *message) { fprintf(stderr,"Error:%s/n",message); exit(1); } //创建循环链表 LinkList createList(int n) { LinkList head; //头结点 LinkList p; //当前创建的节点 LinkList tail; //尾节点 int i; head=(NODE *)malloc(sizeof(NODE));//创建循环链表的头节点 if(!head) { Error("memory allocation error!/n"); } head->code=1; head->next=head; tail=head; for(i=2;i<n;i++) { //创建循环链表的节点 p=(NODE *)malloc(sizeof(NODE)); tail->next=p; p->code=i; p->next=head; tail=p; } return head; }
第二种方法:
//创建循环链表方法2(软件设计师教程书上的方法) 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); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

