Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Elem {
- struct Elem *prev, *next;
- int v;
- };
- struct Elem *InitDoubleLinkedList()
- {
- struct Elem *x = (struct Elem*)malloc(sizeof(struct Elem));
- x->v = -777777777;
- x->prev = x;
- x->next = x;
- return x;
- }
- void InsertSort(struct Elem *l)
- {
- }
- int main()
- {
- int n, i, x, j;
- scanf("%d\n", &n);
- if (n == 1) {
- scanf("%d", &i);
- printf("%d\n", i);
- }
- else {
- struct Elem *l = InitDoubleLinkedList();
- struct Elem *now = (struct Elem*)malloc(sizeof(struct Elem));
- for (i = 0; i < n; i++) {
- if (i == 0) {
- struct Elem *cur = (struct Elem*)malloc(sizeof(struct Elem));
- scanf("%d ", &x);
- cur->v = x;
- cur->prev = l;
- now = cur;
- }
- if (i < n - 1 && i != 0) {
- struct Elem *cur = (struct Elem*)malloc(sizeof(struct Elem));
- scanf("%d ", &x);
- cur->v = x;
- cur->prev = now;
- now->next = cur;
- now = cur;
- }
- if (i == n - 1) {
- struct Elem *cur = (struct Elem*)malloc(sizeof(struct Elem));
- scanf("%d", &x);
- cur->v = x;
- cur->prev = now;
- now->next = cur;
- cur->next = l;
- }
- }
- free(now);
- struct Elem *f, *g;
- f = l->next;
- for (i = 0; i < n; i++) {
- printf("%d ", f->v);
- f = f->next;
- }
- printf("\n");
- f = l->next;
- while (f->v != l->v) {
- g = f->next;
- free(f);
- f = g;
- }
- free(l);
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment