linked list....problem

This is a discussion on linked list....problem within the C++ Programming forums, part of the General Programming Boards category; 1)How to provide the count of all thewords in the list. pls show source code. 2) to insert a given ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    5

    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. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    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. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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
    Away.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    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. #5
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    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
    Last edited by Lynux-Penguin; 07-19-2003 at 10:09 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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
    Away.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    67

    and so the homework is done :>

    see subject

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21