loading...

Creating Linklist Data Structure in CPP.

coderj001 profile image Raju ・2 min read


using namespace std;
template
class singaly{
private:
struct Node
{
T data;
Node * next;
};
Node * head=NULL, * tail=NULL;
public:
singaly(T value, ...){
va_list arguments;
va_start(arguments, value);
for(int i=0;i this->insert(va_arg(arguments,T));
}
va_end(arguments);
}
void insert(T value){
Node * tmp=new Node;
tmp->data=value;
tmp->next;
if(head==NULL){
head=tmp;
tail=tmp;
}
else{
tail->next=tmp;
tail=tail->next;
}
}
void display(){
Node * tmp=new Node;
tmp=head;
while(tmp!=NULL){
cout<data<<" ";
tmp=tmp->next;
}
cout< }
void sort(){
Node * tmpi=head, * tmpj=NULL;
if(head!=NULL){
while(tmpi!=NULL){
tmpj=tmpi->next;
while(tmpj!=NULL){
if(tmpi->data > tmpj->data){
T tmp=tmpi->data;
tmpi->data=tmpj->data;
tmpj->data=tmp;
}
tmpj=tmpj->next;
}
tmpi=tmpi->next;
}
}
}
};

int main(){
// Code
singaly s=singaly(1);
s.insert(11);
s.insert(2);
// s.sort();
s.display();
cout<<"Memory Size : "< // singaly st=singaly();
// st.insert("This");
// st.insert("is");
// st.insert("Good");
// st.insert("Right");
// st.sort();
// st.display();
// cout<<"Memory Size : "<<sizeof(st)<<endl;
return 0;
}

A linked list is a linear dynamic data structure to store data items. We have already seen arrays in our previous topics on basic C++. We also know that arrays are a linear data structure that store data items in contiguous locations.

Unlike arrays, the linked list does not store data items in contiguous memory locations.

A linked list consists of items called “Nodes” which contain two parts. The first part stores the actual data and the second part has a pointer that points to the next node. This structure is usually called “Singly linked list”.

Discussion

markdown guide