C语言中的链表插入

C语言中的链表插入,c,list,shell,unix,pointers,C,List,Shell,Unix,Pointers,我想写一个程序,从用户那里获取最后提示的输入,并按顺序打印它们。 以下是我遇到问题的代码片段: #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> //#include <termios.h> #ifdef _MSC_VER #define _CRT_SECURE_NO_WARNINGS #endif #define MAXCH

我想写一个程序,从用户那里获取最后提示的输入,并按顺序打印它们。 以下是我遇到问题的代码片段:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
//#include <termios.h>

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#define MAXCHARNUM 128
#define MAXARGNUM 32

char *argsexec[MAXARGNUM];
char str[500];
char *path;
char *name;
int length;
int sizeOfHistory=0;

struct History{
    char *hname;
    struct History *nextPtr;
    struct History *prevPtr;
}*nextPtr=NULL,*prevPtr=NULL;


struct History *Head = NULL;
struct History *Tail = NULL;
struct History *Temp = NULL;
struct History *Temp2 = NULL;
struct History *historyTemp = NULL;

void HistoryList(char *historyName){

    struct History *historyTemp = (struct History *)malloc(sizeof(struct History));

    if(sizeOfHistory == 0)
    {
        historyTemp->hname=historyName;
        historyTemp->prevPtr = NULL;
        historyTemp->nextPtr = NULL;
        Tail = historyTemp;
        Head = historyTemp;
        sizeOfHistory++;
        printf("Histtemp's :%s ",historyTemp->hname);
        printf("Head's name: %s ",Head->hname);
        printf("Tail's name: %s ",Tail->hname);
    }



    else if (sizeOfHistory < 10){

        historyTemp->hname=historyName;
        historyTemp->prevPtr = NULL;
        historyTemp->nextPtr = NULL;


        Head->prevPtr = historyTemp;
        historyTemp->nextPtr = Head;
        Head = historyTemp;
        sizeOfHistory++;
        printf("Head's name: %s ",Head->hname);

    }

    else{
        historyTemp->hname=historyName;
        Head->prevPtr = historyTemp;
        historyTemp->nextPtr = Head;
        Head = historyTemp;
        Tail=Tail->prevPtr;
        Temp=Tail->nextPtr;
        Tail->nextPtr = NULL;
        Temp->prevPtr = NULL;
        free(Temp);
    }
    //historyTemp=NULL;
}

void printHistory(){

    int counter=1;
    struct History *historyT=NULL;
    historyT = Head;

    while(historyT != NULL){
        printf("[%d] [%s] \n",counter,historyT->hname);
        historyT=historyT->nextPtr;
        counter++;
    }
}


void Setup(){

    while(1){


        while (!feof(stdin))
        {
            fgets(str, sizeof(str), stdin);
            HistoryList(str);
        }

    }
}

int main(int argc, char const *argv[])
{
    Setup();
    return 0;
}
#包括
#包括
#包括
#包括
//#包括
#ifdef硕士学位
#定义\u CRT\u安全\u无\u警告
#恩迪夫
#定义MAXCHARNUM 128
#定义MAXARGNUM 32
char*argsexec[MAXARGNUM];
char-str[500];
字符*路径;
字符*名称;
整数长度;
int sizeOfHistory=0;
结构历史{
char*hname;
结构历史*nextPtr;
结构历史*prevPtr;
}*nextPtr=NULL,*prevPtr=NULL;
结构历史*Head=NULL;
结构历史*Tail=NULL;
结构历史记录*Temp=NULL;
结构历史*Temp2=NULL;
结构历史*historyTemp=NULL;
无效历史列表(字符*历史名称){
结构历史*historyTemp=(结构历史*)malloc(sizeof(结构历史));
如果(sizeOfHistory==0)
{
historyTemp->hname=historyName;
historyTemp->prevPtr=NULL;
historyTemp->nextPtr=NULL;
尾部=历史温度;
水头=历史温度;
sizeOfHistory++;
printf(“Histtemp的:%s”,historyTemp->hname);
printf(“Head的名称:%s”,Head->hname);
printf(“Tail的名称:%s”,Tail->hname);
}
else if(大小历史<10){
historyTemp->hname=historyName;
historyTemp->prevPtr=NULL;
historyTemp->nextPtr=NULL;
Head->prevPtr=历史温度;
historyTemp->nextPtr=头部;
水头=历史温度;
sizeOfHistory++;
printf(“Head的名称:%s”,Head->hname);
}
否则{
historyTemp->hname=historyName;
Head->prevPtr=历史温度;
historyTemp->nextPtr=头部;
水头=历史温度;
Tail=Tail->prevPtr;
温度=尾部->下一个TPTR;
Tail->nextPtr=NULL;
Temp->prevPtr=NULL;
免费(临时);
}
//historyTemp=NULL;
}
作废打印历史(){
int计数器=1;
结构历史*historyT=NULL;
历史t=头;
while(historyT!=NULL){
printf(“[%d][%s]\n”,计数器,historyT->hname);
historyT=historyT->nextPtr;
计数器++;
}
}
无效设置(){
而(1){
而(!feof(stdin))
{
fgets(str,sizeof(str),stdin);
组织序列(str);
}
}
}
int main(int argc,char const*argv[]
{
设置();
返回0;
}
通常,在程序从用户处获得第二次输入后,它只应更改historyTemp的hname


但它也会改变头部和尾部。我怎样才能改变它?我做错了什么?提前感谢。

您没有为historyName分配任何内存

而不是

historyTemp->hname=historyName;

您可以在至少一个位置指定Head->previous。毫无意义

不能完全理解你的插入逻辑吗

这是我要做的

假设尾部插入

我会做临时工

如果是第一次插入,则将磁头设置为temp

设置尾部->温度旁边 将temp->PREVICE设置为Tail 将临时->设置为空


希望这有帮助

帮助很大。谢谢
historyTemp->hname=strdup(historyName);