展开全部
#include <stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct list
{
int num;
struct list *next;
}List;
int n=0;
List *creat()
{
List *head,*p1,*p2;
int i;
if((head=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
p1=p2=head;
printf("输入创建链表的长度:");
scanf("%d",&head->num);//创建列表,带头结点,头结点数据域表示输入的个数
if(head->num==0)
{
head->next=NULL;
printf("已创建带头结点的空链表");
}
else
{
printf("输入数据:\n");
for(i=0;i<head->num;i++)
{
if((p1=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
scanf("%d",&p1->num);
p2->next=p1;
p2=p1;
}
p1->next=head;
}
return(head);
}
void print(List *head)
{
List *p;
p=head->next;
for(;p!=head;)
{
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
void main()
{
List *head;
head=creat();
print(head);
}
#include<malloc.h>
#include<stdlib.h>
typedef struct list
{
int num;
struct list *next;
}List;
int n=0;
List *creat()
{
List *head,*p1,*p2;
int i;
if((head=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
p1=p2=head;
printf("输入创建链表的长度:");
scanf("%d",&head->num);//创建列表,带头结点,头结点数据域表示输入的个数
if(head->num==0)
{
head->next=NULL;
printf("已创建带头结点的空链表");
}
else
{
printf("输入数据:\n");
for(i=0;i<head->num;i++)
{
if((p1=(List *)malloc(sizeof(List)))==NULL)
{
printf("Error");
exit(0);
}
scanf("%d",&p1->num);
p2->next=p1;
p2=p1;
}
p1->next=head;
}
return(head);
}
void print(List *head)
{
List *p;
p=head->next;
for(;p!=head;)
{
printf("%d ",p->num);
p=p->next;
}
printf("\n");
}
void main()
{
List *head;
head=creat();
print(head);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
* 双向循环链表
*/
#include <stdio.h>
#include<stdlib.h>
#define MAXSIZE 10
typedef int Elemtype;
typedef struct LNode {
Elemtype data;
struct LNode *pre;
struct LNode *next;
}LNode, *DoubleLinkList;
void insert(DoubleLinkList *tail, Elemtype data) {
DoubleLinkList head = (*tail)->next;
(*tail)->next = (DoubleLinkList)malloc(sizeof(LNode));
(*tail)->next->data = data;
(*tail)->next->pre = (*tail);
(*tail)->next->next = head;
(*tail) = (*tail)->next;
head->pre = (*tail);
}
void main() {
DoubleLinkList head, tail;
head = (DoubleLinkList)malloc(sizeof(LNode));
head->data = 0;
head->pre = head;
head->next = head;
tail = head;
insert(&tail, 1);
insert(&tail, 2);
insert(&tail, 3);
insert(&tail, 4);
insert(&tail, 5);
DoubleLinkList pr = head;
while (1) {
printf("%d\n", pr->data);
if (pr->next == head) {
break;
}
pr = pr->next;
}
return;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询