this is a basic linklist,there seems to be a runtime error when i run the program(the main function) i suspect it has something to do with my del function
but i see nothing wrong can you help.Thanx
but i see nothing wrong can you help.Thanx
Code:
#include <iostream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;
class linklist
{
private:
struct node
{
char data;
int line;
node *link;
}*p;
public:
linklist();
void append( char num ,int line);
void add_as_first( char num,int line );
void addafter( int c, char num,int line );
void del( char num );
void display();
char charat(int x);
int lineat(int x);
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(char num,int line)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = num;
p->line=line;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->line=line;
t->link = NULL;
q->link = t;
}
}
void linklist::add_as_first(char num,int line)
{
node *q;
q = new node;
q->data = num;
q->line=line;
q->link = p;
p = q;
}
void linklist::addafter( int c, char num,int line)
{
node *q,*t;
int i;
for(i=0,q=p;i<c;i++)
{
q = q->link;
if( q == NULL )
{
cout<<"\nThere are less than "<<c<<" elements.";
return;
}
}
t = new node;
t->data = num;
t->line=line;
t->link = q->link;
q->link = t;
}
void linklist::del( char num )
{
node *q,*r;
q = p;
if( q->data == num )
{
p = q->link;
delete q;
return;
}
r = q;
while( q!=NULL )
{
if( q->data == num )
{
r->link = q->link;
delete q;
return;
}
r = q;
q = q->link;
}
cout<<"\nElement "<<num<<" not Found.";
}
void linklist::display()
{
node *q;
cout<<endl;
for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data<<'\n'<<q->line;
}
int linklist::lineat(int x){
node *q;
int num;
q=p;
for(int i=0;i<=x;i++){
num=q->line;
q=q->link;
}
return num;
}
char linklist::charat(int x){
node *q;
char num;
q=p;
for(int i=0;i<=x;i++){
num=q->data;
q=q->link;
}
return num;
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
linklist::~linklist()
{
node *q;
if( p == NULL )
return;
while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
int main(){
int x;
linklist l;
l.append('1',1);
l.append('2',2);
l.append('5',10);
l.del('2');
l.display();
cout<<l.lineat(2);
cin>>x;
}
Comment