2013-12-01 2 views
0

Я хочу написать программу, которая берет последние запрошенные данные от пользователя и печатает их по порядку. Вот фрагмент кода, который у меня есть проблема о:Вставка связанного списка в C

#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; 
} 

Обычно после того, как программа получает второй вход от пользователя он должен изменить только hname в historyTemp в.

Но он меняет голову и хвост. Как я могу его изменить? Что я делаю не так? Заранее спасибо.

ответ

1

Вы не выделяя какой-либо памяти HistoryName

Вместо

historyTemp->hname=historyName; 

сделать

historyTemp->hname=strdup(historyName); 

Вы назначаете Глава-> предыдущий, по крайней мере в одном месте. Без разницы.

Не можете понять свою логику для вставки?

Вот что я хотел бы сделать

Предполагая, что введение в хвост

Я бы ТЕмп

если это первый вкладыш, установите головку на темп

набор Tail-> рядом с temp установить temp-> предыдущий к хвосту установить temp-> рядом с NULL

Надежда это помогает

+0

Это очень помогло.Спасибо. – Mertcan

Смежные вопросы