We need a function that simply adds an element to the beginning. I wrote something here, but I still need to do something with a pointer to the previous element, since I have a doubly-linked list, help to remake.

void add(INFO d, BOOK *&head) { BOOK *tmp = new BOOK; tmp->data = d; tmp->next = head; head=tmp; } 
  • @ Alexey Gauba, If you are given a comprehensive answer, mark it as correct (click on the check mark next to the selected answer). - Deleted

2 answers 2

 BOOK *add(INFO d, BOOK *head) // head указатель на первый элемент списка { // для создания нового списка вызвать list = add(d,NULL); // для добавления в существующий list = add(d,list); BOOK *tmp = new BOOK; tmp->data = d; tmp->next = head; tmp->previous = NULL; if (head!=NULL) // список уже существует head->previous = tmp; return tmp; } 
  • Yes, that's better. Only still raises the question of what will happen if new breaks off. :-) - gecube
  • Well, of course it will fall. In a real program, this should be processed in accordance with the meaning of the whole task. It can write a message to the log and end the process, or it can just (of course, without falling) return NULL, etc. - avp
  • @ Alena Nepomnyaschaya: Oh. Would you read a C ++ book, eh? In C ++ - and > together it is understood as one operator -> (arrow), it means “access to a field by a pointer to a structure”. - VladD

This is my implementation of the function for the deck, did quite a long time. Perhaps something will have to think out.

 deck addhead(deck d, int data){ //добавление в голову elem *t=(elem*)malloc(sizeof(elem)); //Выделение памяти под новый элемент t->data=data; //Запись данных в новый элемент t->next=d.head; //Указание следующего элемента - нынешнюю голову t->prev=NULL; //Установка указателя на предыдущий элемент в NULL if(!isempty(d)){ //Если дека не пуста d.head->prev=t; //Указание у нынешней головы предыдущего элемента - нового элемента } d.head=t; //запись в указатель головы указателя на новый элемент return d; //Возвращение деки }