Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct p {
- int data;
- struct p *next;
- struct p *prev;
- } list;
- list* init(int a);
- list* insert_before(list* pc, int num);
- list* start(list* pc);
- list* end(list* pc);
- void print(list* pc);
- int main() {
- list *s, *e;
- int a, n;
- printf("Input root value of list: ");
- scanf(" %d", &a);
- list *head = init(a);
- printf("Input quantity values to insert them before root: ");
- scanf(" %d", &n);
- int *values = (int*)malloc(n * sizeof(int));
- for (int i = 0; i < n; i++) {
- scanf(" %d", &values[i]);
- insert_before(head, values[i]);
- }
- s = start(head);
- e = end(head);
- print(head);
- printf("nnStart address of list: %pnEnd address of list: %pnnstart %dnend %d", s, e, *s, *e);
- return 0;
- }
- list* init(int a) {
- list* root;
- root = (list*)malloc(sizeof(list));
- root->data = a;
- root->next = NULL;
- root->prev = NULL;
- return root;
- }
- list* insert_before(list* pc, int num) {
- list* res, *p;
- res = (list*)malloc(sizeof(list));
- p = pc->prev;
- pc->prev = res;
- res->data = num;
- res->prev = p;
- res->next = pc;
- if (p != NULL) p->next = res;
- return res;
- }
- list* start(list* pc) {
- while (pc->prev != NULL) {
- pc = pc->prev;
- }
- return pc;
- }
- list* end(list* pc) {
- while (pc->next != NULL) {
- pc = pc->next;
- }
- return pc;
- }
- void print(list* pc) {
- int i = 0;
- list* p;
- p = pc;
- do {
- printf("%dn", p->data);
- p = p->prev;
- } while (p != NULL);
- return;
- }
Add Comment
Please, Sign In to add comment