2014-03-05 46 views
0

我想创建一个链接列表dequeed。我创建了我的添加单词,添加了一个单词。因为我改变了列表的头部和尾部,我试图使用引用调用,所以我可以改变列表的头部和尾部。我不确定我在做什么错误,因为它没有改变函数之外的地址。 headtail当它们被发送时是NULLheadtail在功能中获得地址,然后在离开功能headtail后再次变为NULL。 我不明白为什么会发生这种情况?谢谢你的帮助。 我添加了整个程序!它似乎工作正常!但它不会改变函数外部的尾部和节点。 我原来有全球变数的程序,然后我改变了这个,因为老师不喜欢他们。通过引用调用

#include <iostream> 
#include <cstdlib> 
#include <fstream> 
#include <stdio.h> 
#include <cstring> 
#include <strings.h> 
#include <cctype> 

struct node 
{ 
char word [10]; 
node *next; 
node *prev; 
}; 


using namespace std; 


int display(); 
void switcheroo (int, struct node*, struct node*); 
void add_Word (char [], struct node**, struct node**); 
void delete_Word (char [], struct node **, struct node **); 
void display_Queue (struct node*); 
void display_Stack (struct node*); 
void search_List (char[], struct node*); 
struct node* makeNode (void); 

int main () 
{ 

system ("clear"); 
int pick; 
struct node *head; 
struct node *tail; 
head = NULL; 

cout << endl << head << " first call"<< endl; 

while (pick != 6) 
{ 
cout << endl << head << " 888888" << endl; 
pick = display(); 
switcheroo (pick, head, tail); 
} 

return 0; 
} 

/*******************************************************************/ 

int display() 
{ 

int user_choice; 

cout <<"What would you like to do?\n\n"; 
cout <<"Press 1 Add a word\n"; 
cout <<"Press 2 Delete a word\n"; 
cout <<"Press 3 Display the data in Queue order\n"; 
cout <<"Press 4 Display the data in stack order\n"; 
cout <<"Press 5 Search the list for a word\n"; 
cout <<"Press 6 Quit\n"; 
cin >> user_choice; 

    return user_choice; 
} 
/********************************************************************/ 
void switcheroo (int pick, struct node* head, struct node*tail) 
{ 
char word [10]; 
switch (pick) 
    { 
case 1: 
    cout << "Please enter a word:\n"; 
    cin >> word; 
    add_Word(word, &head, &tail); 
    break; 
case 2: 
    display_Queue(head); 
    cout << "\n\nEnter a word to delete\n"; 
    cin >> word; 
    delete_Word(word, &head, &tail); 
    break; 
case 3: 
    display_Queue(head); 
    break; 
case 4: 
    display_Stack(tail); 
    break; 
case 5: 
    display_Queue(head); 
    cout << "\n\nWhat word would you like to search for?\n"; 
    cin >> word; 
    search_List(word, tail); 
    break; 
case 6: 
    return; 
    } 
} 

/*******************************************************************/ 
void add_Word (char word[], struct node **head, struct node **tail) 
{ 
struct node *var, *temp; 
var = makeNode(); 
strcpy (var->word, word); 

cout << endl << *head << endl; 
cout << endl << *tail << endl; 

if (*head == NULL) 
{ 
*head = var; 
cout << *head << endl; 
(*head)->prev = NULL; 
(*head)->next = NULL; 
tail = head; 


cout << endl << "00000" << *head << endl; 
} 
else 
{ 
cout << "--------------" << endl; 
temp = var; 
temp->prev = NULL; 
temp->next = *head; 
(*head)->prev = temp; 
*head = temp; 
} 

} 
/*******************************************************************/ 


struct node* makeNode (void) 
{ 
struct node* newptr; 

newptr = new struct node; 
if (newptr) 
newptr->next = NULL; 

return newptr; 
} 

/*******************************************************************/ 

