Send string value to linklist
Hi all, Please can any one help me I am tring to edit this programe to insert string
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class linklist
{
private:
struct node
{
char data; node *link;
}*p;
public:
linklist();
void append( char num ); void add_as_first( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(char num)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}
void linklist::add_as_first(int num)
{
node *q;
q = new node;
q->data = num;
q->link = p;
p = q;
}
void linklist::addafter( int c, int num)
{
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->link = q->link;
q->link = t;
}
void linklist::del( int 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;
for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;
}
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;
}
}
void main()
{
linklist ll;
ll.append('A');
ll.append('B');
ll.append('C');
ll.display();
cout<<"\nNo. of elements = "<<ll.count();
getch();
}
if I change char data; to char data[30] i get error massege say
error C2440: '=' : cannot convert from 'char' to 'char [30]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
error C2440: '=' : cannot convert from 'char' to 'char [30]'
There are no conversions to array types, although there are conversions to references or pointers to arrays
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class linklist
{
private:
struct node
{
char data[30];
node *link;
}*p;
public:
linklist();
void append( char num[30] );
void add_as_first( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(char num[30])
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = num[30];
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num[30];
t->link = NULL;
q->link = t;
}
}
/*
void linklist::add_as_first(int num)
{
node *q;
q = new node;
q->data = num;
q->link = p;
p = q;
}
void linklist::addafter( int c, int num)
{
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->link = q->link;
q->link = t;
}
void linklist::del( int 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;
for( q = p ; q != NULL ; q = q->link )
cout<<endl<<q->data;
}
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;
}
}
void main()
{
linklist ll;
ll.append("Node1");
ll.append("Node2");
ll.append("Node3");
ll.display();
cout<<"\nNo. of elements = "<<ll.count();
getch();
}
[6642 byte] By [
Mansur] at [2007-11-11 8:36:41]

# 1 Re: Send string value to linklist
Do not make a function like this:
void foo(char x[10]) as now, only a char[10] will fit.
instead, make it a char *
void foo(char *x, int size); //if you need to know the size, or if its zero terminated or whatnot, leave size out, your design here.
you can still use it this way
char data[30];
foo(data, 30) or foo(data) depending on what you decided to do about the length.
your error is:
ll.append("Node1"); //that string is not 30 in length
jonnin at 2007-11-11 21:01:13 >

# 2 Re: Send string value to linklist
Thank jonnin a lot
I so sorry becouse ask again but, i still not understand.
I am Beginner
you say:
Do not make a function like this:
void foo(char x[10]) as now, only a char[10] will fit.
instead, make it a char *
i understand what you say. hare i have limet length 10 only
void foo(char *x, int size); //if you need to know the size, or if its zero terminated or whatnot, leave size out, your design here.
you can still use it this way
char data[30];
foo(data, 30) or foo(data) depending on what you decided to do about the length.
the change in struct or in function
your error is:
ll.append("Node1"); //that string is not 30 in length
please help me to solve this problem. give me some detail or any site explain like this problem
1- is this struct is correct ? if not what is the wrong ?
class linklist
{
private:
struct node
{
char data[30];
node *link;
}*p;
public:
linklist();
void append( char[] );
~linklist();
};
2- what is the wrong here?
void linklist::append(char num[])
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data= num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->link = NULL;
q->link = t;
}
}
3- what is the wrong here?
void main()
{
linklist ll;
ll.append("Node1");
ll.append("Node2");
ll.append("Node3");
getch();
}
Thanks
Mansur at 2007-11-11 21:02:09 >

# 3 Re: Send string value to linklist
the struct is fine.
change the function.
1) no problem
2) -- This is not what you had. It should be fine now, if it is not, try char * instead of char[] -- I do not remember the differences but for some reason I have abandoned [] for the pointer in my own code.
so if
void linklist::append(char num[])
does not work
do this:
void linklist::append(char *num)
and it should. if it does not, post the new errors.
3) this is fine.
jonnin at 2007-11-11 21:03:13 >

# 4 Re: Send string value to linklist
You cannot assign arrays.
char num [20];
num = "123213"; //nope, cant do it.
you do this in append.
-- t->data = num;
you must use a different way:
strcpy(t->data, num); //this is a special function for copying strings.
you might also simply use a "string"
#include<string>
using namespace std;
string num;
num = "12343"; //ok
t->data = num; //ok now if data and num are both strings...
jonnin at 2007-11-11 21:04:19 >

# 5 Re: Send string value to linklist
C, the language c++ came from, uses headers of the format
#include <header.h>
c++ used this as well for many years, until the standard committee decided to fool with it. Now, the the headers are of the form
#include<header> //no .h on most of them, you just have to memorize which ones
//also, random ones have a C in front (cmath, because math is a
// thing you only do in C, ? instead of math.h)
using namespace std; // use the standard namespace. you will learn about namespaces another day.
None of what you have is "wrong" in that the compiler probably does not care, but this is something you need to learn.
jonnin at 2007-11-11 21:05:12 >

# 6 Re: Send string value to linklist
Hello jonnin
first I would thank you a lot because you read my problem and help me to solve them
I am reading your reply carefully and try to do it, But I stay have problem.
1-so if
void linklist::append(char num[])
does not work do this:
void linklist::append(char *num)
I do that but, the problem stay, then i change the strcut
struct node
{
// char data[30]; // old code
char *data;
node *link;
}*p;
but my problem now if I enter first node "Node1" //this is good
but , after I enter second node "Node2"
now all node will be = "Node2"
2-you do this in append.
-- t->data = num;
you must use a different way:
strcpy(t->data, num); //this is a special function for copying strings.
when I use strcpy I get windows error massege and the program is close.
3-you might also simply use a "string"
#include<string>
using namespace std;
string num;
num = "12343"; //ok
when I write that
i get error massege
error C2871: 'std' : does not exist or is not a namespace
error C2065: 'string' : undeclared identifier
the main problem is
if I enter first node "Node1" ,But after I enter second node "Node2"
now node1 value= "Node2"
and node2 value = "Node2"
Thank again jonnin
note: I am useing Microsoft visual c++ 6
Mansur at 2007-11-11 21:06:22 >

# 7 Re: Send string value to linklist
You only define one pointer to the node thingie. you either have to make a "Next/Previous" system or simply a "double pointer" (2D array).
like this:
struct node
{
// char data[30]; // old code
char *data;
node *link;
}**p;
and then do like this:
p[NUMBER_OF_NODES] = new node;
when you wanna add one to the list.
--EDIT--
Sorry just realized that you actually do try to make a linked list.
you need to do like this (I think) when adding a new:
node *Previous = &p;
p=new node;
p->link = Previous;
or at least something like that.
but remember you have to travel backwards through the array to delete the elements.
--EDIT
Nevermind me, looks like your doing it right after all.. just gonna read it more carefully. sorry.
--EDIT
I still only see the string error.
when you do like this:
char *whatever = anothercharpointer;
you assign the pointer and not the value.
so when you change anothercharpointer you also change the value of whatever.
# 8 Re: Send string value to linklist
this is my code after edit
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
//using namespace std;
class linklist
{
private:
struct node
{
char *data;
node *link;
}*p;
public:
linklist();
void append(char *num );
void del( char *num );
void display();
int count();
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(char *num)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data= num;
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = num;
t->link = NULL;
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.";
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
void linklist::display()
{
node *q;
for( q = p ; q != NULL ; q = q->link )
cout << q->data<< endl;
}
linklist::~linklist()
{
node *q;
if( p == NULL )
return;
while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
void main()
{
linklist ll;
char d[50];
int numx=0;
while(numx != -1)
{
cout << "Enter your chois \n";
cout << "Dsiply nodes (1)\n";
cout << "Add new node Normal (2)\n";
cout << "Delete node (5)\n" ;
cout << "Exit (-1)" ;
cin >> numx;
switch ( numx)
{
case 1:
cout << "============================================\n";
ll.display();
cout << endl;
cout<<"No. of elements = "<<ll.count()<< endl;
cout << "============================================ \n";
break;
case 2:
cout << "Please enter value of the node \n";
gets(d);
//cin >> d;
ll.append(d);
break;
case 3:
cout << "To Delete.Please enter value of the node \n";
gets(d);
ll.del(d);
break;
case -1:
cout << "Press any key to exit \n" ;
break;
default:
cout << "Please enter corect choise" ;
}
}
}
Mansur at 2007-11-11 21:08:19 >

# 9 Re: Send string value to linklist
void linklist::append(char *num)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = new char[255];
strcpy(p->data, num);
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = new char[255];
strcpy(t->data, num);
t->link = NULL;
q->link = t;
}
}
remember to delete the data in the destructer ;)
# 10 Re: Send string value to linklist
Thank ungamed too much and thank for anyone help me in this forum
after last editing to append function it is work;
but,the problem now in del function
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.";
}
it is don't work I am debugging to see q->data and num value the value same but the function don't delete
finally
I so. sorry for too much question but I am try to read from many books and from internet to learn .
Mansur at 2007-11-11 21:10:18 >

# 11 Re: Send string value to linklist
Add this function:
bool DoStuff(char *p1,char *p2) {
while ((*(p1++) == *(p2++)) && (*p1) && (*p2));
return !((*p1) || (*p2));
}
and replace :
if( q->data == num )
with
if (DoStuff(q->data, num))
# 12 Re: Send string value to linklist
hello ungamed :WAVE:
I am editing own code to this .
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
//using namespace std;
class linklist
{
private:
struct node
{
char *data;
node *link;
}*p;
public:
linklist();
void append(char *num );
void del( char *num );
void display();
int count();
bool DoStuff(char *p1,char *p2) ;
~linklist();
};
linklist::linklist()
{
p=NULL;
}
void linklist::append(char *num)
{
node *q,*t;
if( p == NULL )
{
p = new node;
p->data = new char[255];
strcpy(p->data, num);
p->link = NULL;
}
else
{
q = p;
while( q->link != NULL )
q = q->link;
t = new node;
t->data = new char[255];
strcpy(t->data, num);
t->link = NULL;
q->link = t;
}
}
void linklist::del( char *num )
{
node *q,*r;
q = p;
if (DoStuff(q->data, num))
{
p = q->link;
delete q;
return;
}
r = q;
while( q!=NULL )
{
if (DoStuff(q->data, num))
{
r->link = q->link;
delete q;
return;
}
r = q;
q = q->link;
}
cout<<"\nElement "<<num<<" not Found.";
}
bool DoStuff(char *p1,char *p2)
{
while ((*(p1++) == *(p2++)) && (*p1) && (*p2));
return !((*p1) || (*p2));
}
int linklist::count()
{
node *q;
int c=0;
for( q=p ; q != NULL ; q = q->link )
c++;
return c;
}
void linklist::display()
{
node *q;
for( q = p ; q != NULL ; q = q->link )
cout << q->data<< endl;
}
linklist::~linklist()
{
node *q;
if( p == NULL )
return;
while( p != NULL )
{
q = p->link;
delete p;
p = q;
}
}
void main()
{
linklist ll;
char d[50];
int numx=0;
while(numx != -1)
{
cout << "Enter your chois \n";
cout << "Dsiply nodes (1)\n";
cout << "Add new node Normal (2)\n";
cout << "Delete node (5)\n" ;
cout << "Exit (-1)" ;
cin >> numx;
switch ( numx)
{
case 1:
cout << "============================================\n";
ll.display();
cout << endl;
cout<<"No. of elements = "<<ll.count()<< endl;
cout << "============================================ \n";
break;
case 2:
cout << "Please enter value of the node \n";
gets(d);
//cin >> d;
ll.append(d);
break;
case 3:
cout << "To Delete.Please enter value of the node \n";
gets(d);
ll.del(d);
break;
case -1:
break;
default:
cout << "Please enter corect choise" ;
}
}
}
compile is ok
but when I try to bulid it. I get this error massage
Linking...
list.obj : error LNK2001: unresolved external symbol "public: bool __thiscall linklist:: DoStuff(char *,char *)" (?DoStuff@linklist@@QAE_NPAD0@Z)
Debug/list.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
list.exe - 2 error(s), 0 warning(s)
Thank
Mansur at 2007-11-11 21:12:21 >

# 13 Re: Send string value to linklist
because you added it to your class but didnt define it as a member.
Either remove the line from the class or change the function to this:
bool linklist::DoStuff(char *p1,char *p2) {
while ((*(p1++) == *(p2++)) && (*p1) && (*p2));
return !((*p1) || (*p2));
}
# 14 Re: Send string value to linklist
Thanksssssssssssssssssssssssssss
Mansur at 2007-11-11 21:14:27 >
