I
israphelr
Hi people,
I am in the process of writing a linked list (for a learning exercise)
and below is what I've written so far.
When I declare a string variable outside of any functions, the program
works as expected, however once I declarea string in the main
function, when I run the program I get a segmentation fault. I needn't
even do anything with the string, all I have to do is declare it, then
run it/
/
*************************************************************************/
#include <iostream>
#include <string>
using namespace std;
struct node
{
string data;
node *next; //pointer to a node.
};
typedef node* PNode;
void initializeList(PNode* a);
void addNode(PNode* a);
void displayList(PNode a);
void deleteNode(PNode *a);
//bool isInList(string b); IMPLEMENT LATER.
string s;//works if declared outside main as a global, but not inside
main.
int main()
{
PNode head;
char choice;
initializeList(&head);
do {
addNode(&head);
cout << "Another? 'Y' for yes, Press 'q' to quit\n";
cin >> choice;
} while (choice != 'q' && choice != 'Q');
displayList(head);
deleteNode(&head);
displayList(head);
cout << "Enter the name you would like to search:\n";
return 0;
}
void initializeList(PNode* a)
{
a = NULL;
}
void addNode(PNode* head)
{
PNode tmp; //new node.
tmp = new node; //create a new node and point to it using tmp.
cout << "Enter your name: \n";
cin >> tmp->data;
if (*head == NULL)
{
tmp->next = NULL;
*head = tmp;
}
else
{
tmp->next = *head; //temp node point to the node pointed to by
head
*head = tmp; //head points to the first node
}
}
void displayList(PNode head)
{
PNode current;
current = head;
do {
cout << current->data << endl;
current = current->next;
} while (current->next != NULL);
}
//list is not empty/has more then 1 node, but will deal with this
later.
void deleteNode(PNode *a)
{
PNode current, previous;
//delete from front.
current = *a; //current now points to what head points to.
*a = current->next; //head points to the node pointed to by current.
delete current;
current = NULL;
}
/
*************************************************************************/
Thanks in advance for your help.
I am in the process of writing a linked list (for a learning exercise)
and below is what I've written so far.
When I declare a string variable outside of any functions, the program
works as expected, however once I declarea string in the main
function, when I run the program I get a segmentation fault. I needn't
even do anything with the string, all I have to do is declare it, then
run it/
/
*************************************************************************/
#include <iostream>
#include <string>
using namespace std;
struct node
{
string data;
node *next; //pointer to a node.
};
typedef node* PNode;
void initializeList(PNode* a);
void addNode(PNode* a);
void displayList(PNode a);
void deleteNode(PNode *a);
//bool isInList(string b); IMPLEMENT LATER.
string s;//works if declared outside main as a global, but not inside
main.
int main()
{
PNode head;
char choice;
initializeList(&head);
do {
addNode(&head);
cout << "Another? 'Y' for yes, Press 'q' to quit\n";
cin >> choice;
} while (choice != 'q' && choice != 'Q');
displayList(head);
deleteNode(&head);
displayList(head);
cout << "Enter the name you would like to search:\n";
return 0;
}
void initializeList(PNode* a)
{
a = NULL;
}
void addNode(PNode* head)
{
PNode tmp; //new node.
tmp = new node; //create a new node and point to it using tmp.
cout << "Enter your name: \n";
cin >> tmp->data;
if (*head == NULL)
{
tmp->next = NULL;
*head = tmp;
}
else
{
tmp->next = *head; //temp node point to the node pointed to by
head
*head = tmp; //head points to the first node
}
}
void displayList(PNode head)
{
PNode current;
current = head;
do {
cout << current->data << endl;
current = current->next;
} while (current->next != NULL);
}
//list is not empty/has more then 1 node, but will deal with this
later.
void deleteNode(PNode *a)
{
PNode current, previous;
//delete from front.
current = *a; //current now points to what head points to.
*a = current->next; //head points to the node pointed to by current.
delete current;
current = NULL;
}
/
*************************************************************************/
Thanks in advance for your help.