help me debug this linked list !

This is a discussion on help me debug this linked list ! within the C Programming forums, part of the General Programming Boards category; I can't find any error in this code but program not run wright. Especially, when i open a file put ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    1

    Unhappy help me debug this linked list !

    I can't find any error in this code but program not run wright.
    Especially, when i open a file put data into linked list and output data on the screen, it 's wrong !
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <dos.h>
    #include <ctype.h>
    typedef struct ngay		//struct ngay
    {
    	int day;
    	int month;
    	int year;
    }NGAY;
    typedef struct nhanvien	//struct nhanvien
    {
    	char name[30];
    	NGAY birthday;
    	float salary;
    	int sex;
    }NHANVIEN;
    typedef struct node	//struct node
    {
    	NHANVIEN info;
    	node * pNext;
    }NODE;
    typedef struct list //struct list
    {
    	NODE* pHead;
    	NODE* pTail;
    }LIST;
    NODE* GetNode(NODE* pNode, NHANVIEN x); //load x into info of node
    void StartList(LIST &l);				//start list
    void Input(LIST &l);					//input data from keyboard-->node--->list
    void AddFirst(NODE* pNode, LIST &l);	//put a node into list
    void ProcessList(LIST l);				//process information of list
    void Output(NODE* pNode);				//display information of node on the screen
    void SearchInList(LIST l);				//search info of node by typing a name of nhanvien
    void EditInList(LIST &l);
    void Menu();
    void OpenFile(LIST &l);
    void SaveFile(LIST &l);
    void Delete(LIST &l);
    void DeleteAllList(LIST &l);
    void Sort(LIST &l);
    
    void main()
    {
    	LIST l;
    	StartList(l);//khoi tao danh sach lien ket don
    	//Menu
    	int x;
    
    
    	do
    	{
    	printf("\n	Menu\n");
    	printf(" 1.Add\n");
    	printf(" 2.Edit\n");
    	printf(" 3.Process All List\n");
    	printf(" 4.Search\n");
    	printf(" 5.Exit\n");
    	printf(" 6.Open a file(updating not use)\n");
    	printf(" 7.Save a file(updating not use)\n");
    	printf(" 8.Delete\n");
    	printf(" 9.Delete All List\n");
    	printf("10.Sort\n");
    	printf("Chon:");
    	scanf("%d%*c", &x);
    	switch (x)
    	{
    	case 1://Nhap them du lieu vao list
    		do
    		{
    		//nhap du lieu tu ban fim, truyen du lieu vao Node, dua node vao dau list
    		Input(l);
    		printf("Tiep tuc nhap du lieu hay ko ? Neu ko thi nhan ESC de thoat\n");
    		}while(getch()!=27);//bam ESC de thoat
    		break;
    	case 2:// Chinh sua thong tin trong list
    		EditInList(l);
    		break;
    	case 3://Duyet toan bo danh sach va in ra man hinh
    		ProcessList(l);
    		break;
    	case 4://Tim thong tin cua mot nhan vien
    		SearchInList(l);
    		break;
    	case 5://Thoat khoi chuong trinh
    		break;
    	case 6://Mo mot file nap du lieu vao bo nho (danh sach lien ket dong)
    		OpenFile(l);
    		break;
    	case 7:
    		SaveFile(l);
    		break;//ghi toan bo du lieu trong danh sach lien ket de len file
    	case 8:
    		Delete(l);//xoa thong tin cua mot nhan vien bang cach nhap vao ten cua nhan vien do
    		break;
    	case 9:
    		DeleteAllList(l);//xoa ca danh sach
    		break;
    	case 10:
    		Sort(l);//sap xep lai danh sach
    		break;
    	}
    
    	}while(x!=5);
    
    }
    
    void Input(LIST &l)
    {
    	float y;
    	int x;
    	NHANVIEN temp;
    	printf("name:");
    	gets(temp.name);
    	printf("birthday:\n");
    	printf("day:");
    	scanf("%d", &x);
    	temp.birthday.day=x;
    	printf("month:");
    	scanf("%d", &x);
    	temp.birthday.month=x;
    	printf("year:");
    	scanf("%d", &x);
    	temp.birthday.year=x;
    	printf("salary:");
    	scanf("%f", &y);
    	temp.salary=y;
    	printf("sex:(1-male 2-female)\n");
    	scanf("%d%*c", &temp.sex);//xoa bo dem
    	NODE *pNode;
    	pNode = new NODE;
    	pNode=GetNode(pNode,temp);
    	AddFirst(pNode,l);
    
    }
    NODE* GetNode(NODE *pNode, NHANVIEN x)
    {
    	pNode->info=x;
    	pNode->pNext=NULL;
    	return pNode;
    }
    void AddFirst(NODE* pNode, LIST &l)
    {
    	//neu danh sach rong
    
    	{
    		if(l.pHead==NULL)
    		{
    			l.pHead=pNode;
    			l.pTail=l.pHead;
    		}
    		else //nguoc lai
    		{
    			pNode->pNext=l.pHead;
    			l.pHead=pNode;
    		}
    	}
    }
    void AddAfter(NODE* pNode, LIST &l)
    {
    	if(l.pHead=NULL)
    	{
    		l.pHead=pNode;
    		l.pTail=l.pHead;
    	}
    	else
    	{
    		l.pTail->pNext=pNode;
    		l.pTail=pNode;
    	}
    }
    void Output(NODE* pNode)
    {
    
    	printf("| %-17s ", pNode->info.name);
    	printf("| %2d/%2d/%4d ", pNode->info.birthday.day, pNode->info.birthday.month, pNode->info.birthday.year);
    	printf("| %12.1f ", pNode->info.salary);
    	if(pNode->info.sex==1)
    		printf("| Male   |\n");
    	else
    		printf("| Female |\n");
    	printf("----------------------------------------------------------\n");
    }
    void ProcessList(LIST l)
    {
    	printf("\n----------------------------------------------------------");
    	printf("\n|       Name        |  Birthday  |    Salary    |  Sex   |");
    	printf("\n----------------------------------------------------------\n");
    	for(NODE* pNode=l.pHead; pNode!=NULL; pNode=pNode->pNext)
    		Output(pNode);
    }
    void StartList(LIST &l)
    {
    	l.pHead=NULL;
    	l.pTail=NULL;
    }
    //tim theo ten nhan vien
    void SearchInList(LIST l)
    {
    	char nametemp[30];
    	printf("Nhap ten nv can tim: ");
    	fflush(stdin);
    	gets(nametemp);
    	printf("\n----------------------------------------------------------");
    	printf("\n|       Name        |  Birthday  |    Salary    |  Sex   |");
    	printf("\n----------------------------------------------------------\n");
    	for(NODE* pNode = l.pHead; pNode!=NULL; pNode=pNode->pNext)
    	{
    		if(strcmp(nametemp,pNode->info.name)==0)
    			Output(pNode);
    	}
    
    }
    //edit thong tin cua 1 nhan vien khi nhap ten cua nhan vien do
    void EditInList(LIST &l)
    {
    	char nametemp[30];
    	int t;
    	float temp;
    	int key=1;
    	printf("Nhap ten nv can edit: ");
    	gets(nametemp);
    	for(NODE* pNode = l.pHead; pNode!=NULL&&key!=0; pNode=pNode->pNext)
    	{
    
    		if(strcmp(nametemp,pNode->info.name)==0)
    		{
    			Output(pNode);
    			printf("Muon edit cai ji nao ?\n");
    			printf("1.name\n2.birthday\n3.salary\n4.sex\nChon:");
    			scanf("%d%*c", &t);
    			switch(t)
    			{
    			case 1:
    				printf("Name:");
    				gets(nametemp);
    				strcpy(pNode->info.name,nametemp);
    				break;
    			case 2:
    				printf("day:");
    				scanf("%d", &t);
    				pNode->info.birthday.day=t;
    				printf("month:");
    				scanf("%d", &t);
    				pNode->info.birthday.month=t;
    				printf("year:");
    				scanf("%d%*c", &t);
    				pNode->info.birthday.year=t;
    				break;
    			case 3:
    				printf("salary:");
    				scanf("%f", &temp);
    				pNode->info.salary=temp;
    				break;
    			case 4:
    				printf("sex:");
    				scanf("%d", &t);
    				pNode->info.sex=t;
    				break;
    			}
    			key=0;
    		}
    	}
    }
    void OpenFile(LIST &l)
    {
    	FILE *pf;
    	NHANVIEN temp;
    	NODE *pNode = new NODE;
    	char *s = new char[50];
    	printf("Hay go vao duong dan:");
    	fflush(stdin);
    	gets(s);
    	if((pf=fopen(s,"r")) == NULL)
    	{
    		//khong tim thay file can tim
    		printf("Can not open this file\n");
    		exit(0);
    	}
    	else
    	{
    		//da tim thay file can tim
    		//tro con tro file ve dau file
    		DeleteAllList(l);
    		fread(&temp,sizeof(pNode->info),1,pf);//doc thong tin tu file luu ra mot bien temp
    		pNode=GetNode(pNode,temp);//luu thong tin cua bien temp vao trong mot node vua tao
    		AddFirst(pNode,l);// add node do vao trong danh sach lien ket
    
    	}
    	delete pNode;
    	delete s;
    	fclose(pf);
    }
    void SaveFile(LIST &l)
    {
    	FILE *pf;
    	char *s=new char[50];
    	printf("nhap vao duong dan:");
    	fflush(stdin);
    	gets(s);
    	if((pf=fopen(s,"wt")) == NULL)
    	{
    		printf("error");
    		exit(0);
    	}
    	else
    	for(NODE * pNode=l.pHead; pNode!=NULL; pNode=pNode->pNext)
    	{
    		//lay thong tin tu node ghi vao file
    		fwrite(&pNode->info,sizeof(pNode->info),1,pf);
    
    	}
    	delete s;//giai phong bo nho
    	fclose(pf);//dong tap tin
    }
    
    void Delete(LIST &l)//xoa thong tin cua mot nhan vien bang cach nhap vao ten cua nhan vien ay
    {
    	printf("Nhap vao ten cua nhan vien muon xoa:\n");
    	printf("Ten:");
    	fflush(stdin);
    	char *s = new char[50];
    	gets(s);
    	NODE* pNode;
    	NODE* pNode2=NULL;
    	for(pNode=l.pHead; pNode!=NULL; pNode=pNode->pNext)
    	{
    		if(strcmp(pNode->info.name,s) == 0)
    		{
    			printf("Da tim thay thong tin cua nhan vien nay\n");
    			break;//khi tim thay nhan vien can tim thi thoat khoi vong lap *pNode tro toi struct cua nhan vien can tim
    		}
    		pNode2=pNode;
    	}
    	if(pNode!=NULL)//tim thay dc thong tin cua nhan vien trong danh sach
    	{
    		//co hai truong hop
    		//truong hop 1 : nhan vien can tim ngay o vi tri dau tien cau danh sach
    		//luc nay thi pNode2 =NULL nen ta phai dung cach xoa Node kieu khac
    		//truong hop 2 : thi dung 2 con tro pNode va pNode2 de xoa, trong do pNode2 la
    		//con tro theo sau cua pNode sau tung buoc so sanh tim kiem thi dich doi con tro pNode2
    		//ngay vi tri cua con tro pNode
    		printf("Ban mun xoa thong tin cua nhan vien nay ko(y/n)\n");
    			fflush(stdin);
    			int c=getch();
    			if(c=='y')
    			{
    				printf("Please wait.............\n");
    				printf("Deleting................\n");
    				if(pNode2==NULL)//truong hop 1
    				{
    					l.pHead=pNode->pNext;
    					delete pNode;
    				}
    				else//truong hop 2
    				{
    					pNode2->pNext=pNode->pNext;//tao cau noi
    					delete pNode; //xoa struct cua nhan vien can tim
    				}
    			}
    	}
    	delete s;
    }
    void DeleteAllList(LIST &l)//xoa toan danh sach nhan vien
    {
    	NODE* pNode;
    	while(l.pHead!=NULL)
    	{
    	pNode=l.pHead;
    	l.pHead=l.pHead->pNext;
    	delete pNode;
    	}
    }
    void Swap(NHANVIEN &a, NHANVIEN &b)//hoan vi thong tin cua 2 node nhan vien
    {
    	NHANVIEN t=a;
    	a=b;
    	b=t;
    }
    
    void SortByName(LIST &l)
    {
    	//phuong an 1: doi cho~ phan info giu nguyen cac lien ket de sap xep uu diem la de cai` dat
    	//				nhuoc diem la luc hoan vi phan info cua node se~ ton them bo nho cua bien temp
    	//				phan info trong that te la rat lon nen dieu nay se ton rat nhui bo nho
    	/*NODE *pNodeMin;
    	for(NODE* pNode1=l.pHead; pNode1!=NULL; pNode1=pNode1->pNext)
    	{
    		pNodeMin=pNode1;
    		for(NODE* pNode2=pNode1->pNext; pNode2!=NULL; pNode2=pNode2->pNext)
    		{
    			if(strncmp(pNodeMin->info.name,pNode2->info.name,1) > 0)
    			{
    				pNodeMin=pNode2;
    			}
    		}
    		Swap(pNode1->info,pNodeMin->info);
    	}*/
    	//phuong an 2 :
    	//chinh cac lien ket giua cac node nhan vien sao cho thanh mot day cac node dc sap xep theo
    	//mot chuan nao do(vi` du nhu tien luong chang han)
    	//Uu diem : ko ton nhui bo nho chi ton vai con tro de thuc hien viec ket noi
    	//Nhuoc diem : Tuy nhien viec cac dat kha phuc tap do vay neu bai nao ta thay
    	//				phan info cua node it bo nho thi nen chon phuong an 2
    	//O day truoc tien ta su dung giai thuat sap xep tang dan theo selectionsort
    	NODE* pNodeMin;
    	NODE* pNodePreMin;//con tro luu vi tri node sau con tro pNodeMin
    	NODE* pNode1;
    	NODE* pNode2;
    	LIST ltemp;
    	StartList(ltemp);
    	while(l.pHead!=NULL)
    	{
    		pNode1=l.pHead;
    		pNode2=l.pHead->pNext;
    		pNodePreMin=NULL;
    		pNodeMin=pNode1;
    		while(pNode2!=NULL)
    		{
    			if(strncmp(pNodeMin->info.name,pNode2->info.name,1) > 0)
    			{
    				pNodeMin=pNode2;
    				pNodePreMin=pNode1;
    			}
    			pNode1=pNode2;
    			pNode2=pNode2->pNext;
    		}
    	//sau vong lap while o tren ta duoc gia tra tri nho nhat tro boi con tro pNodeMin
    	//sau do ta tach node nho nhat nay ra khoi danh sach
    	//co 2 truong hop xay ra:
    	//neu phan tu nho nhat o dau danh sach thi pNodePreMin =NULL thi ta tach o phan dau
    	//nguoc lai thi ta tach o phan giua
    		if(pNodePreMin==NULL)//tach o phan dau
    		{
    			l.pHead=l.pHead->pNext;
    		}
    		else//neu pt nho nhat o cac vi tri khac
    		{
    			pNodePreMin->pNext=pNodeMin->pNext;
    		}
    		pNodeMin->pNext=NULL;//cat dut moc noi pt nho nhat voi danh sach lien ket
    	//sau do add tung node vao danh sach temp
    		AddAfter(pNodeMin,ltemp);
    	}	
    	//wway lai buoc tren cho den khi het danh sach
    	DeleteAllList(l);//xoa danh sach cu~
    	l=ltemp;
    }
    void SortBySalary(LIST &l)
    {
    	/*NODE *pNodeMin;//con tro luu vi tri node co gia tri nho nhat
    	
    	
    	for(NODE* pNode1=l.pHead; pNode1!=NULL; pNode1=pNode1->pNext)
    	{
    		pNodeMin=pNode1;
    		for(NODE* pNode2=pNode1->pNext; pNode2!=NULL; pNode2=pNode2->pNext)
    		{
    			if(pNodeMin->info.salary > pNode2->info.salary)
    			{
    				pNodeMin=pNode2;
    			}
    		}
    		Swap(pNode1->info,pNodeMin->info);
    	}*/
    		NODE* pNodeMin;
    	NODE* pNodePreMin;//con tro luu vi tri node sau con tro pNodeMin
    	NODE* pNode1;
    	NODE* pNode2;
    	LIST ltemp;
    	StartList(ltemp);
    	while(l.pHead!=NULL)
    	{
    		pNode1=l.pHead;
    		pNode2=l.pHead->pNext;
    		pNodePreMin=NULL;
    		pNodeMin=pNode1;
    		while(pNode2!=NULL)
    		{
    			if(pNodeMin->info.salary > pNode2->info.salary)
    			{
    				pNodeMin=pNode2;
    				pNodePreMin=pNode1;
    			}
    			pNode1=pNode2;
    			pNode2=pNode2->pNext;
    		}
    	//sau vong lap while o tren ta duoc gia tra tri nho nhat tro boi con tro pNodeMin
    	//sau do ta tach node nho nhat nay ra khoi danh sach
    	//co 2 truong hop xay ra:
    	//neu phan tu nho nhat o dau danh sach thi pNodePreMin =NULL thi ta tach o phan dau
    	//nguoc lai thi ta tach o phan giua
    		if(pNodePreMin==NULL)//tach o phan dau
    		{
    			l.pHead=l.pHead->pNext;
    		}
    		else//neu pt nho nhat o cac vi tri khac
    		{
    			pNodePreMin->pNext=pNodeMin->pNext;
    		}
    		pNodeMin->pNext=NULL;//cat dut moc noi pt nho nhat voi danh sach lien ket
    	//sau do add tung node vao danh sach temp
    		AddAfter(pNodeMin,ltemp);
    	}	
    	//wway lai buoc tren cho den khi het danh sach
    	DeleteAllList(l);//xoa danh sach cu~
    	l=ltemp;
    }
    void SortByBirthday(LIST &l)
    {
    	NODE *pNodeMin;
    	for(NODE* pNode1=l.pHead; pNode1!=NULL; pNode1=pNode1->pNext)
    	{
    		pNodeMin=pNode1;
    		for(NODE* pNode2=pNode1->pNext; pNode2!=NULL; pNode2=pNode2->pNext)
    		{
    			if(pNodeMin->info.birthday.year > pNode2->info.birthday.year)
    			{
    				pNodeMin=pNode2;
    			}
    		}
    		Swap(pNode1->info,pNodeMin->info);
    	}
    }
    void Sort(LIST &l)
    {
    	int chon;
    	do
    	{
    		printf("Menu sort\n");
    		printf("1.Sort by name\n");
    		printf("2.Sort by salary\n");
    		printf("3.Sort by birthday\n");
    		printf("4.Not sort\n");
    		printf("Chon:");
    		scanf("%d%*c", &chon);
    		switch(chon)
    		{
    		case 1:
    			SortByName(l);
    			break;
    		case 2:
    			SortBySalary(l);
    			break;
    		case 3:
    			SortByBirthday(l);
    			break;
    		case 4:
    			;
    		}
    	}while(chon!=4);
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Do not flush stdin, see FAQ.
    NEVER use gets, see http://cpwiki.sf.net/Gets
    Do not use void main, see http://cpwiki.sf.net/Void_main or alternatively the FAQ.
    Last edited by Elysia; 04-18-2008 at 05:54 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,744
    Can you go into perhaps even slightly more detail than "halp, it's wrong"?

    You can't just dump your whole program and expect an answer.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    this is C++ not C
    but it is horrible C++ that uses all horrible practices of C that should not be used even in C, I do not speak of C++

    You should start by reading FAQ about void main, gets, fflush stdin etc
    Then - decide if you would like to program in C or C++
    then write this program using best practices of the language you have choosen
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by vart View Post
    this is C++ not C
    but it is horrible C++ that uses all horrible practices of C that should not be used even in C, I do not speak of C++

    You should start by reading FAQ about void main, gets, fflush stdin etc
    Then - decide if you would like to program in C or C++
    then write this program using best practices of the language you have choosen
    For example, you're defining variables in the middle of your function.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    That's allowed in C99, but what's worse is that a lot of functions take references instead of pointers which is only allowed in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    C++ for sure, it uses new[] and then incorrectly matches that with delete instead of delete[].

    Foreign language comments are as good as no comments, for me. What amuses me is that it contains the words: "Thong" and "bang", not to mention "sex".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Adding directory/file names to a linked list
    By thoseion in forum C Programming
    Replies: 13
    Last Post: 12-08-2006, 12:13 PM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 08:07 PM
  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