/**********************************************\ * Firtst laborators work at C++ * * Dynamic structeres in memory /lists/ * \**********************************************/ #include #include #include struct TList { int data; struct TList *next,*prew; }; typedef struct TList ttlist; ttlist *head, *work1, *work2, *work3; //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void print_list() { cout << "------------------------------\n"; cout << "Now list is: \n"; work1 = head; while (work1->next != NULL) { cout << " digit: " << work1->data << "\n"; work1 = work1->next; } cout << " digit: " << work1->data << "\n"; cout << "------------------------------\n"; } //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void ins_member(int data) { work1 = head; if (work1->data > data) { work2 = (ttlist *)malloc (sizeof(ttlist)); if (work2 == NULL) { cout << "Not enough memory to allocate buffer\nProgramm halted...\n"; exit(1); } work2->prew = NULL; work2->next = head; head->prew = work2; head = work2; head->data = data; cout << "Data inserted succesfully!..\n\n"; } else { while ((work1->data < data) && (work1->next != NULL)) work1 = work1->next; if (work1->data > data) { work2 = work1->prew; work3 = (ttlist *)malloc (sizeof(ttlist)); if (work3 == NULL) { cout << "Not enough memory to allocate buffer\nProgramm halted...\n"; exit(1); } work3->data = data; work2->next = work3; work3->prew = work2; work1->prew = work3; work3->next = work1; cout << "Data inserted succesfully!..\n\n"; } else { work2 = (ttlist *)malloc (sizeof(ttlist)); work2->next=NULL; if (work2 == NULL) { cout << "Not enough memory to allocate buffer\nProgramm halted...\n"; exit(1); } work2->data = data; work1->next = work2; work2->prew = work1; cout << "Data inserted succesfully!..\n\n"; } } } //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ int del_member(int data) { int err = -1; work1 = head; if (work1->data == data) { head = work1->next; head->prew = NULL; free(work1); err = 0; } else { while ((work1->data != data) && (work1->next != NULL)) work1 = work1->next; if (work1->next != NULL) { work2 = work1->prew; work3 = work1->next; work2->next = work3; work3->prew = work2; free(work1); err = 0; } else if (work1->data == data) { work2 = work1->prew; work2->next = NULL; free(work1); err = 0; } } return(err); } //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void create_list() { head = (ttlist *)malloc (sizeof(ttlist)); if (head == NULL) { cout << "Not enough memory to allocate buffer\nProgramm halted...\n"; exit(1); } head->next = NULL; head->prew = NULL; } //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ int cmpstr(char *str1, char *str2) { int i = 0; while ((str1[i] == str2[i]) && (str1[i] != 0) && (str2[i] != 0)) i++; if ((str1[i] == 0) && (str2[i] == 0)) return(1); else return(0); } //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ //////////////////////////////////*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ void main() { cout << "als Software /10 sep 1998/ \n"; cout << "Copyrght by Alex L. Sterinovich\n"; cout << "ÑÏÈÑÊÈ È ÄÈÍÀÌÈ×ÅÑÊÎÅ ÐÀÑÏÐÅÄÅËÅÍÈÅ ÏÀÌßÒÈ\n\n"; cout << "To insert new member type command 'ins ',\n"; cout << "to remove member type command 'del ',\n"; cout << "to view list type command 'view',\n"; cout << "to exit program type command 'exit'.\n\n"; int i; create_list(); cout << "Enter first data: "; cin >> i; head->data = i; cout << "List was created succesfully!..\n\n\n"; int param; char command[4]; while (!cmpstr(command,"exit")) { cout << "Enter command: "; cin >> command; if (cmpstr(command,"ins")) { cin >> param; ins_member(param); } else if (cmpstr(command,"del")) { cin >> param; if (head->next != NULL) { if (del_member(param) == 0) cout << "Data removed...\n\n"; else cout << "Can't remove data!..\nCan't find this data in list...\n\n"; } else cout << "Can't remove data!..\nList must have at least one element!..\n\n"; } else if (cmpstr(command,"view")) print_list(); else if (!cmpstr(command,"exit")) { cout << "Invalid command!...\n\n"; cout << "To insert new member type command 'ins ',\n"; cout << "to remove member type command 'del ',\n"; cout << "to view list type command 'view',\n"; cout << "to exit program type command 'exit'.\n\n"; } } }