have a problem with a linked list
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: have a problem with a linked list

  1. #1
    Join Date
    Sep 2012
    Posts
    6

    have a problem with a linked list

    I copied this program from the book for understanding and it doesn't work
    phone.h
    Code:
    class phone{
    public:
    	char first_name[15],last_name[15];
    	long number;
    	phone *next;
    	static int n;
    	phone(const char *last,const char *first,long num);
    	~phone();
    	void print();
    };
    phone.cpp
    Code:
    #include <iostream>
    #include "phone.h"
    #include <string>
    #include <process.h>
    #include <iomanip>
    using namespace std;
    int phone ::n;
    phone::phone(const char *last,const char *first,long num)
    {
    	if(strlen(last)<15)
    		strcpy(last_name,last);
    	else
    	{
    		cout<<"Error! the last name is too long\n";
    		exit(1);
    	}
    	if(strlen(first)<15)
    		strcpy(first_name,first);
    	else
    	{
    		cout<<"Error!the first name is too long\n";
    		exit(1);
    	}
    	number=num;
    	next=NULL;
    	n++;
    }
    phone::~phone()
    {
    	cout<<"destructing phone number"<<n<<'\n';
    	print();
    	n--;
    }
    void phone::print()
    {
    	cout<<setw(15)<<last_name<<setw(15)<<first_name<<setw(15)<<number<<'\n';
    }
    list.h
    Code:
    #include "phone.h"
    class list
    {
    	phone *head;
    public:
    	list(const char *last,const char *first,long num);
    	~list();
    	void insert(phone *p);
    	phone *find_place(const char *last);
    	phone *find(const char *last);
    	phone *find_last_place();
    	int del(const char *last);
    	void print();
    };
    list.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "phone.h"
    #include "list.h"
    using namespace std;
    
    list::list(const char *last,const char *first,long num)
    {
    	head=new phone(last,first,num);
    }
    phone *list::find_last_place()
    {
    	phone *tmp,*prev;
    	for(tmp=head,prev=head;tmp->next;prev=tmp,tmp=tmp->next);
    	return(prev);
    }
    phone *list::find(const char *last)
    {
    	phone *tmp,*prev;
    	for(tmp=head,prev=head;tmp!=NULL;prev=tmp,tmp->next)
    		if(strcmp(last,tmp->last_name)==0)
    			return(prev);
    	return(prev);
    }
    phone *list::find_place(const char *last)
    {
    	phone *tmp,*prev;
    	for(tmp=head,prev=head;tmp!=NULL;prev=tmp,tmp=tmp->next)
    		if(strcmp(tmp->last_name,last)>=0)
    			return(prev);
    	return(prev);
    }
    void list::insert(phone *p)
    {
    	phone *tmp;
    	//p->last_name the smallest
    	if(strcmp(p->last_name,head->last_name)<0)
    	{
    		tmp=head;
    		head=p;
    		p->next=tmp;
    		return;
    	}
    	//other cases
    	tmp=find_place(p->last_name);
    	p->next=tmp->next;
    	tmp->next=p;
    }
    void list::print()
    {
    	phone *tmp;
    	for(tmp=head;tmp!=NULL;tmp=tmp->next)
    		tmp->print();
    	cout<<"Total number od Objects are"<<head->n<<'\n';
    }
    int list::del(const char *last)
    {
    	phone *place,*tmp;
    	//if the first object id being deleted
    	if(!strcmp(last,head->last_name))
    	{
    		tmp=head;
    		head=head->next;
    		delete tmp;
    		return(1);
    	}
    
    	place=find(last);
    	tmp=place ->next;
    	if(tmp==NULL||strcmp(tmp->last_name,last)!=0)
    		return(0);
    	else
    	{
    		place->next=place->next->next;
    		delete tmp;
    		return(1);
    	}
    }
    list::~list()
    {
    	phone *tmp,*tmpn;
    	for(tmp=head,tmpn=head->next;tmpn!=NULL;tmp=tmpn,tmpn=tmpn->next)
    		delete tmp;
    	delete tmp;
    };
    linkedlist.cpp
    Code:
    // linked list.cpp : Defines the entry point for the console application.
    //
    #include "stdafx.h"
    #include <iostream>
    #include "phone.h"
    #include "list.h"
    using namespace std;
    
    int main()
    {
    	char last[15],first[15];
    	long num;
    	list *ll=new list("Lenon","Jenon",456641);
    	phone *ph1=new phone("Haim","Moshe",345765),*ph2=new phone("Rabin","itzhak",467323),*phone_ptr;
    	ll->insert(ph1);
    	ll->insert(ph2);
    	ll->print();
    	ll->del("Haim");
    	cout<<"AFTER DELETING\N";
    	ll->print();
    	cout<<"input last name first name phone number\n";
    	do
    	{
    		cin>>last>>first>>num;
    		phone_ptr=new phone(last,first,num);
    		ll->insert(phone_ptr);
    	}
    	while(num);
    	ll->print();
    	delete ll;
    	cin.get();
    	return 0;
    }
    and visual studio writes me:
    1>------ Build started: Project: LinkedList, Configuration: Debug Win32 ------
    1> phone.cpp
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.cpp(2): warning C4627: '#include "phone.h"': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.cpp(3): warning C4627: '#include <string>': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.cpp(4): warning C4627: '#include <process.h>': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.cpp(5): warning C4627: '#include <iomanip>': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.cpp(38): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
    1> list.cpp
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\list.cpp(1): warning C4627: '#include <iostream>': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\list.cpp(2): warning C4627: '#include <string>': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\list.cpp(3): warning C4627: '#include "phone.h"': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\list.cpp(4): warning C4627: '#include "list.h"': skipped when looking for precompiled header use
    1> Add directive to 'StdAfx.h' or rebuild precompiled header
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\list.cpp(86): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
    1> LinkedList.cpp
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.h(1): error C2011: 'phone' : 'class' type redefinition
    1> c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.h(1) : see declaration of 'phone'
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\linkedlist.cpp(14): error C2027: use of undefined type 'phone'
    1> c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\phone.h(1) : see declaration of 'phone'
    1>c:\users\alex\documents\visual studio 2010\projects\linkedlist\linkedlist\linkedlist.cpp(14): fatal error C1903: unable to recover from previous error(s); stopping compilation
    1> Generating Code...
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




    I think this book is too old and i will change the book to this:
    http://newdata.box.sk/bx/c/#heading1
    Last edited by MrAlex; September 25th, 2012 at 12:13 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: have a problem with a linked list

    Whatever book you guys are using that is teaching you to put next* inside the class that your linked list contains, please burn it. Burn it with fire.

  3. #3
    Join Date
    Sep 2012
    Posts
    6

    Re: have a problem with a linked list

    and what do you recommend me to do?

    http://newdata.box.sk/bx/c/#heading1
    this booj is okay?

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: have a problem with a linked list

    Quote Originally Posted by MrAlex View Post
    and what do you recommend me to do?

    http://newdata.box.sk/bx/c/#heading1
    this booj is okay?
    Is that the link you're using? That book appears really dated. It's using features that are no longer supported, but here is where it shows how to implement a linked list.
    http://newdata.box.sk/bx/c/htm/ch11.htm#Heading47

  5. #5
    Join Date
    Sep 2012
    Posts
    6

    Re: have a problem with a linked list

    No its other book
    linked lists are still useful?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,427

    Re: have a problem with a linked list

    Quote Originally Posted by MrAlex View Post
    No its other book
    linked lists are still useful?
    Code:
    #include <list>
    What does that line do, and what is <list>?

    It is the standard C++ linked list class, already written, already works.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Apr 1999
    Posts
    27,427

    Re: have a problem with a linked list

    Code:
    #include <list>
    #include <string>
    
    class phone
    {
        public:
            std::string first_name;
            std::string last_name;
            long number;
            phone(const std::string& last,const std::string& first,long num) : first_name(first), last_name(last), number(num)
            { }
    };
    
    int main()
    {
       std::list<phone> PhoneList;
       PhoneList.push_back(phone("Smith", "Joe", 111111));
       PhoneList.push_back(phone("Brown", "Frank", 222222));
       PhoneList.push_back(phone("Jones", "Jill", 333333));
    }
    I just added 3 phone numbers to the linked list. I can easily remove items also. All of that code you have is in this little program, with the exception that it works, compiles, it's standard (it must compile on every single ANSI C++ compiler out there) and is written by industry professionals.

    Regards,

    Paul McKenzie

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,097

    Re: have a problem with a linked list

    I think writing a list is still a good exercise for beginners and every programmer should understand how they're implemented.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center