hi,
i have a problem with a code that i wrote for an assignment. if it is possible that you guys could help me then its great. otherwise, please delete this thread admins.
basically the most annoying problem is with the sort function. it takes a link list and it sorts it out by using the "insertion sort" method.
Code:
#include <iostream>
using namespace std;
// structure for the link list
struct Node {
char data;
Node* next;
Node(char d, Node* n);
};
Node::Node(char d, Node* n)
{
data = d;
next = n;
}
// the function that builds the link list
Node* build(char* p)
{
if((*p) == 0)
return NULL;
else
return new Node((*p), build(p+1));
}
//the function that prints the link list
void print(Node* p)
{
cout<<'<';
while(p != NULL)
{
cout<< p->data;
p = p->next;
if(p != NULL)
cout<<'-';
}
cout<<'>';
}
// the function that deletes the link list
void dispose(Node* p)
{
if(p == NULL)
{
delete p;
}
else
{
Node* temp=p;
p = p->next;
delete temp;
dispose(p);
}
}
// the function that does the insertion sort in an ascending order
void sort(Node* &p)
{
int i, j, length=0;
char value;
Node* temp = p;
while(temp != NULL)
{
temp = temp->next;
length++;
}
temp=p;
for(i=1 ; i<length ; i++)
{
j=i;
while(j>0 && p->data > p->next->data)
{
value = temp->next->data;
temp->next->data = temp->data;
temp->data = value;
j--;
}
}
}
// the main function
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
Node* p;
p = build(argv[1]);
print(p);
p = build(argv[1]);
print(p);
sort(p);
print(p);
return 0;
}
Bookmarks