สอบถามเรื่องการหาค่า Minimum ของ link list c++ ครับ ผมต้องการหาค่าต่ำสุดของ link list ซึ่งผมสามารถทำค่าสูงสุดได้แล้วแต่เปลี่ยนเป็นค่าต่ำสุดไม่ได้เพราะมันออกมาเป็น 0 ตลอดเลยครับ และถ้าผมต้องการให้แสดงผลตัวเลขที่รับไปจากน้อยไปมากต้องแก้ตรงไหนครับ
Code
void max()
{
int max=0;
current = head;
while(current != NULL)
{
max=current->info>max;
current = current->link;
}
printf("Maximum=%d\n",max); ค่าตรงนี้ออกมาตรงครับเป็นค่าสูงสุด
}
void min()
{
int min=0;
current = head;
while(current != NULL)
{
min=current->info<min;
current = current->link;
}
printf("Minimum=%d\n",min); ผลออกมาเป็น 0 ตลอดเลยครับควรแก้ค่าไหนครับ
}
รบกวนผู้รู้บอกทีครับว่าผมควรเปลี่ยนโค้ดตรงไหนครับ
อันนี้โค้ดทั้งหมดของโปรแกรมครับ Code (C#)
#include <stdio.h>
//Defind structure
typedef struct list
{
int info;
struct list *link;
}node;
node *head, *null, *current, *temp,*pred;
int ShowMenu();
void INSERT_FN();
int _FindMax();
int Affter(int y);
void DisplaySingly(); //--
void CreateSingly(int Number); //--
void InsertFronSingly(int x); //--
void insertRearSingly(int y); //--
void insertMiddleSingly(int _info, int _Affter); //--
void DeleteSingly(int D);
void average();
void _Sum();
void max();
void min();
int main()
{ int Menu;
do
{ int A;
int D;
Menu = ShowMenu();
if(Menu == 1)
{
// Call Insert Function
INSERT_FN();
}
else if (Menu == 2)
{
printf("Delete:");
scanf("%d",&A);
DeleteSingly(A);
}
else if (Menu == 3)
{
DisplaySingly();
}
else if (Menu == 4)
{
_Sum();
}
else if (Menu == 5)
{
average();
}
else if (Menu == 6)
{
max();
}
else if (Menu == 7)
{
min();
}
else if (Menu == 8)
{
printf("\nExit\n");
}
}
while (Menu != 8);
}
int ShowMenu()
{ int _Menu;
printf("\nPlease select menu\n");
printf(" Insert --> 1\n");
printf(" Delete --> 2\n");
printf(" Display --> 3\n");
printf(" Sum --> 4\n");
printf(" Average --> 5\n");
printf(" Maximum --> 6\n");
printf(" Minimum --> 7\n");
printf(" Exit --> 8\n");
printf("Select : ");
scanf("%d",&_Menu);
return _Menu;
}
void DisplaySingly (void)
{
if (head == NULL)
printf("Empty List\n");
else
{
current = head;
while(current != NULL)
{
printf("%d\n",current->info);
current = current->link;
}
}
}
//InsertFunction
void INSERT_FN()
{
int Y;
printf("Insert : ");
scanf("%d",&Y);
if(head == null)
{
CreateSingly(Y);
}
else if(Y <= head->info)
{
InsertFronSingly(Y);
}
else if(Y >= _FindMax())
{
insertRearSingly(Y);
}
else
{
insertMiddleSingly(Y,Affter(Y));
}
}
void CreateSingly(int Number)
{
if (head == NULL)
{
head = new node;
head->info = Number;
head->link = NULL;
}
}
void InsertFronSingly(int x)
{
if (head == NULL)
printf("Empty List");
else
{
temp = new list;
temp->info = x;
temp->link = head;
head = temp;
}
}
int _FindMax(void)
{
current = head;
while(current -> link != null)
{
current = current -> link;
}
return current -> info;
}
void insertRearSingly(int y)
{
if(head == NULL)
printf("EMPTY LIST");
else
{
temp = new node;
temp->info = y;
current = head;
while(current->link != NULL)
current = current->link;
temp->link = NULL;
current->link = temp;
}
}
void insertMiddleSingly(int _info, int _Affter)
{
if(head == NULL)
printf("Empty List");
else
{
temp = new node;
temp->info = _info;
current = head;
while(current->info!=_Affter && current->link != NULL)
current = current->link;
temp->link = current->link;
current->link = temp;
}
}
int Affter(int y)
{
current = head;
while((y<current->info) or (y>current -> link -> info))
{
current = current -> link;
}
return current -> info ;
}
void DeleteSingly(int D)
{
int DELETE;
if(head->info==D)
{
current=head;
head=head->link;
DELETE ; current;
}
else
{
pred=head;
current =head->link;
while(current->link!=NULL&& current->info!=D)
{
pred=current;
current=current->link;
}
if(current->info=D)
{
pred->link=current->link;
DELETE ;current;
}
else
printf("NOT FOUND");
}
}
void _Sum()
{
float sum=0;
current = head;
while(current != NULL)
{
sum=sum+current->info;
current = current->link;
}
printf("Sum=%.2f\n",sum);
}
void average()
{
float sum=0;
int counter = 0;
current = head;
while(current != NULL)
{
sum=sum+current->info;
counter++;
current = current->link;
}
printf("average=%.2f\n",sum/counter);
}
void max()
{
int max=0;
current = head;
while(current != NULL)
{
max=current->info>max;
current = current->link;
}
printf("Maximum=%d\n",max);
}
void min()
{
int min=0;
current = head;
while(current != NULL)
{
min=current->info<min;
current = current->link;
}
printf("Minimum=%d\n",min);
}
Tag : Objective-C
ประวัติการแก้ไข 2015-10-25 20:42:04
Date :
2015-10-25 20:38:23
By :
bkp1995
View :
2175
Reply :
1
No. 1
Guest
Code (C#)
#include <iostream>
//Defind structure
typedef struct list {
int info;
struct list* link;
} node;
node *head, *null, *current, *temp, *pred;
int ShowMenu();
void INSERT_FN();
int _FindMax();
int Affter(int y);
void DisplaySingly(); //--
void CreateSingly(int Number); //--
void InsertFronSingly(int x); //--
void insertRearSingly(int y); //--
void insertMiddleSingly(int _info, int _Affter); //--
void DeleteSingly(int D);
void average();
void _Sum();
void max();
void min();
int main()
{
int Menu;
do {
int A;
int D;
switch(Menu = ShowMenu())
{
case 1: INSERT_FN(); break;
case 2: std::cout << "Delete:"; std::cin >> A; DeleteSingly(A); break;
case 3: DisplaySingly(); break;
case 4: _Sum(); break;
case 5: average(); break;
case 6: max(); break;
case 7: min(); break;
}
} while (Menu != 8);
std::cout << "\nExit\n";
return 0;
}
int ShowMenu()
{
int _Menu;
std::cout << "\nPlease select menu\n";
std::cout << " Insert --> 1\n";
std::cout << " Delete --> 2\n";
std::cout << " Display --> 3\n";
std::cout << " Sum --> 4\n";
std::cout << " Average --> 5\n";
std::cout << " Maximum --> 6\n";
std::cout << " Minimum --> 7\n";
std::cout << " Exit --> 8\n";
std::cout << "Select : ";
std::cin >> _Menu;
return _Menu;
}
void DisplaySingly(void)
{
if (head == NULL)
std::cout << "Empty List\n";
else {
current = head;
while (current != NULL) {
std::cout << current->info << std::endl;
current = current->link;
}
}
}
//InsertFunction
void INSERT_FN()
{
int Y;
std::cout << "Insert : ";
std::cin >> Y;
if (head == null) {
CreateSingly(Y);
} else if (Y <= head->info) {
InsertFronSingly(Y);
} else if (Y >= _FindMax()) {
insertRearSingly(Y);
} else {
insertMiddleSingly(Y, Affter(Y));
}
}
void CreateSingly(int Number)
{
if (head == NULL) {
head = new node;
head->info = Number;
head->link = NULL;
}
}
void InsertFronSingly(int x)
{
if (head == NULL)
std::cout << "Empty List";
else {
temp = new list;
temp->info = x;
temp->link = head;
head = temp;
}
}
int _FindMax(void)
{
current = head;
while (current->link != null) {
current = current->link;
}
return current->info;
}
void insertRearSingly(int y)
{
if (head == NULL)
std::cout << "EMPTY LIST";
else {
temp = new node;
temp->info = y;
current = head;
while (current->link != NULL)
current = current->link;
temp->link = NULL;
current->link = temp;
}
}
void insertMiddleSingly(int _info, int _Affter)
{
if (head == NULL)
std::cout << "Empty List";
else {
temp = new node;
temp->info = _info;
current = head;
while (current->info != _Affter && current->link != NULL)
current = current->link;
temp->link = current->link;
current->link = temp;
}
}
int Affter(int y)
{
current = head;
while ((y < current->info) or (y > current->link->info)) {
current = current->link;
}
return current->info;
}
void DeleteSingly(int D)
{
int DELETE;
if (head->info == D) {
current = head;
head = head->link;
DELETE;
current;
} else {
pred = head;
current = head->link;
while (current->link != NULL && current->info != D) {
pred = current;
current = current->link;
}
if (current->info = D) {
pred->link = current->link;
DELETE;
current;
} else
std::cout << "NOT FOUND";
}
}
void _Sum()
{
int sum = 0;
current = head;
while (current != NULL) {
sum = sum + current->info;
current = current->link;
}
std::cout << "Sum=" << sum << std::endl;
}
void average()
{
float sum = 0;
int counter = 0;
current = head;
while (current != NULL) {
sum = sum + current->info;
counter++;
current = current->link;
}
std::cout << "average=" << sum / counter << std::endl;
}
void max()
{
int max = 0;
current = head;
for (current = head->link, max = head->info; current; current = current->link)
if (current->info > max)
max = current->info;
std::cout << "Maximum=" << max << std::endl;
}
void min()
{
int min;
for (current = head->link, min = head->info; current; current = current->link)
if (current->info < min)
min = current->info;
std::cout << "Minimum=" << min << std::endl;
}