0 / 0 / 0
Регистрация: 18.01.2013
Сообщений: 4
1

Создание и сортировка связного списка

18.01.2013, 15:16. Показов 1728. Ответов 5
Метки нет (Все метки)

Задание: Написать программу, реализующую связный список с информацией о сотрудниках и отображающую список в порядке возрастания возраста сотрудника

Вот мой код,почему-то не работает,ошибок не выдает,при запуске тут же закрывается
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
typedef struct tag_table
        {
        int number;
        char familia[4];
        int age;
        } TABLE;
 
 
typedef struct tag_obj
        {
        TABLE table;
        TABLE *next,*prev;
        } OBJ;
     OBJ *head=NULL;
     OBJ *tail=NULL;
OBJ* add_obj(int number,char* familia,int age)
     {
        OBJ* current= (*OBJ)malloc(sizeof(OBJ));
        current->table.number=number;
        strcpy(current->table.familia,familia);
        current->table.age=age;
        current->prev=tail;
        current->next=NULL;
     if(tail!=NULL)
        tail->next=current;
     if(head==NULL)
        head=current;
        tail=current;
        return current;
        }
 
OBJ* del_obj(OBJ* current)
        {
    if(current == head)
        {
        if(current->prev != NULL)
 
        head = current->prev;
        else
        head = current->next;
        }
    if(current == tail)
        {
        if(current->next != NULL)
        tail = current->next;
        else
        tail = current->prev;
        }
if(current->prev != NULL)
   current->prev->next = current->next;
if(current->next != NULL)
   current->next->prev = current->prev;
free(current);
return head;
}
 
 class AGECompare{
public:
    bool operator() (const AGE *first, const AGE *second)
    {
         return first->age < second->age;
    }
    bool operator() (const AGE &first, const AGE &second)
    {
        return first.age < second.age;
    }
 
} compare;
 
 
 
        int main()
{
 
  OBJ *current = NULL;
  int number;
  char familia[100];
  int age;
  do
{
printf("Vvedite poryadkovyi nomer: ");
scanf("%d",&number);
printf("Vvedite familiu: ");
scanf("%s",familia);
printf("Vvedite vozrast: ");
scanf("%d",&age);
current = add_obj(number, familia, age);
printf("Exit- 'q'");
} while(scanf("%d", &age) == 1);
current = head;
 
while(current != NULL)
{
printf("number= %d, familia %s, age = %d\n",
current->table.number, current->table.familia, current->table.age);
current = current->next;
}
while(head != NULL)
del_obj(head);
return 0;
 
}
0
Programming
Эксперт
94731 / 64177 / 26122
Регистрация: 12.04.2006
Сообщений: 116,782
18.01.2013, 15:16
Ответы с готовыми решениями:

Создание двойного связного списка целых чисел, вводимых с клавиатуры; печать списка
Люди помогите, нужно сдать последнюю Лабу. Задача: Написать программу которая создает двойной...

Сортировка связного списка
Привет всем! пришлите пожалуйста код реализации сортировки односвязного списка (желательно с...

Сортировка связного списка
Привет всем! как правильно написать сортировку для связного циклического списка ? помогите...

Быстрая сортировка связного списка
Здравствуйте. не пойму как должна заканчиваться функция.что передавать в рекурсию и до каких пор....

5
Twilight Parasite
154 / 150 / 7
Регистрация: 21.07.2011
Сообщений: 908
18.01.2013, 15:19 2
Ann3ooo, а где остальные классы?
0
0 / 0 / 0
Регистрация: 18.01.2013
Сообщений: 4
18.01.2013, 15:22  [ТС] 3
Invader_Zim, а разве нельзя просто структурами?я очень плохо разбираюсь в программировании и поэтому прошу помощи((
0
Twilight Parasite
154 / 150 / 7
Регистрация: 21.07.2011
Сообщений: 908
18.01.2013, 15:28 4
Ann3ooo, ну структуры. Где весь код? и добавь system("Pause") перед return в main
0
0 / 0 / 0
Регистрация: 18.01.2013
Сообщений: 4
18.01.2013, 16:03  [ТС] 5
Invader_Zim, system("Pause") добавила,ничего не меняется, пробую установить другой компилятор может в нем дело?
это был весь код!
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//---------------------------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <string>
#include <stdio>
#include<conio>
 
//---------------------------------------------------------------------------
 
 
typedef struct tag_table
        {
        int number;
        char familia[100];
        int age;
        } TABLE;
 
 
typedef struct tag_obj
        {
        TABLE table;
        TABLE *next,*prev;
        } OBJ;
     OBJ *head=NULL;
     OBJ *tail=NULL;
OBJ* add_obj(int number,char* familia,int age)
     {
        OBJ* current= (*OBJ)malloc(sizeof(OBJ));
        current->table.number=number;
        strcpy(current->table.familia,familia);
        current->table.age=age;
        current->prev=tail;
        current->next=NULL;
     if(tail!=NULL)
        tail->next=current;
     if(head==NULL)
        head=current;
        tail=current;
        return current;
        }
 
OBJ* del_obj(OBJ* current)
        {
    if(current == head)
        {
        if(current->prev != NULL)
 
        head = current->prev;
        else
        head = current->next;
        }
    if(current == tail)
        {
        if(current->next != NULL)
        tail = current->next;
        else
        tail = current->prev;
        }
if(current->prev != NULL)
   current->prev->next = current->next;
if(current->next != NULL)
   current->next->prev = current->prev;
free(current);
return head;
}
 
 class AGECompare{
public:
    bool operator() (const AGE *first, const AGE *second)
    {
         return first->age < second->age;
    }
    bool operator() (const AGE &first, const AGE &second)
    {
        return first.age < second.age;
    }
 
} compare;
 
 
 
        int main()
{
 
  OBJ *current = NULL;
  int number;
  char familia[100];
  int age;
  do
{
printf("Vvedite poryadkovyi nomer: ");
scanf("%d",&number);
printf("Vvedite familiu: ");
scanf("%s",familia);
printf("Vvedite vozrast: ");
scanf("%d",&age);
current = add_obj(number, familia, age);
printf("Exit- 'q'");
} while(scanf("%d", &age) == 1);
current = head;
 
while(current != NULL)
{
printf("number= %d, familia %s, age = %d\n",
current->table.number, current->table.familia, current->table.age);
current = current->next;
}
while(head != NULL)
del_obj(head);
system("pause");
return 0;
 
}
 
 
 
//---------------------------------------------------------------------------
Добавлено через 25 минут
нашла ошибку в 28 строке
C++
1
 OBJ* current= (*OBJ)malloc(sizeof(OBJ));
поменять на
C++
1
 OBJ* current= (OBJ*)malloc(sizeof(OBJ));
И выдал целый ряд ошибок: [C++ Error] Unit1.cpp(36): E2034 Cannot convert 'tag_obj *' to 'tag_table *'
[C++ Error] Unit1.cpp(49): E2034 Cannot convert 'tag_table *' to 'tag_obj *'
[C++ Error] Unit1.cpp(61): E2316 'next' is not a member of 'tag_table'
[C++ Error] Unit1.cpp(63): E2316 'prev' is not a member of 'tag_table'
0
2 / 2 / 0
Регистрация: 03.03.2013
Сообщений: 37
06.05.2013, 08:03 6
можете помочь с таким же заданием
0
IT_Exp
Эксперт
87844 / 49110 / 22898
Регистрация: 17.06.2006
Сообщений: 92,604
06.05.2013, 08:03
Помогаю со студенческими работами здесь

Сортировка пузырьком связного списка
Доброго времени суток, надеюсь на вашу помощь в понимании проблемы при сортировке пузырьком...

Создание связного списка
нужно создать связной список, что собственно уже сделал. что нужно: -функции: -root...

Сортировка связного списка
Проставить сложность для алгоритмов сортировки связного списка и дать ответ на два вопроса:...

Сортировка связного списка
Помогите написать сортировку связного списка по мере добавления в него элементов


Искать еще темы с ответами

Или воспользуйтесь поиском по форуму:
6
Ответ Создать тему
Опции темы

КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin
Copyright ©2000 - 2023, CyberForum.ru