佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 741|回复: 0

linked list problem 2

[复制链接]
cheong_yuen 该用户已被删除
发表于 27-12-2005 05:42 PM | 显示全部楼层 |阅读模式
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 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 4-3-2025 01:05 PM , Processed in 0.111939 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表