我正在尝试创建一个dequeed链表。我创建了一个添加单词的单词。因为我改变了列表的头部和尾部,所以我试图通过引用来调用,所以我可以改变列表的头部和尾部。我不确定我做错了什么,因为它没有改变函数之外的地址。
发送时head
和tail
为NULL
。 head
和tail
在函数中获取地址,然后在离开函数head
和tail
后再次成为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 :(得分:0)
你所展示的内容对我来说很合适。 head
退出后,tail
和add_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()
函数未修改head
中声明的tail
和main()
变量。您是按值而非指针传递这些变量,就像使用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;
}