建立单向循环链表的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct _A{
int data
stru<img id="selectsearch-icon" src="http://img.baidu.com/img/iknow/qb/select-search.png" alt="搜索">ct _A *next
}A
typedef A* IA
void createDoubleLink(IA *header) {
int data
IA p
printf("input data end with -1:")
scanf("%d", &data)
while(data != -1) {
p = (IA)malloc(sizeof(A))
p->data = data
if(*header == NULL) {
*header = p
(*header)->next = *header
}
else{
IA q = *header
while(q->next != *header) {
q = q->next
}
q->next = p
q->next->next = *header
}
scanf("%d", &data)
}
}
void print(IA header) {
IA p = header
if(header == NULL) return
while(1){
printf("%d\t", p->data)
if(p->next == header) break
p = p->next
}
printf("\n")
}
int main()
{
IA header = NULL
createDoubleLink(&header)
print(header)
return 0
}
#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=0i<head->numi++)
{
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)
}