linked lists

Hey , i had written this code to swap the max value with last element in the liked list and to swap the min value with first element in linked list.
the min code it's done but the max is cannot be done . Help me plz


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
  #include<iostream>
using namespace std;
struct node{
	int data;
	node *next;
};


	
int main()
{
	node *head=NULL,*n1=NULL,*n2=NULL,*n3=NULL,*n4=NULL,*n5=NULL;
	n1=new node;
	n2=new node;
	n3=new node;
	n4=new node;
	n5=new node;
	cout<<"please enter the numbers for the link list"<<endl;
	cin>>n1->data;
	n1->next=n2;
	cin>>n2->data;
	n2->next=n3;
	cin>>n3->data;
	n3->next=n4;
	cin>>n4->data;
	n4->next=n5;
	cin>>n5->data;
	n5->next=NULL;
	head=n1;
	int max=n1->data;
	int min=n1->data;
	for(node *loc=n2;loc!=NULL;loc=loc->next)
		{if(loc->data>max){
		max=loc->data;
		n5->data=max;}
	}

	
	for(node *loc=n2;loc!=NULL;loc=loc->next)
	{if(loc->data<min){
	min=loc->data;
	n1->data=min;}
	}
	
	for(node *loc=head;loc!=NULL;loc=loc->next)
		cout<<loc->data<<endl;
	
	
	
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.