处理字符串的链表,C
我试图从用户那里获取输入(一个单词/字符串)并将其存储在我的链接列表中,但是当我打印列表时,程序结束。我对C语言非常陌生,任何帮助我的建议或文章都将不胜感激处理字符串的链表,C,c,string,linked-list,C,String,Linked List,我试图从用户那里获取输入(一个单词/字符串)并将其存储在我的链接列表中,但是当我打印列表时,程序结束。我对C语言非常陌生,任何帮助我的建议或文章都将不胜感激 #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { int data, index; char text[255]; struct node* next; }; //Global Variable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
int data, index;
char text[255];
struct node* next;
};
//Global Variables
struct node* root = NULL;
//Prototypes
void insertA();
void insertB();
void prn();
//Main
void main () {
char command[4];
int num;
char text[255];
while(1) {
printf("Command? "); //assume we know the key words
fflush(stdout);
scanf("%s", &command);
//Reads input and selects which command to execute
if (strcmp(command, "insertA")==0) {
scanf("%s", &text);
insertA(text);
} else
if(strcmp(command, "insertB")==0) {
scanf("%s", &text);
insertB(num);
} else
if (strcmp(command, "prn")==0) {
prn();
} else
if (strcmp(command, "end")==0) {
exit(1);
}
else {
return;
}
}
}
//Command Insert After
void insertA(char* text) {
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = text;
temp->next = NULL;
//Function if link does not exist, creates one
if(root==NULL) {
root = temp;
printf("Text inserted at beginning\n");
}
//If link exists, adds to the end
else {
struct node* p;
p = root;
while(p->next != NULL) {
p = p->next;
}
p->next = temp;
printf("Ok\n");
}
}
//Command Insert Before
void insertB(char* text) {
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->data = text;
temp->next=NULL;
//Function if link does not exist, creates one
if (root == NULL) {
root = temp;
printf("Text inserted at beginning\n");
fflush(stdout);
}
//If link exists, add to beginning
else {
temp->next=root;
root = temp;
printf("Ok\n");
fflush(stdout) ;
}
}
//Command Print
void prn() {
struct node* temp;
temp = root;
int i=1;
if(temp == NULL) {
printf("List is empty\n");
fflush(stdout);
}
else {
while(temp != NULL) {
printf("%d. ", i);
printf("%s\n",temp->data);
temp = temp->next;
i++;
}
printf("\n");
}
}
#包括
#包括
#包括
结构节点{
int数据,索引;
字符文本[255];
结构节点*下一步;
};
//全局变量
结构节点*root=NULL;
//原型
void insertA();
void insertB();
void prn();
//主要
空干管(){
char命令[4];
int-num;
字符文本[255];
而(1){
printf(“Command?”);//假设我们知道关键词
fflush(stdout);
scanf(“%s”,命令(&C);
//读取输入并选择要执行的命令
if(strcmp(命令,“insertA”)==0){
scanf(“%s”,文本(&T);
插入(文本);
}否则
if(strcmp(命令,“insertB”)==0){
scanf(“%s”,文本(&T);
插入b(num);
}否则
如果(strcmp(命令,“prn”)==0){
prn();
}否则
如果(strcmp(命令,“结束”)==0){
出口(1);
}
否则{
返回;
}
}
}
//命令插入后
无效插入符(字符*文本){
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=文本;
temp->next=NULL;
//函数,如果链接不存在,则创建一个链接
if(root==NULL){
根=温度;
printf(“开头插入的文本”);
}
//如果存在链接,则添加到末尾
否则{
结构节点*p;
p=根;
while(p->next!=NULL){
p=p->next;
}
p->next=温度;
printf(“Ok\n”);
}
}
//命令插入之前
void insertB(字符*文本){
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=文本;
temp->next=NULL;
//函数,如果链接不存在,则创建一个链接
if(root==NULL){
根=温度;
printf(“开头插入的文本”);
fflush(stdout);
}
//如果存在链接,则添加到开头
否则{
temp->next=root;
根=温度;
printf(“Ok\n”);
fflush(stdout);
}
}
//命令打印
无效prn(){
结构节点*temp;
温度=根;
int i=1;
if(temp==NULL){
printf(“列表为空\n”);
fflush(stdout);
}
否则{
while(temp!=NULL){
printf(“%d.”,i);
printf(“%s\n”,临时->数据);
温度=温度->下一步;
i++;
}
printf(“\n”);
}
}
我将附上我得到的输出的屏幕截图,以便您可以看到发生了什么您的问题在
prn
函数中:节点
结构中的数据
属性是int作为数据类型,但在prn
函数中,您将其用作字符串;这是您的错误答案:通过printf
函数导致分段错误。您只需要在结构中定义好属性,并在函数中正确使用这些属性。
我建议大家学习:指针和双指针,它们让你知道如何更好地编码。当你将数据定义为int时,你的文本是一个char变量 在插入中,您正在执行
temp->data=text代码>也在打印功能中
您正在执行printf(“%s\n”,temp->data)代码>当temp->data为int时您的命令缓冲区只有4个字节-如何将“insertA”放入其中
您正在使用scanf()
获取命令<当您按enter键时,code>scanf()
将用一个“\n”
分隔字符串填充输入缓冲区,然后当它在循环中进行第二轮时,您的下一个scanf将首先读取换行符,而不是您想要测试的任何命令
如果要安全地重用
文本
缓冲区,请将其大小增加一些,并使用fgets()
将输入读入缓冲区。在insert函数中使用strcpy()或strncpy()。strcpy(临时->数据,文本)@user2181624当我这样做时,程序在我输入了inputregarding语句后退出,比如:scanf(“%s”,&command)代码>1)始终检查返回值(而不是参数值),以确保操作成功。建议:在使用输入格式说明符时,如果(1!=scanf(“%s”,&command)){//handle error and exit}//else,scanf successful
2)使用输入格式说明符:%[…]
和%s
,则始终包含比输入缓冲区长度小1的MAX CHARACTERS修饰符(因为这些说明符总是附加NUL字节)为避免输入缓冲区溢出,输入缓冲区溢出是未定义的行为,可能导致seg故障事件。“命令”字符串(如“insertA”)实际上为8个字符长。所以不能放入4个字符的缓冲区。建议将缓冲区(至少)延长一倍。
struct node {
int data, index;
char text[255];
struct node* next;
};