c ++作业 - 在链接列表代码中初始化节点的正确方法



我编写了以下代码来实现链接列表,它在我的Windows计算机上编译并运行(使用mingw 32)g ++ main.cpp如果我在 Linux 上编译,它会给我分段错误,所以我使用 -Wall 标志编译,看起来我有未初始化的变量

head->next= NULL;
^ backup.cpp: In function 'int main()': backup.cpp:37:17: warning: 'head' is used uninitialized in this function [-Wuninitialized]  
listInsert(head,prev,choice);
                ^ backup.cpp:53:32: warning: 'prev' may be used uninitialized in this function [-W maybe-uninitialized] 

不过,我尝试解决此问题会导致整个代码停止工作。任何帮助/解释表示赞赏。

#include <string>
#include <set>
#include <iostream>
#include <sstream>
#include <cstring>
#include <stdlib.h>
#include <ctime>
using namespace std;
//struct
struct ListNode{
int data;
ListNode *next;
};
//function prototypes
ListNode * createNode(int elem); //allocate node
ListNode * listInsert(ListNode *head, ListNode *prev, int elem); //insert an element
ListNode * listDelete(ListNode *head, int elem); //delete an element
ListNode * listSearch(ListNode *head, int elem); //search for an element
void listPrint(ListNode *head); //print csv list of elements
void listDeallocate(ListNode *head); //delete list
int menu();
double input_num(string input );
void fill_list(ListNode *head,ListNode *prev);
int RandomNumber(int min, int max);
int main()
{
ListNode *head;
ListNode *prev;
head->next= NULL;
    int choice=6;
    while (choice!=0)
     {
     choice=menu();
     if(choice==1)
     {
     choice =input_num("nEnter value to search for : ");
     listSearch(head,choice);
     choice=6;
     }
     if(choice==2)
         {
            choice =input_num("nEnter value to insert : ");
            listInsert(head,prev,choice);
            choice=6;
        }   
     if (choice==3)
     {
     choice =input_num("nEnter value to delete: ");
     listDelete(head,choice);
     choice=6;
     }
     if(choice==4)
     {
     listPrint(head);
     choice=6;
     }
     if(choice==5)
     {
     fill_list(head,prev);
     choice=6;
     }
     }
return 0;
}
//***********************FUNCTIONS**********************************************
ListNode * listDelete(ListNode *head, int elem)
{   
int count=0;
int result=0;
    ListNode *p = head->next;
    //ListNode *temp;
    cout<<"n";
    while(p != NULL) {
            //cout<<p->data<<", ";
            if(p->next->data==elem)
            {result++;
            break;
            }
            else
            p = p->next;
            count++;
        }
    if(result==0)
    {
    cout<<"Nothing to deleten";
      return p;
     } 
    if(p->next != NULL) 
    {
            ListNode *temp = p->next;
            //cout<<p->next->next;
            p->next = p->next->next;
            free(temp);
    }
    return p;
}
//******************************************************************************
void listPrint(ListNode *head)
{
  ListNode *p = head->next;
  cout<<"n";
        while(p != NULL) {
            cout<<p->data<<" , ";
            p = p->next;
        }
        cout<<"nn";
}
//******************************************************************************
ListNode* listInsert(ListNode *head, ListNode *prev, int elem)
{
ListNode *p;
        p = new ListNode;
        p->data = elem;
        p->next = head->next;
        head->next = p;
 return head;
}
//******************************************************************************
ListNode * listSearch(ListNode *head, int elem)
{
int count=0;
int result=0;
    ListNode *p = head->next;
    cout<<"n";
        while(p != NULL) {
            //cout<<p->data<<", ";
            if(p->data==elem)
            {result++;
            break;
            }
            p = p->next;
            count++;
        }
        if (result>0)
        cout<<"nFound "<<elem<<" in position "<<count+1;
        else
        cout<<"no result";
        cout<<"nn";
    return p;
}
//******************************************************************************
ListNode *createNode(int elem) //allocate node
{
ListNode *newnode;
 return newnode;
}
//******************************************************************************
int menu()
{
int choice=6;
while(choice<0||choice>5)
{
cout<<"nn Linked List Menu Options n";
cout<<"=======================================n";
cout<<"1. Search for an element in the listn";
cout<<"2. Add an element to the listn";
cout<<"3. Delete an element from the listn";
cout<<"4. Print the listn";
cout<<"5. Fill array with random numbers n";
cout<<"0. Exitn";
cout<<"======================================n";
choice =input_num(":");
}
return choice;
}
//********************************************************************************
double input_num(string input) //accepts user input returns a double in number
{                                
    bool cont=true;
    double number=0;
    char user_num[100];
    cout  << input;
    while (cont==true)
      {
    getline (cin,input);
    strcpy(user_num, input.c_str());
    number=atof(user_num);
        if((number==0)&(input[0]!='0'))
        {
            cont=true;
            cout<< "Please enter a number:";
        } 
    else cont=false;
}
    return number;
}
//*************************************************************************************
void listDeallocate(ListNode *head)
{
    while(head != NULL)
    {
    ListNode *temp =head;
    // If only one item in the list, delete it and empty the list...
    if(head->next == NULL) {
        delete head;
        head = NULL;
        temp = NULL;
        return;
    }
    // Find the last item in the list
    while(temp->next!=NULL)
    {
        temp=temp->next;
    }
    delete temp;
    cout<<"deletedn";
    }
}
//******************************************************************************************
int RandomNumber(int min, int max)
{  
    srand(time(0));
    return ( ( rand() % (max-min+1) ) + min);
}
//*******************************************************************************************
void fill_list(ListNode *head,ListNode *prev)
{int choice=0;
 int elem;
choice =input_num("nEnter number of elements : ");
while(choice>0)
{
elem=RandomNumber(choice,99);
listInsert(head,prev,elem);
choice--;
}
}
好的,

我现在明白了:第一个固定的创建节点函数

    ListNode *createNode(int elem) //allocate node
{
ListNode *newnode;
newnode = new ListNode;
newnode->next=newnode;
 return newnode;
}

然后通过调用函数将头分配给新节点

ListNode *head;
    head=createNode(0);
head->next= NULL;

实际上createNode()分配一个ListNode并返回其地址。

最新更新