You are here:

# C++/data structure and algorithms

Question
Implement the linked list Node class (which store integer values)in c++.

Dear Ehsan:

Use is code from the following link:
http://www.dreamincode.net/code/snippet82.htm

Use can use it to store integer or whatever structures you want:

#include <iostream>

using namespace std;

{
private:

struct node
{
int data;
}*p;

public:

void append( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
};

{
p=NULL;
}

{
node *q,*t;

if( p == NULL )
{
p = new node;
p->data = num;
}
else
{
q = p;

t = new node;
t->data = num;
}
}

{
node *q;

q = new node;
q->data = num;
p = q;
}

{
node *q,*t;
int i;
for(i=0,q=p;i<c;i++)
{
if( q == NULL )
{
cout<<"nThere are less than "<<c<<" elements.";
return;
}
}

t = new node;
t->data = num;
}

{
node *q,*r;
q = p;
if( q->data == num )
{
delete q;
return;
}

r = q;
while( q!=NULL )
{
if( q->data == num )
{
delete q;
return;
}

r = q;
}
}

{
node *q;
cout<<endl;

for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;

}

{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;

return c;
}

{
node *q;
if( p == NULL )
return;

while( p != NULL )
{
delete p;
p = q;
}
}

int main()
{
cout<<"No. of elements = "<<ll.count();
ll.append(12);
ll.append(13);
ll.append(23);
ll.append(43);
ll.append(44);
ll.append(50);

ll.display();
cout<<"nNo. of elements = "<<ll.count();

ll.del(333);
ll.del(12);
ll.del(98);
cout<<"nNo. of elements = "<<ll.count();
return 0;
}

C++

Volunteer

#### Titus B. Ledbetter, Jr.

##### Expertise

C , MFC, Object Oriented, Artificial Intelligence

##### Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins