/*
* Chapter 15, Problem 1
*
* Write a program to remove an element from a linked list; the remove
* function should take just the element to be removed. Is this function
* easy to write and will it be fast? Could this be made easier or faster
* by adding an additional pointer to int?
*
* Problem 2
*
* Write a program that adds elements to a linked list in sorted order, rather
* than at the begining.
*
* Problem 3
*
* Write a program to find an element in a linked list by name.
*
*/
#include <iostream>
#include "c15p1.h"
usingnamespace std;
// Function Prototypes
void addHead(LinkedList *list, int* data);
void initializeList(LinkedList* list);
void displayLinkedList(LinkedList *list);
int main()
{
// Declare and initialize variables
int x = 0;
int choice = 0;
LinkedList linkedList;
initializeList(&linkedList);
while (true)
{
// Print Menu
cout << "Menu:" << endl <<
'\t' << "0. Exit" << endl <<
'\t' << "1. Add a node " << endl <<
'\t' << "2. Delete a node " << endl <<
'\t' << "3. Display node(s) " << endl << endl;
cout << "Select a number from the Menu: ";
cin >> choice;
switch (choice)
{
case 0:
return 0;
case 1:
cout << "Please enter an integer: ";
cin >> x;
addHead(&linkedList, &x);
break;
case 2:
break;
case 3:
displayLinkedList(&linkedList);
break;
}
}
}
void addHead(LinkedList* list, int* data)
{
Node* node = (Node*)malloc(sizeof(Node));
node->x_node = data;
if (list->head == NULL)
{
list->tail = node;;
node->next = NULL;;
}
else
{
node->next = list->head;
}
list->head = node;
}
void initializeList(LinkedList* list)
{
list->head = NULL;
list->tail = NULL;
list->current = NULL;
}
void displayNode(Node* node)
{
cout << node->x_node << endl;
}
void displayLinkedList(LinkedList* list)
{
cout << endl << "Linked List \n";
Node* current = list->head;
while (current != NULL)
{
cout << current->x_node << endl;
current = current->next;
}
}
Menu:
0. Exit
1. Add a node
2. Delete a node
3. Display node(s)
Select a number from the Menu: 1
Please enter an integer: 5
Menu:
0. Exit
1. Add a node
2. Delete a node
3. Display node(s)
Select a number from the Menu: 3
Linked List
0036FE60
Menu:
0. Exit
1. Add a node
2. Delete a node
3. Display node(s)
Select a number from the Menu: