插入的名称和编号,以链表的末尾 [英] Inserting a name and number to the end of linked list

查看:146
本文介绍了插入的名称和编号,以链表的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文本文件读取这样的:

 乔治·华盛顿,2345678
约翰·亚当斯,3456789
托马斯·杰斐逊,4567890
詹姆斯·麦迪逊,0987654
詹姆斯·门罗,9876543
约翰·昆西·亚当斯,8765432
安德鲁·杰克逊,7654321
马丁·范布伦,6543210

有人能提供我如何得到我的插入功能的姓名和身份证号码添加到链接列表的末尾见解?当我运行code。将选择选项1那么它跳过附加名,仅要求进入​​整数。之后,什么也不会发生。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;//创建节点保持学生的信息
结构节点
{
    焦炭名[50];
    INT ID;
    结构节点*接下来的;
}*头;//创建函数原型
无效readDataFile();
无效插入(字符* inName,字符* INID);
无效显示(结构节点* D);
INT deleteID(INT NUM);
无效deleteName(CHAR DNAME);
//主要功能
诠释的main()
{
    //声明变量
    INT I,NUM,身份证;
    字符*名称;
    焦炭nameDelete [50];
    焦炭nameInsert [50];
    结构节点* N;    //初始化链接列表
    头= NULL;    //读取文件
    readDataFile();    //创建程序使用的操作列表
    而(1)
    {
        的printf(\\ NLIST操作\\ n);
        的printf(=============== \\ n);
        的printf(1.Insert \\ n);
        的printf(2.显示\\ n);
        的printf(根据ID \\ n 3.删除);
        的printf(按名称\\ n 4.Delete);
        的printf(5.Exit \\ n);
        的printf(请输入您的选择:);        如果(scanf函数(%d个,&安培; I)LT; = 0)
        {
            的printf(请输入只是一个整数\\ n);
            出口(0);
        }
        其他
        {
            开关(ⅰ)
            {
                情况1:
                    的printf(输入名称插入:\\ n);
                    scanf函数(%[^ \\ n]的,nameInsert);
                    的getchar();
                    的printf(请输入与名称相关联的标识:);
                    scanf的(%D,和ID);
                    插入(nameInsert,身份证);
                    打破;
                案例2:
                    如果(头== NULL)
                        的printf(列表为空\\ n);
                    其他
                    {
                        的printf(列表中的元素有:\\ n);
                    }
                    显示(N);
                    打破;
                案例3:
                    如果(头== NULL)
                        的printf(列表为空\\ n);
                    其他
                    {
                        的printf(请输入身份证号码删除:);
                        scanf的(%D,和ID);
                    }                    如果(deleteID(ID))
                        的printf(%d个成功删除的\\ n,身份证);
                    其他
                        的printf(%D不在列表中\\ n找到了,ID);
                    打破;
                情况4:
                    如果(头== NULL)
                        的printf(列表为空\\ n);
                    其他
                    {
                        的printf(请输入名称,删除:);
                        得到(nameDelete);
                    }
                情况5:
                    返回0;
                默认:
                    的printf(无效选项\\ n);
            }
        }
    }
    返回0;
}//定义功能
//功能中的数据文件中读取
无效readDataFile()
{
    //初始化链接列表
    结构节点*温度;
    TEMP =的malloc(sizeof的(结构节点));
    TEMP->接着= NULL;
    头=温度;    //打开文件
    FILE * FP;
    FP = FOPEN(AssignmentOneInput.txt,R);    //使用memset的功能将字符字符串复制
    字符字符串[100];
    memset的(字符串,0,100);    //创建,而在该文件的内容循环扫描
    而(与fgets(字符串,100,FP)!= NULL)
        {
        的sscanf(字符串,%20 ^,],%d个,TEMP-&G​​T;名称&放大器; TEMP-和SEQ ID);
        TEMP->接下来=的malloc(sizeof的(结构节点));
        TEMP = TEMP->接下来,
        TEMP->接着= NULL;        }
        FCLOSE(FP);
}//函数插入姓名和身份证号码
无效插入(字符* inName,字符* INID)
{
    //创建临时和助手节点
    结构节点*温度,*帮手;    //分配内存为临时节点
    TEMP =(结构节点*)malloc的(的sizeof(结构节点));    //转换字符到整数
    INT INTID =的atoi(INID);    //设置节点中的数据
    的strcpy(TEMP-GT&;名,inName);
    TEMP-和SEQ ID = INTID;
    TEMP->接着= NULL;    //创建新的节点,并添加到它
    帮助=(结构节点*)头;    而(helper->!下次= NULL)
    {
        助手= helper->接下来,
    }    helper->接下来=温度;
    助手=温度;
    helper->接着= NULL;
}


