usafzz
BAN USER#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
node()
{
next=NULL;
}
node(int x)
{
data=x;
next=NULL;
}
// ~node()
// {
// delete next[];
// }
};
struct stack
{
node *head;
stack()
{
head=NULL;
}
void push(int x)
{
node *n=new node(x);
if(head==NULL)
{
head=n;
}
else
{
n->next=head;
head=n;
}
}
int pop()
{
node *temp=head;
if(head->next!=NULL)
head=head->next;
else
head=NULL;
return temp->data;
}
bool IsEmpty()
{
if(head==NULL)
return true;
else
return false;
}
int top()
{
if(head!=NULL)
{
return head->data;
}
}
void print()
{
node *temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
};
void sort_stack(stack &s)
{
stack A,B;
while(!s.IsEmpty())
{
int tmp=s.pop();
if(A.IsEmpty())
{
A.push(tmp);
continue;
}
if(A.top()<tmp)
{
int mark=0;
while(!A.IsEmpty() && A.top() <tmp)
{
mark=1;
B.push(A.pop());
}
if(mark==1)
{
mark=0;
A.push(tmp);
}
while(!B.IsEmpty())
{
A.push(B.pop());
}
}
else
{
A.push(tmp);
}
}
while(!A.IsEmpty())
{
s.push(A.pop());
}
}
void main()
{
stack s;
s.push(15);
s.push(9);
s.push(2);
s.push(7);
s.push(3);
s.push(11);
s.print();
sort_stack(s);
s.print();
}
works prefectly.....
#include<iostream>
using namespace std;
void print(int arr[], int start,int end)
{
for(int i=start;i<=end;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void swap(int &x,int &y)
{
int temp=x;
x=y;
y=temp;
}
void corr_arr(int arr[], int start, int end)
{
int new_end=end;
if((end-start)%2!=0)
{
new_end=end-1;
}
int mid= start + (new_end-start) /2;
while(mid!=start)
{
if(mid==new_end)
{
mid--;
}
if(mid==start)
break;
swap(arr[mid],arr[new_end]);
new_end--;
}
}
void corr_arr1(int arr[], int start, int end)
{
int check=(end-start)%2;
int new_start=start;
if((end-start)%2!=0)
{
new_start=start+1;
}
int mid= start + (end-new_start) /2 ;
if(check==1)
mid++;
while(mid!=end)
{
if(mid==new_start)
{
mid++;
}
if(mid==end)
break;
swap(arr[mid],arr[new_start]);
new_start++;
}
}
void reorder(int arr [], int size)
{
int arr1_start=0;
int arr1_end=size/2-1;
int arr2_start=size/2;
int arr2_end=size-1;
print(arr,0,size-1);
corr_arr(arr,arr1_start,arr1_end);
print(arr,0,size-1);
corr_arr1(arr,arr2_start,arr2_end);
print(arr,0,size-1);
if((size/2)%2==1)
{
for(int i=1;i<size/2;i+=2)
{
swap(arr[i],arr[i+(size/2)]);
}
}
else
{
int size1=size -2 ;
for(int i=1;i<=size/2;)
{
swap(arr[i],arr[i+(size1/2)]);
i+=2;
}
}
print(arr,0,size-1);
}
void main()
{
int arr[]={0,2,4,6,8,10,1,3,5,7,9,11};
int size=sizeof(arr)/sizeof(int);
reorder(arr, size);
}
as for the negative number addition, a small check is required... check if both are positive or negative simple addition ... if first number is positive number and other is negative than multiple each number in the second link list with -1 ...
a recursive approach can work after that as well as reversing the link list both would be very good approaches ...
1.take a min integer variable and set it to stack.top()
- usafzz May 21, 20082.use another stack to store while you pop out the integers from original stack and check it with min.
3.if the popped integer is less than the integer in min, min=B.top();