Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > C/C++ программирование > Общие вопросы C/C++
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 06.06.2009, 16:23   #11
Sazary
В тени
Старожил
 
Аватар для Sazary
 
Регистрация: 19.12.2008
Сообщений: 5,788
По умолчанию

Код:
#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..." );

}
Только вот при выводе он что-то левое выдает (у вас это тоже было). Но список теперь создается.
Вполне очевидно, чтобы что-то понять, необходимо книги читать.
Не нужно плодить бессмысленных тем. Вас Поиск избавит от многих проблем.

___________________________________ ___________________________________ _______
[=Правила форума=]_____[Поиск]_____[Литература по С++]____[Литература. Паскаль]
Sazary вне форума Ответить с цитированием
Старый 06.06.2009, 19:13   #12
ai\ekcah^p
Форумчанин
 
Аватар для ai\ekcah^p
 
Регистрация: 03.05.2009
Сообщений: 112
По умолчанию

Спасибо Вам огромное! Все работает как часики) Наконец-то)
ai\ekcah^p вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Списки C++ paladinn Помощь студентам 1 27.05.2009 12:31
Двусвязные списки Serp Помощь студентам 3 14.04.2009 16:13
списки Влдислаав3911 Паскаль, Turbo Pascal, PascalABC.NET 5 10.05.2008 17:35