Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > กระทู้สำหรับภาษาต่าง ๆ > สอบถามเรื่องการหาค่า Minimum ของ link list c++ ครับ



สอบถามเรื่องการหาค่า Minimum ของ link list c++ ครับ

 
Topic : 119550



โพสกระทู้ ( 0 )
บทความ ( 0 )



สถานะออฟไลน์



สอบถามเรื่องการหาค่า 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
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-10-25 20:38:23 By : bkp1995 View : 2098 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;
}


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2016-12-12 23:50:09 By : Watt Duean
 


   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สอบถามเรื่องการหาค่า Minimum ของ link list c++ ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่