Your program will use a linked list. The program is to maintain a sorted list of
ID: 3560682 • Letter: Y
Question
Your program will use a linked list. The program is to maintain a sorted list of integers. The program will read integers froma file named intInput.dat and create the original inked list of values--the numbers in the input file are already in sorted sequence. After a succesfull creation of the list the prgoram will give the user a choice of adding an integer, deleting an integer, searching for an integer to see if it is the list, print the list to the screen, determine the length of the list, or exit the program. When the user exits the program the linked list will be written to an output file named intOutput.dat
IMPORTANT
Run the program at least three times (wher intOUputdat of run 1 becomes intInput.dat in run 2 and intOutput.dat of run 2 becomes intInput.dat for run 3. On your screen prints display contents of flies between runs.
Explanation / Answer
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
typedef struct node{
int data;
struct node *next;
}Node;
class Linkedlist{
Node *head;
public:
Linkedlist(){
head = NULL;
}
void append(int);
void search(int);
void remove(int);
void display(void);
void sort(void);
void length(void);
void read(void);
void write(void);
};
/* add a node at the end of the list */
void Linkedlist :: append(int v){
Node *temp, *Node_ptr;
temp = new Node;
temp->data = v;
temp->next = NULL;
if(head == NULL)
head = temp;
else
{
Node_ptr = head;
while(Node_ptr->next!=NULL)
Node_ptr = Node_ptr->next;
Node_ptr->next = temp;
}
}
/* display the whole linked list */
void Linkedlist :: display(){
Node *Node_ptr;
Node_ptr = head;
if(Node_ptr != NULL){
if((Node_ptr->next == NULL))
cout << " " << Node_ptr->data;
else
{
while(Node_ptr->next!=NULL)
{
cout << " " << Node_ptr->data;
Node_ptr = Node_ptr->next;
}
cout << " " << Node_ptr->data;
}
}else{
cout << "Empty list." << endl;
}
}
/* length of linked list */
void Linkedlist :: length(){
Node *Node_ptr;
Node_ptr = head;
int count = 0;
if((Node_ptr->next == NULL))
cout << "Length : " << count << endl;
else
{
while(Node_ptr != NULL)
{
Node_ptr = Node_ptr->next;
count++;
}
cout << "Length : " << count << endl;
}
}
/* search for a particular value of a node in the list */
void Linkedlist :: search(int v){
int pos = 1, found = 0;
Node *Node_ptr;
Node_ptr = head;
while(Node_ptr!=NULL)
{
if(Node_ptr->data == v)
{
found = 1;
cout << v << " is found at position " << pos;
}
pos++;
Node_ptr = Node_ptr->next;
}
if(found != 1)
cout << v << " is not found in the list";
}
/* delete a specified node */
void Linkedlist :: remove(int v){
Node *Node_ptr, *temp;
int found = 0;
Node_ptr = head;
if(Node_ptr->data == v && Node_ptr->next == NULL)
{
delete head;
head = NULL;
found =1;
}
else
{
while(Node_ptr->next!=NULL)
{
if(Node_ptr->data == v && Node_ptr == head)
{
head = head->next;
found = 1;
}
else if(Node_ptr->next->data == v)
{
if(Node_ptr->next->next == NULL)
{
temp = Node_ptr->next;
Node_ptr->next = NULL;
found = 1;
break; /* Because the code Node_ptr = Node_ptr->next will cause an error */
}
else
{
temp = Node_ptr->next;
Node_ptr->next = Node_ptr->next->next;
}
delete temp;
found = 1;
}
Node_ptr = Node_ptr->next;
}
}
if(found == 0){
cout << v << " is not found in the list";
} else {
cout << v << " has been deleted!";
}
}
void Linkedlist :: sort(){
Node *Node_ptr = head;
int temp, j = 1, i;
while(Node_ptr!=NULL)
{
Node_ptr = Node_ptr->next;
j++;
}
for(i = 0; i < j; i++)
{
for(Node_ptr = head;Node_ptr->next != NULL;Node_ptr = Node_ptr->next)
{
if(Node_ptr->data > Node_ptr->next->data)
{
temp = Node_ptr->data;
Node_ptr->data = Node_ptr->next->data;
Node_ptr->next->data = temp;
}
}
}
}
void Linkedlist :: read(){
int v;
string line;
ifstream input;
stringstream ss;
input.open("intInput.dat");
if(input.is_open()){
while(getline(input, line)){
ss << line;
ss >> v;
ss.clear();
this->append(v);
}
}
}
void Linkedlist :: write(){
ofstream output;
output.open("intOutput.dat");
this->sort();
if(output.is_open()){
Node *Node_ptr;
Node_ptr = head;
if(Node_ptr != NULL){
while(Node_ptr != NULL)
{
output << Node_ptr->data << " ";
Node_ptr = Node_ptr->next;
}
}
}
}
int main(){
Linkedlist L;
unsigned int c;
int v;
L.read();
cout << " A Singly Linked List Program" << endl;
cout << " Menu ";
cout << " 1. Add item to list ";
cout << " 2. Display the list ";
cout << " 3. Delete an item from list ";
cout << " 4. Search an item in the list ";
cout << " 5. Length of list ";
cout << " 6. Exit ";
cout << " Enter your choice : [1 - 6] > ";
cin >> c;
do{
switch(c){
case 1:
cout << " Data Item : ";
cin >> v;
L.append(v);
break;
case 2:
cout << " Data Items: ";
L.display();
break;
case 3:
cout << " Data Item to be deleted > ";
cin >> v;
L.remove(v);
break;
case 4:
cout << " Data Item to be searched > ";
cin >> v;
L.search(v);
break;
case 5:
cout << " Length of list > ";
L.length();
break;
case 6:
break;
default:
cout << " Invalid option";
break;
}
cout << " Enter your choice : [1 - 6] > ";
cin >> c;
}while(c != 6);
L.write();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.