循环单链表的建立和输出

个人博客网站:https://www.liuzhi.org.cn/ 

#include 
#include 

typedef struct Node {
    int data;
    struct Node *next;
}LNode,*LinkList;

LNode *CreateList() { // 创建单循环链表,返回链表头
    LNode *head,*p;
    int i,n;
printf("请输入要输入的个数");
    scanf("%d",&n);
    head = p = (LNode *)malloc(sizeof(LNode)); // 专用头结点
    head->data = 0;
    for(i = 0; i < n; ++i) {
        p->next = (LNode *)malloc(sizeof(LNode));
        scanf("%d",&p->next->data);
        p = p->next;
    }
    p->next = head;
    return head;
}


void PrintList(LNode *head) {
    LNode *p = head->next;
    short counter = 0;
    while(p != head) {
        if(counter && counter%10 == 0) printf("\n");
        printf("%d ",p->data);
        counter++;
        p = p->next;
    }
    if(counter % 10) printf("\n");
}

void freeheap(LNode *head) {
    LNode *p,*q;
    p = head;
    q = p->next;
    while(q != head) {
        p = q;
        q = p->next;
        free(p);
    }
    free(head);
}

int main() {
    LNode *A;
    A = CreateList();
    PrintList(A);
    freeheap(A);
    printf("\n\n");
    return 0;
}

 

你可能感兴趣的:(循环单链表的建立和输出)