about implementaton of list

Some times ago, in universty we start study how to create dinamic structures like stack,list and etc.
and this is my queston:
Is STL fully use functonality of these structures, and is my header file well immitate some of the function for list in STL.

My header is:


#include<iostream>

using namespace std;

struct data{
int key;
data* prev;
data* next;
};

struct list{
data* start;

void push_front(int num);
void pop_back(int num);
int print(int index);
int size();
bool empty();
list();
void swap(int index,int index2);
void sort();
data* at(int index);
void clear(int index);

};



data* list::at(int index)
{
data* p=start;

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

return p;
}

void list::clear(int index)
{data* p;data* q;

if(index==0)
{
p=start;
q=start->next;
q->prev=NULL;
delete p;
start=q;

}else if(index==size())
{
p=at(size());
q=p->prev;
q->next=NULL;
delete p;

}else {
p=at(index-1);
q=at(index+1);
data* s=at(index);

delete s;
p->next=q;
q->prev=p;




}






}


list::list()
{start=NULL;

}

void list::sort()
{
for(int i=1;i<size();i++)
{


if(print(i)<print(i-1))
{swap(i,i-1);i=0;}



}



}

void list::push_front(int num)
{ data *p=start;
start= new data;
start->key=num;
start->next=p;
start->prev=NULL;

if(p)
{
p->prev=start;
}

}

void list::pop_back(int num)
{

}

int list::print(int index)
{
data* p=start;
int k=0;
if(index>size())
cout<<endl<<" Greshen index";

do{
if(k==index)
{return p->key;}
else {k++;p=p->next;}


}while(1);


}

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

return sum+1;

}

bool list::empty()
{
if(start)
return 1;
else return 0;

}

void list::swap(int index,int index2)
{
data* p=at(index);

data* q=at(index2);


int ky;
if(index2==0||index==0)
{

ky=p->key;
p->key=q->key;
start->key=ky;

}
else{

ky=q->key;
q->key=p->key;
p->key=ky;

}


}
another question. Sort function works but is it can be implement on another better way
Is STL fully use functonality of these structures, and is my header file well immitate some of the function for list in STL.
Can you rephrase this?

As for sorting, the method you chose is not suitable to use with linked lists, as there is too much jumping between non-adjacent nodes.
This is the best method to sort linked lists (that I know of): http://en.wikipedia.org/wiki/Insertion_sort
In stl have a iterators.
In my header member-function at(index) returns ponter to the structure at this index. And in my program i can use list like a array.
some lke that

#include"list.h"
using namespace std;

void main()
{
list ls;
int num;
cout<<endl<<" Input numbers ";
while(cin>>num)
{
ls.push_front(num);

}




ls.sort();


for(int i=0;i<ls.size();i++)
{
cout<<endl<<ls.print(i);

}


}
Topic archived. No new replies allowed.