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.
//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;
}
I am creating a program to sort a list of names from a given text file...
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 ->...
Need some help creating a bubble sort for my linked list in C. Here are the structs used in my code: struct simulation { void *list; int et; /* in seconds */ }; struct plane { double x, y, altitude; char callsign[15]; struct simulation *sim; }; Here is the struct used in my linked list: struct Node { void *data; struct Node *next; }; Here is my insert function: //ComparisonFunction and FILE not...
Help in JAVA please Write a program that reads a list of students (first names only) from a file. It is possible for the names to be in unsorted order in the file but they have to be placed in sorted order within the linked list. The program should use a doubly linked list. Each node in the doubly linked list should have the student’s name, a pointer to the next student, and a pointer to the previous student. Here...
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 have a Java Data Structures project and we're creating a linked list and an iterator for that linked list. The add method for the linked list has to be O(1). This is my code public class MyLinkedList extends CS20bLinkedList implements Iterable<Integer> { @Override public void add(int value){ Node tail = head; if(head == null){ head = new Node(value); //System.out.println("Very Fun! "+head.value); return; }else{ ...
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 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 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 the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> 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...
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...