class problem

This is a discussion on class problem within the C++ Programming forums, part of the General Programming Boards category; I cannot get started with this project. Can anyone help? Create a file, link.h that contains the definition of a ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    class problem

    I cannot get started with this project. Can anyone help?
    Create a file, link.h that contains the definition of a struct Node. Should have 2 parts data and link. Your link.h file should containg a prototype of three functions: delrep(), invert() and PRintlinked().
    Create a file, link.cpp with the 3 functions.
    Thedelrep has one paramater a head pointer to a linked list of ints. With a return type of delrep() of void.
    The inver() has one parameter, a head pointer to a linked list of ints.
    The function Printlinked() has one parameter, again a head pointer to a linked list of ints. Return type is void.
    linktest.cpp should contain the main()function. Should create a list, call to print, then reverse the order , again call to print call delrep() to delter the duplicates and then call printlinked again to print again.

    I have my printlinked funtion as:
    void Printlinked(Node*headptr)
    {
    Node*cursor:
    for(cursor_headptr;cursoe !=NULL; cursor+cursor->link)
    cout<<cursor->data<< " ";

    Need help with delrep() and invert() & main.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    well theres nothing hard there. Have a decent go at it and i'll happily help you fix your code. The bit of code u posted looks like it was written by a dyslexic chimpanzee. perhaps if u spend a little time thinking and planning it will all come together for you.
    read this to aid with some of the details of lists if you are unsure how they work.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    class project

    Ok so maybe I am a dyslexic chimpanzee, but I didn't get any help to get started yet!:
    My main :
    void Main()
    {
    int x
    Node* head
    head=NULL
    Node*ptr;

    cout<< "Pleae enter a list of integers, 0 when done: << endl;
    cin x;
    while(x!=0)
    {
    ptr=new Node;
    ptr-> data=x;
    ptr-> link=head;
    head=ptr;
    cin x;
    }
    cout<< endl;
    cout<< "Here is your list of integers: << endl;
    Printlinked(head)
    Invert(head)
    Printlinked(head);
    Delrep(head);
    Printlinked(head);


    Here is my struct:
    STRUCT Node
    {
    int data;
    Node* link;
    }

    I have a poor perception of how to fix my Printlinked so that it doesn't look like a dyslexic chimpanzee, so you'd better tell me.
    Thanks

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    repost code using code tags. read the stickies to see how.

    C++ is case sensitive.

    Main() should be main() and it returns an int not void.
    fix couple other typos like cin x and then ill look at it again.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    int main()
    {
    int x
    Node* head
    head=NULL
    Node*ptr;

    cout<< "Pleae enter a list of integers, 0 when done" << endl;
    cin >>x;
    while(x!=0)
    {
    ptr=new Node;
    ptr-> data=x;
    ptr-> link=head;
    head=ptr;
    cin >>x;
    }
    cout<< endl;
    cout<< "Here is your list of integers: << endl;
    Printlinked(head)
    cout<< "Here is your list of integers in reverse"<<endl;
    Invert(head)
    Printlinked(head);
    cout << "Here is your list of integers with duplicates deleted"<<endl;
    Delrep(head);
    Printlinked(head);


    Here is my struct:
    STRUCT Node
    {
    int data;
    Node* link;
    }

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Originally posted by Stoned_Coder
    repost code using code tags. read the stickies to see how.
    Please listen to Stoned_Coder and use code tags.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    Code:
    int main()
    {
    int x
    Node* head
    head=NULL
    Node*ptr;
    
    cout<< "Pleae enter a list of integers, 0 when done" << endl;
    cin >>x;
    while(x!=0)
    {
    ptr=new Node;
    ptr-> data=x;
    ptr-> link=head;
    head=ptr;
    cin >>x;
    }
    cout<< endl;
    cout<< "Here is your list of integers: << endl;
    Printlinked(head)
    cout<< "Here is your list of integers in reverse"<<endl;
    Invert(head)
    Printlinked(head);
    cout << "Here is your list of integers with duplicates deleted"<<endl;
    Delrep(head);
    Printlinked(head);

    Here is my struct:
    STRUCT Node
    {
    int data;
    Node* link;
    }

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Here are a few linked lists problems and solutions.

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    70
    what exactually is a node?

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    head-->[Data]
    |
    node1-->[Data]
    |
    node2-->[Data]
    |
    node3-->[Data]
    |
    NULL

    A node in a linked list usually contains a pointer to a data class/struct and a pointer to the next element in the list. If the data is as simple as ints, chars, floats, then you don't need a pointer to a data class/struct.

    The node can be a class or a struct.

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    Here is my link.cpp file:
    Code:
    void Delrep(Struct Node* head)
    {
    	Node* current=head;
    	if (current==NULL)            // Do nothing if is it empty
    		return;              
    	while (current->next!=NULL)   // compare current node with next node.
    	{
    		if(curent->data==current->next->data)
    		{
    			Node* nextNext = current->next->next;
    			free(current-> next);
    			current->next=nextNext;
    		}
    		else
    		{
    			current=current->next;    // advances if no deletion
    		}
    	}
    }
    
    
    
    
    
    
    
    void Inverse(Node** head)
    {
    	Struct Node* result=NULL;
    	Struct Node* current= *head;
    	Struct Node* next;
    
    	while(current!=NULL)
    	{
    		next=current->next;              //note the next node
    		current->next = result           //move the node onto the result
    	    result=current;
    		current=next;
    	}
    	*head= result;
    }
    
    
    
    
    
    
    
    void Printlinked(Node* head)
    {
    	Node*cursor;
    	for(cursor=head;cursor !=NULL;cursor=cursor->link)
    		cout <<cursor->data<<" ";
    
    }

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Struct should be"struct", "NULL' should be defined before u use it

    Hope u have a teacher beside u, I advice u choose a good program from a good book, write the program 1000 times in a paper . I bet u will have an impression on what is called C or C++.

    good luck!

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    8
    Thanks all, but I got the program to run good now!

    Heres all my code:

    # include "link.h"
    # include <iostream.h>

    Code:
    void Delrep(Node* &head)
    {
    	Node* current=head,*next=head, *ptr;
    	if (head!=NULL) // Do nothing if is it empty
    		next=head->link;
    	while(next!=NULL)
    	{
    		ptr=head;
    		
    	              
    	while (ptr!=next&&ptr->data!=next->data) // compare current node with next
    		ptr=ptr->link;
    	
    		if(ptr==next)
    		{
    			current->link=next;
    			current=current->link;;
    			next=next->link;    //advances if no deletion
    		}
    		else
    		{
    		    ptr=next->link; // advances if no deletion
    			current->link=next->link;
    			delete next;
    			next=ptr;
    		}
    	}
    }
    
    
    
    
    
    
    
    void Invert(Node*&head)
    {
    	 Node* result,*present=head, *link;
    	 result = NULL;
    	
    
    	while(present!=NULL)
    	{
    		link=present->link;              //note the next node
    		present->link=result;          //move the node onto the result
    	    result=present;
    		present=link;
    	}
    	head= result;
    }
    
    
    
    
    
    
    
    void Printlinked(Node* &head)
    {
    	Node*cursor;
    	for(cursor=head;cursor !=NULL;cursor=cursor->link)
    		cout <<cursor->data<<" ";
    
    }
    # include "link.h"
    # include <stddef.h>
    # include < iostream.h>
    
    
    void main()
    {
    	int x;
    	Node* head;
    	head = NULL;
    	Node* ptr;
    	
    	cout<< "Please enter a list of integers,0 when done"<<endl;
    	cin>>x;
    	while(x!=0)
    	{
        	ptr = new Node;            // Use to dynamically allocate a new node
    		ptr->data=x;             // Set new node=x(input integer)
    		ptr->link=head;
    		head=ptr;
    		cin>>x;
    	}
    	cout<<endl;
    	cout<<"Here is your list of integers"<<endl;
    	Printlinked(head);
    	cout<< "Here is your list of integers in reverse"<<endl;
    	Invert(head);
    	Printlinked(head);
    	cout<< "Here is your list of integers after duplicates have been deleted"<<endl;
    	Delrep(head);
    	Printlinked(head);
    }
    
    # include <stddef.h>
    # include <iostream.h>
    
    struct Node
    {
    	int data;    // to store information
    	Node* link;  // Address to the next Node in a list
    };
    
    
    
    void Delrep(Node* &head);
    void Invert(Node* &head);
    void Printlinked(Node* &head);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mesh Class Design problem
    By sarah22 in forum Game Programming
    Replies: 2
    Last Post: 05-20-2009, 05:52 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  4. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM

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