Как при удалении элемента из списка, полностью его удалить - C (СИ)
Формулировка задачи:
Подскажите, как при удалении элемента из списка, полностью его удалять, чтобы не висел в памяти?
#define _CRT_SECURE_NO_WARNINGS #include <iostream> struct data { char *mas; struct data *next; } *first; struct data *begin=NULL; char *p; int i=1; void qstore(char *buf) { struct data *temp; if((temp=(data*)malloc(sizeof(data))) == NULL) { printf("Memory allocation error!\n"); exit(1); } else { temp->mas=buf; if (begin==NULL) begin=temp; else first->next=temp; first=temp; first->next=NULL; } //free(temp); } void enter() { char s[256]; p=(char*)malloc(sizeof(char)); while(1) { printf("Enter element %d: ", i++); gets(s); if(*s==0) {i--; break;} p=(char*)malloc(strlen(s)+1); if(!p){ printf("\nMemory allocation error!\n"); return; } strcpy(p,s); if(*s) qstore(p); } } void review() { struct data *curr=begin; i=1; while(curr!=NULL) { printf("%d: %s\n", i++,curr->mas); curr=curr->next; } free(curr); } char* qretrieve() { char *temp; if(begin==NULL) { printf("Empty!\n"); return(0); } else { temp=begin->mas; printf("Deleted : %s\n",temp); free(begin->mas); begin=begin->next; return(temp); } free(temp); free(p); } int main() { char s[80]; if((first=(data*)malloc(sizeof(data))) == NULL){ printf("Memory allocation error!\n"); exit(1); } while(1){ printf("Enter (E), List (L), Delete (R), Quit (Q): "); gets(s); *s=toupper(*s); switch(*s) { case 'E': enter(); break; case 'L': review(); break; case 'R': qretrieve(); break; case 'Q': free(first); exit(1); } } return 0; }
Решение задачи: «Как при удалении элемента из списка, полностью его удалить»
textual
Листинг программы
p=(char*)malloc(sizeof(char));
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д