Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // doublespisok.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- struct list
- {
- int v;
- list *next;
- list *prev;
- };
- struct list * init(int a)
- {
- list *lst = (struct list*)malloc(sizeof(struct list));
- lst->v = a;
- lst->next = NULL;
- lst->prev = NULL;
- return(lst);
- }
- list *addelem(list *lst, int v)
- {
- list *curr, *buff;
- curr = (list*)malloc(sizeof(list));
- buff = lst->next;
- lst->next = curr;
- curr->v = v;
- curr->prev = lst;
- curr->next = buff;
- if (buff != NULL)
- buff->prev = curr;
- return(curr);
- }
- struct list *deleteelem(list *lst)
- {
- struct list *prev, *next;
- prev = lst->prev;
- next = lst->next;
- if (prev != NULL)
- prev->next = lst->next;
- if (next != NULL)
- next->prev = lst->prev;
- free(lst);
- return(prev);
- }
- void listprint(list *lst)
- {
- struct list *curr;
- curr = lst;
- do {
- printf("%d ", curr->v);
- curr = curr->next;
- } while (curr != NULL);
- printf("\n");
- }
- void olistprint(list *lst)
- {
- struct list *curr;
- curr = lst;
- while (curr->next != NULL)
- curr = curr->next;
- do
- {
- printf("%d ", curr->v);
- curr = curr->prev;
- } while (curr != NULL);
- printf("\n");
- }
- int main()
- {
- list *p = NULL, *curr=NULL;
- p = init(0);
- curr = p;
- for (int i = 1; i <10; i++) {
- curr = addelem(curr, i);
- }
- listprint(p);
- olistprint(p);
- deleteelem(curr);
- listprint(p);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement