min and max element is list

Pages: 12
Hello!
How can i find minimal and maximal element in list?
I think i must start with function for sorting but how to put in dynamic initialization without STL?

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
  #include<iostream>

using namespace std;

void init();
void add_b(int n);
void list();




struct elem {
	int key;
	elem *next;
} start, p, *q;

elem* at(int index);
void init() {
	start = NULL;
}
void add_b(int n) {
	p = start;
	start = new elem;
	start->key = n;
	start->next = p;
}
void list() {
	p = start;
	cout << "The List is: ";
	while (p) {
		cout << p->key << " ";
		p = p->next;
	}
	cout << endl << endl;
}

elem* at(int index)
{
	p = start;

	while (index)
	{
		p = p->next;
		index--;
	}

	return p;
}

int size(){
	p = start;
	int sum = 0;
	while (p->next != NULL)
	{
		sum++;
		p = p->next;
	}

	return sum + 1;

}



int main()
{
	int n;
	init();
	cout << "Insert elements :\n";
	while (cin >> n && n > 0) {
		add_b;
	}
	
	list();
	
	return 0;
} 
Last edited on
first make your code compile
then create a list correctly
then worry about operating on the list.

sorting to find the maximum and minimum elements is overkill
think how you'll do it in real life
example program:
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
#include <iostream>
using namespace std;

struct st{
	int k=0;
	st *next= nullptr;
};

int main(){
	int ar[5]={4,6,9,12,2}; 
	st *head= new st;
	st *temp= head;
	for(int i=0; i<5; i++){		
		temp->next= new st;
		temp->k=ar[i];
		temp= temp->next;
	}
	
	temp=head;
	cout<< "all elements= ";
	do{
		cout<< temp->k << ", ";
		temp= temp->next;
	}while(temp->next);
	
	
	temp= head;
	int max= temp->k;;
	do{
		if(temp->k > max)
			max= temp->k;
		temp= temp->next;		
	}while(temp->next);	
	
	cout<< "\nmax value= " << max << "\n";
	
	//delete allocated memory
}
Sorry, too complex for me, I suggest something simple:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	unsigned ar_size=5, ar_min=0, ar_max=0;
	int ar[ar_size] = {4, 6, -9, 12, 2};

	for (unsigned i=1; i < ar_size; i++)
	{
		if (ar[ar_min] > ar[i])
		    ar_min = i;
		if (ar[ar_max] < ar[i])
		    ar_max = i;
	}
	
	cout << "\nmin value = " << ar[ar_min];
	cout << "\nmax value = " << ar[ar_max];
}
@MikeStgt, op need to find max value from linked list. not that i created linked list to find max value from a normal array. lol
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	int ar[]={1,3,5,9,14,2};
	cout << "max value= " << *max_element(ar,ar+6);
	
return 0;	
}
And *max_element(ar,ar+6) for arrays does not work also for linked lists?
no afaik
no afaik

Hard cheese. That's C++. It is what it is.
>
Hard cheese
?
*max_element will not directly work on my code above. but i said
afaik
because there could be complicated ways to make it work, that i don't know.
also there are STL containers such as list(doubly linked list) & forward_list(singly linked list).
I think there's some weird stuff being lost in translation here. Anup was just answering your question, Mike.
there is "little non-harming bug" in my first code. it uses 'new' once more than needed, and last new is unused.
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
#include <iostream>
using namespace std;

struct st{
	int k=100;
	st *next= nullptr;
};

int main(){
	int ar[3]={1,2,3}; 
	st *head= new st;
	st *temp= head;
	for(int i=0; i<3; i++){
		temp->k=ar[i];
		temp->next= new st;
		temp= temp->next;
	}
	
	temp=head;
	cout<< "all elements= ";
	do{
		cout<< temp->k << ", ";
		temp= temp->next;
	}while(temp->next);
	
	
	temp= head;
	int max= temp->k;;
	do{
		if(temp->k > max)
			max= temp->k;
		temp= temp->next;		
	}while(temp->next);	
	
	cout<< "\nmax value= " << max << "\n";
	
	//delete allocated memory	
	temp=head->next; 
	do{ ///
		cout<< "\ndeleting " << head->k;
		delete head;
		head=temp;
		temp=temp->next;
	}while(temp);
	cout << "\n" << head->k <<"\n"; // one element left
	delete head; // all deleted
 	
return 0;
}
Xin lỗi, quá phức tạp đối với tôi
Thank you very much :)
> And *max_element(ar,ar+6) for arrays does not work also for linked lists?
rtfm
http://www.cplusplus.com/reference/algorithm/min_element/ requires forward iterators
http://www.cplusplus.com/reference/iterator/ForwardIterator/ implemet oprations of comparison, increment and dereference
anup30 wrote:
*max_element will not directly work on my code above. but i said
afaik
because there could be complicated ways to make it work, that i don't know.

Not really "complicated". The algorithm expects ForwardIterators.
In the plain array case you have raw pointers, which do behave like ForwardIterators.
http://www.cplusplus.com/reference/iterator/ForwardIterator/

If you write a linked list, then you can write a helper iterator for it too.
That is no more complicated than writing the list. In fact, having the iterator might
simplify the code of some list operations.

The obvious reason to write a linked list is to practice programming and writing both a list and an iterator for it is surely "better practice"?
@Ganado
Anup was just answering your question, Mike.

Did I miss something? Let me explain:
I translated Anup's reply 'no afaik' as "No, as far as I know." My reaction to this was 'hard cheese', what I meant as "what a pity" or "bad luck" or "hard lines" or "too bad".
And (for me still a C++ novice) it is too bad in two ways: i) if there really isn't something like *max_element(ar,ar+6) for linked lists, and ii) if there is something similar but stays unknown, what shows a poor documentation, or at least one not helpful for "fast forward" to get results ASAP.

I think there's some weird stuff being lost in translation here.

Something else I missed?
Getting results ASAP:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <forward_list>
#include <algorithm>

int main ()
{
  std::forward_list<int> mylist = { 34, 77, 16, 2, 7 };
  auto result = minmax_element( mylist.begin(), mylist.end() );
  std::cout << "min is " << *result.first;
  std::cout << " max is " << *result.second << '\n';
}

Documented linked list. Documented algorithm.


The gist, however, is in the "result". OP wrote: "without STL" as in "I want to learn something, but it ain't the use of C++."
"without STL"
So here we have the suspected 'lost in translation' -- please, what is STL?

Edit: thank you for the ASAP result :)
Last edited on
"STL" should refer to https://en.wikipedia.org/wiki/Standard_Template_Library
but here more likely to "most of the C++ Standard Library". Just guessing.
Just guessing.
Much more than I found out.

But I will not complain, I learned my first complete clause Vietnamese:
http://www.cplusplus.com/forum/beginner/253331/#msg1114177
Sorry, too complicated for me
Pages: 12