Код:
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <malloc.h>
using namespace std;
struct Item
{
char itsName[10];
int itsPrice;
int itsCount;
Item* itsNext;
Item* itsPrev;
};
struct List
{
Item* itsFirst;
Item* itsLast;
};
void menu();
void AddItem(List&);
void DeleteItem(List&);
void PrintList(List*);
void CreateList (List*&);
List* theList = 0;
int main()
{
List* itsFirst=0;
List* itsLast=0;
menu();
getche();
return 0;
}
void menu()
{
bool quit = false;
while (true)
{
int choice;
printf("\n ******* MENU *********** \n" );
printf("(1) vvedite spisok \n" );
printf("(2) prosmotr \n");
printf("(3) dobavlenie \n");
printf("(4) udalenie \n");
printf("(5) vyhod \n" );
scanf("%d",&choice);
switch (choice)
{
case(1):
if (!theList)
{
CreateList (theList);
printf(" the List has been created succesfully... \n");
}
else
printf(" the List is already created...\n" );
break;
case(2):
if (theList)
PrintList(theList);
else
printf(" the List is not created...\n");
break;
case(3):
if (theList)
AddItem(*theList);
else
printf(" the List is not created...\n");
break;
case 4:
if (theList)
DeleteItem(*theList);
else
printf(" the List is not created... \n");
break;
case(5):
quit = true;
}
if (quit == true)
break;
}
}
void AddItem(List& theList)
{
printf("\n *** dobavit' novuy tovar *** \n");
printf(" vvedite nazvanie: ");
Item* newItem = new Item;
scanf("%s",&newItem->itsName);
printf(" vvedite cenu: ");
scanf("%f",&newItem->itsPrice);
printf(" vvedite kolichestvo: ");
scanf("%d",&newItem->itsCount);
if (theList.itsLast)
{
theList.itsLast->itsNext = newItem;
newItem->itsPrev = theList.itsLast;
}
else
{
theList.itsFirst = newItem;
newItem->itsPrev = 0;
}
theList.itsLast = newItem;
newItem->itsNext = 0;
printf("*** item was added successfully *** \n");
}
void CreateList (List*& theList)
{
theList = new List;
theList->itsFirst = NULL;
theList->itsLast = NULL;
Item *p,*k=NULL;
do
{
p = new Item;
printf( "*** vvedite tovar *** \n");
printf(" vvedite nazvanie: ");
scanf("%s", p->itsName);
printf( " vvedite cenu: " );
scanf("%f", &p->itsPrice);
printf(" vvedite kolichestvo: ");
scanf( "%d",&p->itsCount);
if(theList->itsFirst==NULL) theList->itsFirst = p;
p->itsNext = NULL;
if(k)
{
p->itsPrev = k;
k->itsNext = p;
}
else
{
p->itsPrev = NULL;
}
k = p;
puts(" Zakonchit' - <esc>");
}
while (getch()!=27);
theList->itsLast=p;
}
void PrintList(List* theList)
{
printf("\n *** prosmotr spiska *** \n");
Item* curItem = theList->itsFirst;
while (curItem)
{
printf("%s\n",curItem->itsName);
printf("%f\n",&curItem->itsPrice);
printf("%d\n",&curItem->itsCount);
curItem = curItem->itsNext;
}
printf("\n *** end *** \n" );
}
void DeleteItem(List& theList)
{
Item* curItem = theList.itsFirst;
int Pos;
printf( " Enter the position of deleted item: \n");
scanf("%d",Pos);
printf("%d",Pos);
for (int i=0; i<Pos ; i++)
{
if (curItem)
curItem = curItem->itsNext;
}
if (curItem && (Pos >= 0))
{
if (curItem->itsPrev)
{
curItem->itsPrev->itsNext = curItem->itsNext;
}
else
{
theList.itsFirst = curItem->itsNext;
}
if (curItem->itsNext)
{
curItem->itsNext->itsPrev = curItem->itsPrev;
}
else
{
theList.itsLast = curItem->itsPrev;
}
delete curItem;
printf("Item ¹ ", Pos ," has been deleted successfully..." );
}
else
printf("Item ¹ ", Pos , " not found..." );
}
Только вот при выводе он что-то левое выдает (у вас это тоже было). Но список теперь создается.