我想创建一个链接列表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;
}
}
我可以问你为什么你用'char []'和你自己的节点实现,而不是'std :: list' /'std :: forward_list'和'std: :string'? – Shoe
不允许在此作业上使用字符串。 – user3304639
请修复您代码的缩进。 – zoska