查看: 742|回复: 0
|
linked list problem 2
[复制链接]
|
|
Can anyonne help me to correct the getstudentinfo() function bz the result come out is not the result that i want, for example if my database have 2 student record and i using id to search that particular record , example i want to search id 111 but the result come out is xxx .
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/* student2.c : The Module file */
#include "student.h"
static NODE *head_ptr = NULL;
/**
** main_interface()
**/
void main_interface(int ch)
{
switch(ch){
case 'a':
list_node_add();
break;
case 'd':
if (!list_node_delete())
list_node_print();
break;
case 'p':
list_node_print();
break;
case 's':
getStudentInfo();
break;
default:
break;
}
}
/**
** list_node_create()
**/
NODE *list_node_create(void)
{
NODE *ptr;
if((ptr=(NODE *)malloc(sizeof(NODE))) == NULL)
ErrorExit("malloc() failed.\n");
ptr ->next_ptr = NULL; /* set the next pointer to null */
ptr ->id = 0; /* initialization */
return ptr;
}
/**
** list_node_add()
**/
void list_node_add(void)
{
NODE *new_ptr, *ptr;
new_ptr = list_node_create();
printf("Enter the Student Name and ID,\n");
printf("Enter the Student CGPA and GRADE : ");
scanf("%s%ld%f%s",new_ptr ->name, &new_ptr ->id,&new_ptr ->cgpa,new_ptr ->grade);
if (head_ptr == NULL){
head_ptr = new_ptr;
}
else{
/*find the last node in the list */
for(ptr=head_ptr;
ptr ->next_ptr !=NULL;
ptr=ptr ->next_ptr)
;/* doing nothing here */
/* link to the last node */
ptr ->next_ptr = new_ptr;
}
}
/**
** list_node_delete()
**/
int list_node_delete(void)
{
NODE *ptr, *ptr_saved;
unsigned long id;
int deleted = 0;
int reval = 0;
if (head_ptr == NULL){
printf("Sorry, nothing to delete.\n");
reval = 1;
}
else
{
printf("Enter the student ID: ");
scanf("%ld",&id);
if(head_ptr ->id == id){
ptr_saved = head_ptr ->next_ptr;
free(head_ptr);
head_ptr = ptr_saved;
if (head_ptr == NULL){
printf("All node have been deleted.\n");
reval = 1;
}
}else{
for (ptr=head_ptr;
ptr->next_ptr != NULL;
ptr=ptr->next_ptr){
if (ptr->next_ptr->id == id){
ptr_saved = ptr->next_ptr->next_ptr;
free(ptr->next_ptr);
ptr->next_ptr = ptr_saved;
deleted = 1;
break;
}
}
if (!deleted){
printf("Can not find the student ID.\n");
}
}
}
return reval;
}
/**
** list_node_print()
**/
void list_node_print(void)
{
NODE *ptr;
if (head_ptr == NULL){
printf("Nothing to display.\n");
}else{
printf("The content of the linked list:\n");
for (ptr = head_ptr;ptr ->next_ptr != NULL;ptr = ptr ->next_ptr){
printf("%s:ID:%d,CGPA:%.2f,GRADE:%s -> \n",ptr -> name,ptr ->id,ptr -> cgpa,ptr ->grade);
}
printf("%s:ID:%d,CGPA:%.2f,GRADE:%s ->|",ptr -> name,ptr ->id,ptr -> cgpa,ptr ->grade);
printf("\n");
}
}
/**
** list_node_free()
**/
void list_node_free()
{
NODE *ptr, *ptr_saved;
for (ptr=head_ptr; ptr != NULL; ){
ptr_saved = ptr ->next_ptr;
free(ptr);
ptr = ptr_saved;
}
free(ptr);
}
/**
** ErrorExit()
**/
void ErrorExit(char *str)
{
printf("%s\n",str);
exit(ERR_FLAG);
}
/**
** Display()
**/
void Display(void)
{
printf("\t****************************************************\n");
printf("\t\t WELCOME TO CLASS RECORD SYSTEM\n");
printf("\t****************************************************\n");
printf("\t\t***************************************\n\n");
printf("\t\tGrade Grade Point\n");
printf(" \t\tA 4.00\n");
printf(" \t\tA- 3.67\n");
printf(" \t\tB+ 3.33\n");
printf(" \t\tB 3.00\n");
printf(" \t\tB- 2.67\n");
printf(" \t\tC+ 2.33\n");
printf(" \t\tC 2.00\n");
printf(" \t\tC- 1.67\n");
printf(" \t\tC 1.33\n");
printf(" \t\tC- 1.67\n");
printf(" \t\tD+ 1.33\n");
printf(" \t\tD 1.00\n");
printf(" \t\tE 0.00\n\n");
printf("\t\t***************************************\n\n");
}
void getStudentInfo(void)
{
NODE *ptr; unsigned long id;
printf("Enter the ID of a student: ");
scanf("%ld",&id);
printf("Searching the list...");
if (head_ptr == NULL){
printf("Nothing to display.\n");
}else{
printf("The Information for the student is:\n");
for (ptr=head_ptr;
ptr->next_ptr != NULL;
ptr=ptr->next_ptr){
if (ptr->next_ptr->id == id){
printf("%s:ID:%d,CGPA:%.2f,GRADE:%s -> \n",ptr -> name,ptr ->id,ptr -> cgpa,ptr ->grade);
} }
printf("\n");
}
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/* student1.c : The drive file */
#include "student.h" /* include header file */
main(void)
{
int ch;
Display();
printf("Enter a for adding, d for deleting,\n");
printf("p for displaying, and q for quit:\n");
while ((ch=getchar())!= 'q') {
main_interface(ch); /* process input from the user */
}
list_node_free();
printf("\nBye!\n");
return 0;
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/*student.h : the header file */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifndef LNK_LIST_H
#define LNK_LIST_H
#define ERR_FLAG 1
#define MAX_LEN 16
struct lnk_list_struct
{
char name[MAX_LEN];
unsigned long id;
float cgpa;
char grade[10];
struct lnk_list_struct *next_ptr;
};
typedef struct lnk_list_struct NODE;
NODE *list_node_create(void);
void list_node_add(void);
int list_node_delete(void);
void list_node_print(void);
void list_node_free(void);
void ErrorExit(char *);
void main_interface(int);
void Display(void);
void getStudentInfo(void);
#endif /* for LNK_LIST_H */
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[ 本帖最后由 cheong_yuen 于 29-12-2005 03:34 PM 编辑 ] |
|
|
|
|
|
|
| |
本周最热论坛帖子
|