void display_Queue(struct node * x) 
{ 
int n = 01; 
while (x != NULL) 
{ 
cout << "---" << x->word << "---" << n << endl; 
n++; 
x = x->prev; 
} 

return; 
} 
/*******************************************************************/ 
void display_Stack (struct node *x) 
{ 
int n=1; 
while (x != NULL) 
{ 
cout << "---" << x->word << "---" << n << endl; 
n++; 
x = x->next; 
} 

return; 
} 
/*******************************************************************/ 
void delete_Word(char word [], struct node **head, struct node **tail) 
{ 

struct node *palabra, *x; 
int n = 0; 
x = *head; 
char copy [10]; 
while (x != NULL) 
{ 
strcpy (copy,x->word); 
cout << "!!!!!!!!!!" << x->word << endl; 
if (strcasecmp (x->word, word)==0) 
{ 
    cout << "**********"<< endl; 
palabra = x; 
n++; 
} 
x = x->next; 
} 

if (n==0) 
cout << "NO word to delete!!! try again"; 


if (n==1) 
{ 
if ((palabra->next == NULL) && (palabra->prev == NULL)) 
{ 
delete (palabra); 
*head = NULL; 
*tail = NULL; 
} 
else if (palabra->next == NULL) 
{ 
*tail = palabra->prev; 
(*tail)->next = NULL; 
delete (palabra); 
} 
else if(palabra->prev == NULL)  
{ 
*head = palabra->next; 
(*head)->prev = NULL; 
delete (palabra); 
} 
else if((palabra->next != NULL)&&(palabra->prev!= NULL)) 
{ 
palabra->next->prev = palabra->prev; 
palabra->prev->next = palabra->next; 
delete (palabra); 
} 
} 
} 
/*******************************************************************/ 
void search_List (char word[], struct node *x) 
{ 
int n =1; 
cout << endl << word << endl; 


while (x != NULL) 
{ 
    if (strcasecmp (x->word, word)==0) 
    { 
    cout << "Your word has been found\n"; 
    cout << word << " is number: " << n << " in the list\n\n"; 
    } 
n++; 
x = x->prev; 
} 
} 
+0

我可以问你为什么你用'char []'和你自己的节点实现,而不是'std :: list' /'std :: forward_list'和'std: :string'? – Shoe

+0

不允许在此作业上使用字符串。 – user3304639

+0

请修复您代码的缩进。 – zoska

回答

0

你已经显示出来的效果对你来说很好,因为你已经展示了它。如预期的那样,headtailadd_Word()退出之后不再为NULL。

就这么说,你的逻辑有点倒退了。您正在以相反的顺序添加单词 - 新单词出现在列表中较早的单词之前,而不是出现在单词之后。这是你真正想要的吗?如果没有,那么你的逻辑应该是更喜欢这个:

struct node* makeNode() 
{ 
    struct node *n = (struct node*) malloc(sizeof(struct node)); 
    memset(n, 0, sizeof(struct node)); 
    return n; 
} 

void add_Word(char word[], struct node** head, struct node** tail) 
{ 
    struct node* var = makeNode(); 
    strcpy(var->word, word); 

    if (*head == NULL) 
     *head = var; 

    if (*tail != NULL) 
    { 
     var->prev = *tail; 
     (*tail)->next = var; 
    } 

    *tail = var; 
} 

更新:其实,你原来add_Word()确实有一个错误我错过了。它没有正确更新tail。您需要取消引用tail参数以更新外部变量以指向新的node。您改为将本地tail参数add_Word()更新为指向本地head参数指向的相同地址。不是一回事。

此外,在您最新的代码更新中,您的switcheroo()函数未修改在main()中声明的headtail变量。您正在通过值而不是指针传递这些变量,就像您使用add_Word()delete_Word()一样。因此,当switcheroo()调用add_Word()delete_Word()时,它们所做的更改仅适用于本地参数switcheroo(),并且不会传播到main()

您的代码还有其他问题。试试更像这样的东西:

#include <iostream> 
#include <cstdlib> 
#include <fstream> 
#include <stdio.h> 
#include <cstring> 
#include <strings.h> 
#include <cctype> 
#include <limits> 

struct node 
{ 
    char word [10]; 
    node *next; 
    node *prev; 
}; 

using namespace std; 


int display(); 
void switcheroo (int, struct node**, struct node**); 
void add_Word (char [], struct node**, struct node**); 
void delete_Word (char [], struct node **, struct node **); 
void display_Queue (struct node*); 
void display_Stack (struct node*); 
void search_List (char[], struct node*); 
struct node* makeNode (void); 
void destroyNode (struct node*, struct node**, struct node**); 

int main() 
{ 
    system ("clear"); 

    struct node *head = NULL; 
    struct node *tail = NULL; 
    struct node *temp; 
    int pick; 

    do 
    { 
     pick = display(); 
     switcheroo (pick, &head, &tail); 
    } 
    while (pick != 6); 

    while (head != NULL) 
     destroyNode (head, &head, &tail); 

    return 0; 
} 

/*******************************************************************/ 

int display() 
{ 
    int user_choice; 

    do 
    { 
     cout << endl; 
     cout << "What would you like to do?" << endl << endl; 
     cout << "Press 1 Add a word" << endl; 
     cout << "Press 2 Delete a word" << endl; 
     cout << "Press 3 Display the data in Queue order" << endl; 
     cout << "Press 4 Display the data in Stack order" << endl; 
     cout << "Press 5 Search the list for a word" << endl; 
     cout << "Press 6 Quit" << endl; 

     if (cin >> user_choice) 
     { 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 

      if ((user_choice >= 1) && (user_choice <= 6)) 
       break; 
     } 
     else 
     { 
      cin.clear(); 
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
     } 

     cout << "That is not a valid choice. Try again." << endl << endl; 
    } 
    while (true); 

    return user_choice; 
} 
/********************************************************************/ 
void switcheroo (int pick, struct node** head, struct node** tail) 
{ 
    char word [10]; 
    switch (pick) 
    { 
     case 1: 
      cout << "Please enter a word:" << endl; 
      cin.getline(word, 10); 
      if (cin.fail()) 
      { 
       cin.clear(); 
       cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      } 
      add_Word(word, head, tail); 
      break; 
     case 2: 
      display_Queue(*tail); 
      cout << endl << endl << "Enter a word to delete" << endl; 
      cin.getline(word, 10); 
      if (cin.fail()) 
      { 
       cin.clear(); 
       cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      } 
      delete_Word(word, head, tail); 
      break; 
     case 3: 
      display_Queue(*tail); 
      break; 
     case 4: 
      display_Stack(*head); 
      break; 
     case 5: 
      display_Queue(*tail); 
      cout << endl << endl << "What word would you like to search for?" << endl; 
      cin.getline(word, 10); 
      if (cin.fail()) 
      { 
       cin.clear(); 
       cin.ignore(numeric_limits<streamsize>::max(), '\n'); 
      } 
      search_List(word, *tail); 
      break; 
     case 6: 
      return; 
    } 
} 

/*******************************************************************/ 
void add_Word (char word[], struct node **head, struct node **tail) 
{ 
    struct node *var = makeNode(); 
    strncpy (var->word, word, 10); 

    var->next = *head; 
    if (var->next != NULL) 
     var->next->prev = var; 
    *head = var; 

    if (*tail == NULL) 
     *tail = var; 
} 
/*******************************************************************/ 

struct node* makeNode (void) 
{ 
    struct node* newptr = new struct node; 
    memset(newptr, 0, sizeof(struct node)); 
    return newptr; 
} 

/*******************************************************************/ 

void destroyNode (struct node *x, struct node** head, struct node** tail) 
{ 
    if (x == NULL) 
     return; 

    if (x->next != NULL) 
     x->next->prev = x->prev; 

    if (x->prev != NULL) 
     x->prev->next = x->next; 

    if (x == *head) 
     *head = x->next; 

    if (x == *tail) 
     *tail = x->prev; 

    delete x; 
} 

/*******************************************************************/ 

void display_Queue(struct node * x) 
{ 
    int n = 0; 
    while (x != NULL) 
    { 
     n++; 
     cout << "---" << x->word << "---" << n << endl; 
     x = x->prev; 
    } 

    return; 
} 
/*******************************************************************/ 
void display_Stack (struct node *x) 
{ 
    int n = 0; 
    while (x != NULL) 
    { 
     n++; 
     cout << "---" << x->word << "---" << n << endl; 
     x = x->next; 
    } 

    return; 
} 
/*******************************************************************/ 
void delete_Word(char word [], struct node **head, struct node **tail) 
{ 
    struct node *x = *head; 
    struct node *temp; 
    int n = 0; 

    while (x != NULL) 
    { 
     cout << "!!!!!!!!!!" << x->word << endl; 
     if (strncmpi (x->word, word, 10)==0) 
     { 
      cout << "**********" << endl; 

      temp = x->next; 
      destroyNode (x, head, tail); 
      x = temp; 

      n++; 
     } 
     else 
      x = x->next; 
    } 

    if (n==0) 
     cout << "NO word deleted!!! try again" << endl; 
} 
/*******************************************************************/ 
void search_List (char word[], struct node *x) 
{ 
    int n = 0; 
    int found = 0; 

    cout << "Searching for: " << word << endl; 

    while (x != NULL) 
    { 
     n++; 
     if (strcmpi (x->word, word)==0) 
     { 
      cout << "Your word has been found" << endl; 
      cout << word << " is number " << n << " in the list" << endl; 
      ++found; 
     } 
     x = x->prev; 
    } 

    if (found==0) 
     cout << "NO word found!!! try again" << endl; 
} 
+0

HEAD和TAIL在功能上发生了变化!但是一旦他们离开函数HEAD和TAIL就回到NULL,然后整个程序就搞砸了。 – user3304639

+0

当函数退出时,前面显示的'add_Word()'代码可能使它们回到NULL。唯一的办法是如果你自己的代码在函数退出后的稍后时间重置它们。通过调试器运行你的代码,你会看到到底发生了什么。 –

+0

你的'switcheroo()'函数有一个bug,它可以防止在'main()'中声明'head'和'tail'变量的变化。 –