CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Threaded View

  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.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured