Question

I am creating a program to sort a list of names from a given text file...

I am creating a program to sort a list of names from a given text file using linked listed.

The text file would look something like this:

3 John Johnson

1 Tim Boring

1 Jason Mendoza

1 Bobert Reed

2 Tim Boring

4

5

Essentially I would like to associate each numerical value with an action: 1 = add a person, 2 = remove person, 3 = check if that name is in the list, 4 = print list, 5 = exit.

I've been having trouble sorting the list once the linked list is established.

Here's an example of what I was going for:

void sortList(node *head)

{

node *i, *j;

int temp;

for(i=head; i->next!=NULL; i=i->next)

{

for(j=i->next; j!=NULL; j=j->next)

{

if(i->data > j->data)

{

temp = i->data

i->data = j->data;

j->data = temp;

}

}

}

}

general assistance of the overall program and samples would be greatly appreciated.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

struct Node{
   string data;
   struct Node*next;
};

void insert(struct Node **head, string data)
{
struct Node *ptr1 = new struct Node;
ptr1->data = data;
ptr1->next = *head;
*head = ptr1;
}
void remove(struct Node **head, string key)
{
struct Node* temp = *head, *prev;
  
if (temp != NULL && temp->data == key)
{
*head = temp->next;   
delete(temp);
return;
}
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
  
if (temp == NULL) {
   cout<<key<< " not found in list\n";
   return;
   }
prev->next = temp->next;
  
delete(temp);
}

void printList(struct Node *head)
{
struct Node *temp = head;
cout<<"\n";
while (temp!=NULL)
{
cout<< temp->data<<" ";
temp = temp->next;
}
}

bool search (struct Node*head , string key){
   struct Node*temp=head;
  
   while(temp){
       if(temp->data==key)return true;
       temp=temp->next;
   }
   return false;
}

/* function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b)
{
string temp = a->data;
a->data = b->data;
b->data = temp;
}


void bubbleSort(struct Node *head)
{
int swapped, i;
struct Node *ptr1;
struct Node *lptr = NULL;
  
if (!head)
return;
  
do
{
swapped = 0;
ptr1 = head;
  
while (ptr1->next != lptr)
{
if (ptr1->data > ptr1->next->data)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}
while (swapped);
}

int main(){
   int choice;
   string name;
   struct Node *head = NULL;
   do{
       cout<<"1 = add a person\n2 = remove person\n3 = check if that name is in the list\n4 = print list\n5 = exit\n";
       cout<<"Enter choice : ";
       cin>>choice;
       switch(choice){
           case 1:{
               cout<<"Enter name: ";
               cin.ignore();
               getline(cin,name);
               insert(&head,name);
               break;
           }
           case 2:{
               cout<<"Enter name: ";
               cin.ignore();
               getline(cin,name);
               remove(&head,name);
               break;
           }
           case 3:{
               cout<<"Enter name: ";
               cin.ignore();
               getline(cin,name);
               if(search(head,name))cout<<name<<" is in the list\n";
               else cout<<name<<" is not in the list\n";
               break;
           }
           case 4:{
               cout<<"Your list is : \n";
               printList(head);
               cout<<"\n";
              
               break;
           }
           case 5:{
               cout<<"Goodbye!\n";
               break;
           }
           default:{
               cout<<"Invalid choice\n";
               cout<<"Re-enter your choice\n";
               break;
           }
       }
      
   }while(choice!=5);
  
   return 0;
}
  

Add a comment
Know the answer?
Add Answer to:
I am creating a program to sort a list of names from a given text file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am losing elements when I sort my Linked List. How can I sort my Linked...

    I am losing elements when I sort my Linked List. How can I sort my Linked list in alphabetical order? Code: void sort(Node *head) { Node* temp = head; Node* tempAhead = temp -> next; Node* buff1 = temp; Node* buff2 = tempAhead; while (temp -> next != NULL) { while (tempAhead -> next != NULL) { if (strcmp(temp -> name, tempAhead -> name) < 0) { buff1 -> next = temp -> next; buff2 -> next = tempAhead ->...

  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a...

    Writing a method retainAll for Circular Doubly-Linked List: I am working on an assignment creating a Circular Doubly Linked List and am having serious trouble creating a method retainAll. Here's the code, and my attempt. Initialization: public class CDoublyLinkedList {    private class Node {        private Object data; //Assume data implemented Comparable        private Node next, prev;        private Node(Object data, Node pref, Node next)        {            this.data = data;       ...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • 5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following...

    5.8 Sort Linked List Use mergersort to sort a linked list Do not use the following libraries: algorithm, cmath. Do not load the values into a vector and sort them. The sort must be done with a linked list Example Two lists 6->3->1->2->4->5 will return a list with 1->2->3->4->5->6. Input There will be no input read in anymore. Going forward use the hard-coded input on the template and ensure the program works. There will be one compare output test based...

  • c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file...

    c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT