循环链表任意位置输出

#include<stdio.h>
#include<stdlib.h>
#define N 8
#define NULL 0
#define OK 1
#define ERROR 0

typedef struct LNode
{
    struct LNode *next;
    int data;
}LNode,*list;

void creatList(list &l,int n)
{
    list p,q;
    l=(list)malloc(sizeof(LNode));
    scanf("%d",&l->data);
    p=l;
    for(int i=1;i<n;i++)
    {
        q=(list)malloc(sizeof(LNode));
        scanf("%d",&q->data);
        p->next=q;
        p=q;
    }
    p->next=l;
}

void printList(list l,int pos)
{
    list p,q;
    int i;
    p=l;
    for(i=1;i<=pos-1;i++)p=p->next;
    q=p->next;
    do
    {
        printf("%d ",p->data);
        p=p->next;
    }while(p->next!=q);
}

int main()
{
    list l;
    int pos;
    printf("input a list contain %d elements:\n",N);
    creatList(l,N);
    printf("start from:\n");
    scanf("%d",&pos);
    while(pos<=0||pos>N)
    {
        printf("wrong place,input again:\n");
        scanf("%d",&pos);
    }
    printList(l,pos);
    printf("\n");
    return 0;

}




你可能感兴趣的:(链表)