Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Node Node;
- struct Node { int value; Node *next; };
- static Node *newNode(int);
- static void initCList(Node *);
- static void addCList(Node *, int);
- static void clearCList(Node *);
- static void showCList(Node *);
- static Node *newNode(int value) {
- Node *new = calloc(sizeof *new, 1);
- new->value = value;
- new->next = NULL;
- return new;
- }
- static void initCList(Node *head) {
- clearCList(head);
- }
- static void addCList(Node *head, int value) {
- Node *tail = head;
- for (; tail->next != head; tail = tail->next) {
- ;
- }
- Node *add = newNode(value);
- tail->next = add;
- add->next = head;
- }
- static void clearCList(Node *head) {
- for (Node *p = head->next, *next = p->next; p != head; p = next, next = next->next) {
- free(p);
- head->next = next;
- }
- }
- static void showCList(Node *head) {
- printf("[");
- for (Node *p = head->next; p != head; p = p->next) {
- printf("%s%d", ((p == head->next) ? "" : ", "), p->value);
- }
- puts("]");
- }
- int main(void) {
- Node head = { .value = 20181203, .next = &head };
- initCList(&head); showCList(&head);
- for (int i = 0; i < 5; ++i) {
- addCList(&head, i); showCList(&head);
- }
- int n;
- while (scanf("%d", &n) == 1) {
- addCList(&head, n); showCList(&head);
- }
- clearCList(&head); showCList(&head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement