26.05.2014, 13:53. Просмотров 172. Ответов 0
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
| // книги.cpp: определяет точку входа для консольного приложения.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct Node
{
char name [50];
char nameb [50];
int year;
int count;
Node* next;
};
typedef Node *PNode;
Node *Create (int n);
void Sort (Node *Head, int n);
void Show (Node *Head, int n);
int _tmain(int argc, _TCHAR* argv[])
{int n, i;
Node *New = new Node;
cout << "Enter the number of records" << endl;
cin >> n;
Node *Head = Create (n);
Show (Head, n);
Sort (Head, n);
cout << "\n\n";
Show (Head, n);
system ("pause");
return 0;
}
Node *Create (int n)
{Node *Head = new Node;
cout << "\n Enter Name of Author" << endl;
cin >> Head -> name;
cout << "\n Enter name of book\n";
cin >> Head -> nameb;
cout << "\n Enter year\n";
cin >> Head -> year;
cout << "\n Enter count\n";
cin >> Head -> count;
Node *tmp;
tmp = Head;
tmp -> next = new Node;
tmp = tmp -> next;
for (int i = 0; i < n-1; i++)
{cout << "\n Enter Name of Author" << endl;
cin >> tmp -> name;
cout << "\n Enter name of book\n";
cin >> tmp -> nameb;
cout << "\n Enter year\n";
cin >> tmp -> year;
cout << "\n Enter count\n";
cin >> tmp -> count;
tmp -> next = new Node;
tmp = tmp -> next;
}
tmp -> next = new Node;
tmp -> next -> next = new Node;
return Head;
}
void Show (Node *Head, int n)
{Node *tmp = Head;
for (int i = 0; i < n; i++)
{cout << "Author: " << tmp -> name << endl;
cout << "Name of book: " << tmp -> nameb << endl;
cout << "Year: " << tmp -> year << endl;
cout << "Count: " << tmp -> count << endl << endl;
tmp = tmp -> next;
}
}
void Sort (Node *Head, int n)
{Node *tmp1 = Head;
Node *tmp2 = Head;
Node *tmp = new Node;
tmp2 = tmp2 -> next;
for (int i = 0; i < n-1; i++)
{
for (int j = 1; j< n; j++)
{if ((tmp1 -> year) > (tmp2 -> year))
{tmp = tmp1;
tmp1 = tmp2;
tmp2 = tmp;
}
if(tmp2 -> next != NULL)
tmp2 = tmp2 -> next;
}
if(tmp1 -> next != NULL)
tmp1 = tmp1 -> next;
}
} |
|
Помогите решить задачу, не удается сортировка списка(функция Sort) по возрастанию года(поле year)