解决方案

一旦你选择的选项中输入1标准输入既有1和换行符,你在接下来的scanf函数声明立即期待的。

修改 scanf函数(%[^ \\ n]的,nameInsert); scanf函数(%S,nameInsert);
或者使用的getchar()获得选择选项之后。

My text file reads like this:

George Washington, 2345678 
John Adams, 3456789 
Thomas Jefferson, 4567890 
James Madison, 0987654 
James Monroe, 9876543 
John Quincy Adams, 8765432 
Andrew Jackson, 7654321 
Martin Van Buren, 6543210

Can someone offer insight on how I get my insert function to add the name and ID number to the end of the linked list? When I run the code an select option 1 it skips over the add name and only asks to enter the integer. After that nothing happens.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//Creates node for holding student's information
struct node
{
    char name [50];
    int id;
    struct node *next;
}*head;

//Create Function Prototypes
void readDataFile ();
void insert(char *inName, char *inID);
void display(struct node *d);
int deleteID(int num);
void deleteName(char dname);


//Main function
int main()
{
    //Declare variables
    int i, num, id;
    char *name;
    char nameDelete [50];
    char nameInsert [50];
    struct node *n;

    //initialize link list
    head = NULL;

    //Read in file
    readDataFile();

    //Create list of operations utilized in program
    while (1)
    {
        printf("\nList Operations\n");
        printf("===============\n");
        printf("1.Insert\n");
        printf("2.Display\n");
        printf("3.Delete by ID\n");
        printf("4.Delete by Name\n");
        printf("5.Exit\n");
        printf("Enter your choice : ");

        if(scanf("%d", &i) <= 0)
        {
            printf("Enter only an Integer\n");
            exit(0);
        }
        else
        {
            switch(i)
            {
                case 1:
                    printf("Enter the name to insert:\n");
                    scanf("%[^\n]s", nameInsert);
                    getchar();
                    printf("Enter the ID associated with the name: ");
                    scanf("%d", &id);
                    insert(nameInsert, id);
                    break;
                case 2:
                    if (head == NULL)
                        printf("List is Empty\n");
                    else
                    {
                        printf("Elements in the list are:\n");
                    }
                    display(n);
                    break;
                case 3:
                    if(head == NULL)
                        printf("List is Empty\n");
                    else
                    {
                        printf("Enter the ID number to delete: ");
                        scanf("%d", &id);
                    }

                    if(deleteID(id))
                        printf("%d deleted successfully \n", id);
                    else
                        printf("%d not found in the list\n", id);
                    break;
                case 4:
                    if(head == NULL)
                        printf("List is Empty\n");
                    else
                    {
                        printf("Enter name to delete: ");
                        gets(nameDelete);
                    }
                case 5:
                    return 0;
                default:
                    printf("Invalid option\n");
            }
        }
    }
    return 0;
}

//Define the functions
//Function to read in the data file
void readDataFile()
{
    //Initialize the link the list
    struct node *temp;
    temp = malloc(sizeof(struct node));
    temp->next = NULL;
    head = temp;

    //Open up the file
    FILE *fp;
    fp = fopen("AssignmentOneInput.txt", "r");

    //Use memset function to copy the characters in string
    char string[100];
    memset(string, 0, 100);

    //Create while loop scan in the contents of the file
    while(fgets(string, 100, fp) != NULL)
        {
        sscanf(string, "%20[^,], %d", temp->name, &temp->id);
        temp->next = malloc(sizeof(struct node));
        temp = temp->next;
        temp->next = NULL;

        }
        fclose(fp);
}

//Function to insert a name and ID number
void insert(char *inName, char *inID)
{
    //Create temporary and helper nodes
    struct node *temp, *helper;

    //Allocate the memory for the temporary node
    temp = (struct node *)malloc(sizeof(struct node));

    //Convert character into integer value
    int intID = atoi(inID);

    //Set the data in the node
    strcpy(temp->name, inName);
    temp->id = intID;
    temp->next = NULL;

    //Create new node and add to it
    helper = (struct node *)head;

    while(helper->next != NULL)
    {
        helper = helper->next;
    }

    helper->next = temp;
    helper = temp;
    helper->next = NULL;
}

解决方案

Once you enter 1 in selection option the standard input has both 1 and newline character, which you are expecting immediately in the next scanf statement.

change the scanf("%[^\n]s", nameInsert); into scanf("%s", nameInsert); or use getchar() immediately after getting selection option.

这篇关于插入的名称和编号,以链表的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