# Thread: linked list....problem

1. ## linked list....problem

1)How to provide the count of all thewords in the list.
pls show source code.

2) to insert a given word before a specified word in the list.
pls show source code.

3)to delete a given word form the list.

4)to list all the words which starts with a specific character, say character'c'.

*the text file has a no. of lines and each line has am integral no. of words. We assume that all the words in the file are in lower case letters.(?? is this mean i have to create a file to store the word i type in the above program..???)

thanks for ur hlp.
from: forever82

2. 1) Do my homework for me

2) Do my homework for me

3) Do my homework for me

4) if (current->word[0] == 'c') <do something>;

gg

3. 1) traverse the list with a while loop and increment a counter

2)transverse the list with a while loop, looking at the data in the next node. Then switch around the pointers... it's not very hard

3)set the node before that word's next pointer to the node after the word. Er... to clarify, if the word is in node 2, set node 1 to point to node 3 and then delete node 2

4) see above

5) See Here

4. ## Re: linked list....problem

Originally posted by Forever82
1)How to provide the count of all thewords in the list.
pls show source code.

2) to insert a given word before a specified word in the list.
pls show source code.

3)to delete a given word form the list.

4)to list all the words which starts with a specific character, say character'c'.

*the text file has a no. of lines and each line has am integral no. of words. We assume that all the words in the file are in lower case letters.(?? is this mean i have to create a file to store the word i type in the above program..???)

thanks for ur hlp.
from: forever82

WHat r u studyin.. high school?? college??

5. im just going to stick some random piece of code here and you can do your own research and pick out what you want.

Code:
```#include <iostream.h>

struct node
{
int id;
node* next;
};

typedef node* nodeptr;

int createNode(int id, nodeptr &nde)
{
if(nde)
return 1;
nde = new node;
nde->id = id;
nde->next = NULL;
return 0;
}

int killNode(nodeptr &nde) //deletes the node and all leading nodes
{
if(nde == NULL)
return 0;
killNode(nde->next);
delete nde;
return 0;
}

node* search(int id, node* nde)
{
if(nde->id==id)
return nde;
if(!nde)
return NULL;
return search(id,nde->next);
}

int countNodes(node* nde)
{
if(!nde)
return 0;
return (1 + countNodes(nde->next));
}

int insertNode(nodeptr &nde1, nodeptr &nde2) //inserts nde2 right after nde1
{
if(nde1 && nde2)
{
nde2->next = nde1->next;
nde1->next = nde2;
return 0;
}
return 1;
}

int main()
{
return 0;
}```
Knock yourself out.
Note: Copied this from a really really really old C++ project for CS AP in High School.

BTW: Please don't ask members to do the work for you, ask for help. No one wants to do someone else's homework.
And before posting again, please read the FAQ
There is a lot of answers to most of the questions you will come across.

Thanks,

-LC

6. Hey Lynux-Penguin, this can be done a lot better
Code:
```int createNode(int id, nodeptr &nde)
{
if(nde)
return 1;
nde = new node;
nde->id = id;
nde->next = NULL;
return 0;
}```
That relies on the calling function to do several things... yuck. I know that you didn't write that as a class, but you could have, and really, maybe should have. Anyway, this code does the same thing, but is better.
Code:
```int createNode(node *headnode)
{
node *current = *headnode;
int id=0;

if(current)
{
while (current->next)		//keep going until current is last node
{
current=current->next;
id++;
}

current->next=new node;
if (current->next)
{
current->next->next=NULL;
current->next->id=id;	//I don't know what you're using id for
//but I left it
return 0;
}
return 1; //failure
}```
If you stick the linked list functions together in a class, it becomes even better. Your next pointers are safe (private members), the list can allocate new nodes for itself, delete portions of itself, and just in general protect itself and clean up when you're done with it. If you decide to program a linked list class, the "this" keyword will be of much use to you (at least it was to me). Here's some code from my llist class.

Code:
```#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

class llist
{
public:
int adddata(unsigned char add);
llist(void);
~llist(void);
void printlist();
private:
unsigned char data;
llist *next;
};

int llist::adddata(unsigned char add)
{
llist *current = this;			//this is a pointer to this instance of the class

while (current->next)			//move current to the last llist in the list
current = current->next;
current->next = new llist;		//create a new llist at the end of the list

if (current->next)				//memory allocation succeeded
{
current->next->data=add;	//store add in the new llist
return EXIT_SUCCESS;
}
else							//memory allocation failed
return EXIT_FAILURE;
}

llist::llist()
{
next=NULL;
data = 0;
}

llist::~llist()
{
if (next)			//use recursion to delete all the llists after this one
delete next;
}

void llist::printlist()
{
llist *current = this;

while (current)
{
cout<<current->data<<endl;	//display current node's data
current=current->next;		//move to next node
}								//rinse and repeat
}```
edit: smileys in my code, lol

7. ## and so the homework is done :>

see subject

Popular pages Recent additions