project.exe has triggered a breakpoint - c++

I had a circular link list which has add, delete, printlist and search functions etc. All the functions works correctly but when I delete all nodes in the list and than try to add new nodes to the same list, I get project.exe has triggered a breakpoint message.
List.h
#ifndef LIST_H
#define LIST_H
using namespace std;
class list {
public:
struct node {
string data, assignee, date;
node *next;
};
typedef struct node NodePtr;
NodePtr *head, *current, *temp, *dummy;
list();
void add();
void del();
void printList();
void menu();
void exit();
void search();
};
#endif
Source.cpp
#include <cstdlib>
#include <iostream>
#include "List.h"
#include<string>
using namespace std;
list::list() { //constructor
head = NULL;
current = NULL;
temp = NULL;
}
void list::add(){
string addTask, addAssignee, addDate;
NodePtr *newNode = new NodePtr;
//entering the data of the tasks
cout << "Enter the name of the task:\n"<<"<<";
getline(cin, addTask); // compailer doesnt detect the first getline function, ı dont understan why. ı had to put it twice
getline(cin, addTask);
cout << "Enter the assignee name for the task:\n" << "<<";
getline(cin, addAssignee);
cout << "Enter the deadline for the task:\n" << "<<";
getline(cin, addDate);
cout << "\n";
//assignt data to the new node
newNode->data = addTask;
newNode->assignee = addAssignee;
newNode->date = addDate;
newNode->next = NULL;
if (head != NULL){
current = head;
while (current->next != head){ //finding the last node
current = current->next;
}
current->next = newNode;
dummy = newNode;
dummy->next = head; // we make the list, circular
}
else //here will work if the list is empty
{
head = newNode;
dummy = newNode;
head->next=head;
}
}
void list::printList(){
int i = 1;
current = head;
if (head == NULL){
cout << "List is empty\n\n";
}
else
{
do
{
cout << i << "-";
cout << current->data << endl;
cout << current->assignee << endl;
cout << current->date << endl << endl;
current = current->next;
i++;
} while (current != dummy->next);
}
}
void list::del(){ //tüm liste silindikten sonra tekrar işlem(ekleme, listeleme etc.) yapılamıyor hata veriyor
NodePtr *delPtr;
delPtr = NULL;
int ID;
cout << "These tasks exist in the list:\n";
printList();
cout << "Choose the ID of the task you want to remove:\n";
cin >> ID;
if (head == NULL){
cout << "List does not includes any task.\n";
}
else
if (ID == 1){
delPtr = head;
head = head->next;
delete delPtr;
dummy->next = head;
}
else
{
current = head;
for (int k = 1; k <= ID - 1; k++){
temp = current;
current = current->next;
}
if (current == dummy){
dummy = temp;
dummy->next = head;
}
delPtr = current;
current = current->next;
temp->next = current;
delete delPtr;
}
}
void list::menu(){
cout << "Choose an operation\n"
<< "A: Add Task\n"
<< "S: Search for Task\n"
<< "L: List All Tasks\n"
<< "R: Remove Task\n"
<< "E: Exit\n\n"<<"<<";
}
void list::search(){
string searchTask;
NodePtr *search;
search = head;
cout << "To search for a task, enter its task name or assignee name:\n" << "<<";
getline(cin, searchTask);
getline(cin, searchTask);
while (search != dummy){//bu satır problemli gibi gözüküyor
if (search->data == searchTask || search->assignee == searchTask){
cout << search->data << endl
<< search->assignee << endl
<< search->date << endl<<endl;
}
search = search->next;
}
}
void list::exit(){
node *delPtr;
current = head;
if (head != NULL){
do{
temp = current;
delPtr = temp;
current = current->next;
delete delPtr;
} while (current != dummy);
}
}
Main.cpp
#include <cstdlib>
#include <iostream>
#include "List.h"
#include<string>
using namespace std;
int main(){
char operation;
list tasks;
tasks.menu();
cin >> operation;
while (operation!='E' && operation!='e'){
switch (operation)
{
case 'A':case'a':
tasks.add();
break;
case 'S': case's':
tasks.search();
break;
case 'L': case 'l':
cout << "All tasks are listed below:\n";
tasks.printList();
break;
case 'R': case 'r':
tasks.del();
break;
default:
break;
}
tasks.menu();
cin >> operation;
}
tasks.exit();
return 0;
}

Related

I am designing a Linked List where I can Insert and delete item from the list. But there is an issue inserting item at the end of linked list

I am designing a Linked List where I can Insert and delete item from the list. But there is an issue inserting item at the end of linked list.
I thought my logic was fine for inserting an item at the end of linked list. But I am getting this (segmentation fault : core dumped) error when I am trying to insert item that should be inserted at the end of Linked List. Can Anyone help me with this issue
#include <iostream>
using namespace std;
struct list
{
int data;
list *next,*back;
};
typedef struct list node;
node *start = NULL;
void display()
{
node *temp;
temp = start;
while (temp!=NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
void insertion(int item)
{
node *temp,*temp2;
if(start == NULL)
{
start = new node();
start->data = item;
start->next = NULL;
}
else if(item <= start->data)
{
temp = new node();
temp->data = item;
temp->next = start;
start = temp;
}
else // this the logic for inserting middle and end of linked list
{
temp = start;
while(item >= temp->next->data && temp->next != NULL)
{
temp = temp->next;
}
temp2 = new node();
temp2->data = item;
temp2->next = temp->next;
temp->next = temp2;
}
}
void deletion()
{
}
void showMenu()
{
cout << "0.Exit\n1.ShowMenu\n2.Insert\n3.Delete\n4.Display" << endl;
}
int main()
{
showMenu();
int choice;
cout <<"Enter your Choice " << endl;
cin >> choice;
while(choice != 0)
{
if(choice == 1)
{
showMenu();
}
else if(choice == 2)
{
int item;
cout << "Enter your item " << endl;
cin >> item;
insertion(item);
}
else if(choice == 3)
{
deletion();
}
else if(choice == 4)
{
display();
}
else
{
choice = 0;
}
cout <<"Enter your Choice " << endl;
cin >> choice;
}
return 0;
}

LinkedList Remove function fails when deleting from the end of the list C++

I have a LinkedList class and everything works except for my remove function. It works except for when it is removing from the end. If I remove from the end and then try to display the new list the program crashes and doesnt give me an error. I was wondering if someone could help me pinpoint what I did wrong in the remove function where I am removing the tail.
Main method
#include "LinkedList.h"
#include <cstdlib>
#include <iomanip>
using namespace std;
void MainMenu(int& menu);
int main(int argc, char** argv) {
int menu = 0;
string name;
LinkedList list;
while (true)
{
MainMenu(menu);
if (menu == 1)
{
cout << "Please enter the name you want to add to the front: " << endl;
cin >> name;
list.addFront(name);
}
else if (menu == 2)
{
cout << "Please enter the name you want to add to the back: " << endl;
cin >> name;
list.addBack(name);
}
else if (menu == 3)
{
cout << "Please enter the name you want to remove: " << endl;
cin >> name;
list.remove(name);
}
else if (menu == 4)
{
list.display();
}
else if (menu == 5)
{
cout << "\nThank you for using my program!" << endl;
exit(0);
}
}
return 0;
}
void MainMenu(int& menu)
{
cout << '\n' << endl;
cout << left << setw(30) << right << setw(20) << "Main Menu" << endl;
cout << setw(36) << setfill('*') << "\n" << endl;
cout << "1. Add node to front." << endl;
cout << "2. Add node to back." << endl;
cout << "3. Remove node." << endl;
cout << "4. Display list." << endl;
cout << "5. Exit" << endl;
cout << setw(36) << "\n" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin >> menu;
while (menu != 1 && menu != 2 && menu != 3 && menu != 4 && menu != 5)
{
cout << "Menu options are 1 - 5" << endl;
cout << "Please enter a menu number to proceed: " << endl;
cin.clear();
cin.ignore(100, '\n');
cin >> menu;
}
cout << setfill(' ') << "\n" << endl;
}
Header file
#include <iostream>
using namespace std;
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
struct Node
{
string name;
Node * next;
};
class LinkedList {
public:
Node * head = nullptr;
Node * tail = nullptr;
Node * current = nullptr;
void addFront(string name);
void addBack(string name);
void remove(string name);
void display();
LinkedList();
LinkedList(const LinkedList& orig);
virtual ~LinkedList();
private:
};
#endif /* LINKEDLIST_H */
Source File
#include "LinkedList.h"
void LinkedList::addFront(string name)
{
Node * node = new Node();
node->name = name;
if (head != nullptr)
{
node->next = head;
head = node;
}
else
{
head = node;
tail = node;
}
}
void LinkedList::addBack(string name)
{
Node * node = new Node();
node->name = name;
node->next = nullptr;
if (head == nullptr)
{
head = node;
tail = node;
}
else
{
node->next = nullptr;
tail->next = node;
tail = node;
}
}
void LinkedList::remove(string name)
{
Node * temp = head;
while (temp != nullptr)
{
if (temp->name == name)
{
cout << "Name found! Being removed." << endl;
break;
}
temp = temp->next;
if (temp == nullptr)
{
cout << "Name not found!" << endl;
return;
}
}
temp = head;
if(head == nullptr)
{
cout << "The list is empty." << endl;
return;
}
while(!(temp->name == name))
{
temp = temp->next;
}
if (temp == tail && temp == head)
{
head = nullptr;
tail = nullptr;
delete temp;
}
else if (temp == tail)
{
temp->next = tail;
delete temp->next;
temp->next = nullptr;
tail = temp;
}
else if(temp == head)
{
temp = head;
head = temp->next;
delete temp;
}
else
{
temp = head;
while(!(temp->next->name == name))
{
temp = temp->next;
}
Node * next = temp->next->next;
delete temp->next;
temp->next = next;
}
}
void LinkedList::display()
{
current = head;
if (current == nullptr)
{
cout << "The list is empty!" << endl;
}
while (current != nullptr)
{
cout << current->name << endl;
current = current->next;
}
}
LinkedList::LinkedList() {
}
LinkedList::LinkedList(const LinkedList& orig) {
}
LinkedList::~LinkedList() {
}
Your main problem is this section here
else if (temp == tail)
{
temp->next = tail;
delete temp->next;
temp->next = nullptr;
tail = temp;
}
That is what executes if the match is the last item. But if it is the last item then temp->next will be tail because you just set it to tail, and since temp == tail that also invalidates temp. Setting temp->next will be using a deleted pointer. There's no fixing this at this point because you need the previous node to set tail to, and you didn't save it, so you'd have to loop through again to get it.
Now, for the solution, you should do a single loop. No need to loop multiple times (3 in most cases)
void LinkedList::remove(string name)
{
Node * temp = head;
Node * prev = nullptr;
while (temp != nullptr)
{
if (temp->name == name)
{
cout << "Name found! Being removed." << endl;
if (prev != nullptr) {
prev->next = temp->next
}
else {
head = temp->next;
}
if (tail == temp) {
tail = prev;
}
delete temp;
return;
}
prev = temp;
temp = temp->next;
}
cout << "Name not found!" << endl;
}
For starters everywhere in member function declarations where a parameter is declared like
string name
you should change it to
const string &name
Secondly the member function remove shall not issue any message. It is the caller of the function that decides whether to output a message.
Your function is inefficient because it traverses the list two or even three times in loops and has too many conditions that makes the function too complicated. As a result the function has bugs.
For example in this if statement
else if (temp == tail)
{
temp->next = tail;
delete temp->next;
temp->next = nullptr;
tail = temp;
}
there is used deleted pointer temp to access memory because temp is equal to tail (according to the condition of the if statement)
temp->next = nullptr;
and tail again is set to the deleted pointer
tail = temp;
The function can be declared and defined the following way
class LinkedList {
public:
//...
bool remove( const std::string &name );
};
bool LinkedList::remove( const std::string &name )
{
Node *prev = nullptr;
Node *current = head;
while ( current && current->name != name )
{
prev = current;
current = current->next;
}
bool success = current != nullptr;
if ( success )
{
if ( prev )
{
prev->next = current->next;
if ( prev->next == nullptr ) tail = prev;
}
else
{
head = head->next;
if ( head == nullptr ) tail = nullptr;
}
delete current;
}
return success;
}

Can you check why removing an Item from a linked list is not working in this code? [duplicate]

This question already has an answer here:
How do I properly delete nodes of linked list in C++
(1 answer)
Closed 2 years ago.
I've been asked by a friend to help him with an exercise, basically the idea is like below.
Car number = fr50000 Car owner = AlexNelson Parking time = 3.5 hours.
He was told not to use stuff like string or getline, so it's simple app just to learn the idea of working with linked lists.
So I made this program. When I'm calling the remove() function the first time, it says the list is empty at the beginning (as it should do). But the second and third time, it's saying that the car is removed, but when I call the display() function, the car is still there (it didn't remove from the list)
Can you tell me what's wrong with my code?
#include <iostream>
using namespace std;
int length = 0;//variable of how many items in the list
struct node
{
char carNumber[15];
char carOwner[20];
float parkingTime;
node *link;
};
typedef struct node node;
node *head;//the begining of a list;
bool isempty()
{
if (length == 0)
return true;
else
return false;
}
void insert()
{
if (isempty())
{
head = new node;
cout << "Enter car number: ";
cin >> head->carNumber;
cout << "Enter Car owner: ";
cin >> head->carOwner;
cout << "Enter parking time: ";
cin >> head->parkingTime;
head->link = NULL;
length++;
}
else
{
node *p = head;
node *pnext = new node;
while (true)
{
if (p->link == NULL)
{
p->link = pnext;
break;
}
p = p->link;
}
cout << "Enter car number: ";
cin >> pnext->carNumber;
cout << "Enter Car owner: ";
cin >> pnext->carOwner;
cout << "Enter parking time: ";
cin >> pnext->parkingTime;
pnext->link = NULL;
length++;
}
}
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p;
p = head;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
p = p->link;
cout << "Car removed\n";
return;
}
p = p->link;
}
cout << "Car was not found, check the number\n";
}
void display()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
cout << "Car Number\t\tCar Owner\t\tParking Time\n";
node *p = head;
while (p != NULL)
{
cout << p->carNumber << "\t\t" << p->carOwner << "\t\t" << p->parkingTime << " Hours\n";
p = p->link;
}
}
int main()
{
string number;
display();
insert();
insert();
insert();
display();
remove();
display();
insert();
remove();
display();
}
Your remove() function is not actually removing (or destroying) a node from the list. You need to update the link of the previous node in the list to point to the next node in the list. And you need to update the head too, if removing the 1st node in the list.
Try this instead:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node *p = head;
node *prev = NULL;
while (p != NULL)
{
if (strcmp(p->carNumber, carnumber) == 0)
{
if (p == head)
head = p->link;
if (prev)
prev->link = p->link;
delete p;
cout << "Car removed\n";
return;
}
prev = p;
p = p->link;
}
cout << "Car was not found, check the number\n";
}
Alternatively:
void remove()
{
if (isempty())
{
cout << "List is empty\n";
return;
}
char carnumber[15];
cout << "Enter car number to remove: ";
cin >> carnumber;
node **p = &head;
while (*p != NULL)
{
if (strcmp((*p)->carNumber, carnumber) == 0)
{
node *n = *p;
*p = (*p)->link;
delete n;
cout << "Car removed\n";
return;
}
p = &(p->link);
}
cout << "Car was not found, check the number\n";
}
p = p->link; You're modifying the value of the local variable p, not the list. You need to modify the previous node's link field.
In the loop in remove, you are doing p = p->link, when p points to the node you want to remove. But you actually need to update the link field of the node that is pointing to p.
Here's a simple way of doing that:
node **p = &head;
while(*p)
if (strcmp((*p)->carNumber, carnumber) == 0)
break;
else p = &(*p)->link;
if (*p)
{
cout<<"Car removed\n";
delete std::exchange(*p, (*p)->link);
}
else cout << "Car was not found, check the number\n";
If you can't use std::exchange, then you can replace that with:
auto h = *p;
*p = (*p)->link);
delete h;

Implement a linked list program with the following functions

i am trying to implement a linked list with add, search, remove and print functions. But i keep getting errors that "search, print and file are not declared in this scope" and ISO C++ forbids comparison between pointer and integer [-fpermissive] error. Any help would be appreciated.
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
struct node
{
public:
char name;
node *next;
};
bool isEmpty(node *head);
char welcomeScreen();
void addInitialElement(node *head, node*last, char name);
void add (node *&head, node *&last, char name);
void serach (node *current);
void remove (node *&head, node *&last);
void printFile(node *current);
bool isEmpty(node *head)
{
if(head == NULL)
return true;
else
return false;
}
char welcomeScreen()
{
char options;
cout <<"Address Book \n";
cout <<"Available Commands \n";
cout <<" add <name>. \n";
cout <<" search <name>. \n";
cout <<" remove <name>. \n";
cout <<" print \n";
cout <<" file <filename>. \n";
cout <<" quit \n";
cin >> options;
return options;
}
void addInitialElement(node *head, node*last, char name)
{
node *temp = new node;
temp->name = name;
temp->next = NULL;
head = temp;
last = temp;
}
void add (node *&head, node *&last, char name)
{
if(isEmpty(head))
addInitialElement(head, last,name);
else
{
node *temp = new node;
temp->name = name;
temp->next = NULL;
last->next = temp;
last = temp;
}
}
void serach (node *current)
{
node *temp = new node;
if (current->name == temp)
{
cout << "The List Contains the Following: \n";
while(current != NULL)
{
cout<< current->name <<endl;
current = current->next;
}
}
}
void remove (node *&head, node *&last)
{
if(isEmpty(head))
cout << "ERROR: Not found for Removal. \n";
else if(head == last)
{
delete head;
head == NULL;
last == NULL;
}
else
{
node *temp = head;
head = head->next;
delete temp;
}
}
void printFile(node *current)
{
if(isEmpty(current))
cout << "NO NAME IN LIST. \n";
else
{
cout << "The List Contains the Following: \n";
while(current != NULL)
{
cout<< current->name <<endl;
current = current->next;
}
}
}
int main()
{
node *head = NULL;
node *last = NULL;
char options;
char name;
do{
options = welcomeScreen();
switch(options)
{
case '1': cout <<"please add a name: ";
cin >> name;
add(head, last, name);
break;
case '2' : search(head, last);
cin >> name;
break;
case '3' : remove(head, last);
break;
case '4' : print(head);
break;
case '5' : file(head);
break;
default: cout << "system exit \n";
}
}while(options != '6');
}
You misspelled search. Do a find / replace on "serach".
In case 4, you may mean to call printFile instead of print
case '4' : printFile(head);
In case 5, I don't know what your intention is here, but there is no function called "file". Try searching on that word, file, and you'll see.

Insertion sort in a doubly linked list

I'm making a list in C++ from scratch, not using the STL list funciton. My program is designed for the user to insert a new item (string) to the list, one at a time. The items are supposed to be sorted as they are inserted, and it partially works. The user is supposed to be able to print them forwards and backwards.
Examples:
I insert: 1, 3, 2 - result forward: 1, 2, 3 - backwards: 3, 2, 1
I insert 1, 3, 2, 4 - result forward: 1, 4, 2, 3 - backwards: 3, 2, 4, 1
I insert 4, 2, 3 - result forward: 2, 3, 4 - backwards: 4, 3, 2
I insert 4, 2, 3, 1 - result forward: 1, 4 - backwards: 1, 3 repetedly until program crashes
These are a few examples, I could include more. I can not for the life of me see what's wrong wth my code. I have gotten help from people I know who are great coders, but they were unable to see the problem, after looking at it for a couple hours. I've been stuck at this for days now, and I just don't see it.
Here is my code (comments are in Norwegian, sorry!):
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//definerer noden
struct node
{
string data; //det noden inneholder
node* next; //nestepeker
node* prev; //forrigepeker
};
//definerer funksjonene
bool isEmpty(node* first);
char menu();
void insertAsFirstElement(node* &first, node* &end, string data);
void insert(node* &first, node* &end, string data);
void remove(node* &first, node* &end, string data);
void showList(node* first);
void printBackwards(node* end);
//er noden tom?
bool isEmpty(node* first)
{
if(first == NULL)
return true;
else
return false;
}
//sender valgskjermen til konsollen
char menu()
{
char choice;
cout << "Welcome to LISTOMANIA 3000x! \n" << endl;
cout << "Press <1> to add an item" << endl;
cout << "Press <2> to remove item on top" << endl;
cout << "Press <3> to display the list forward" << endl;
cout << "Press <4> to display the list backwards" << endl;
cout << "Press <5> to exit" << endl;
cout << "\nInput: ";
cin >> choice;
return choice;
}
//hvis lista er tom, sett inn node som første element:
void insertAsFirstElement(node* &first, node* &end, string data)
{
cout << "temp is first\n";
node* temp = new node;
temp->data = data;
temp->next = NULL;
temp->prev = NULL;
first = temp;
end = temp;
}
//hvis lista ikke er tom, sett inn noden sortert:
void insert(node* &first, node* &end, string data)
{
if(isEmpty(first))
{
insertAsFirstElement(first, end, data);
}
else
{
node* temp = new node;
temp->data = data;
node* n = first;
while(n)
{
if(n->data > temp->data)
{
cout << "temp BEFORE n" << endl;
temp->prev = n->prev;
temp->next = n;
n->prev = temp;
if(temp->prev)
{
temp->prev->next = temp;
}
else
{
first = temp;
}
}
else if(n->data <= temp->data)
{
cout << "temp AFTER n" << endl;
//temp = new node;
//temp->data = data;
temp->prev = n;
temp->next = n->next;
n->next = temp;
if(temp->next)
{
temp->next->prev = temp;
}
else
{
end = temp;
}
break;
}
n = n->next;
}
}
}
//sletter valgt node
void remove(node* &first, node* &end, string data)
{
string delItem;
node* temp;
if(isEmpty(first))
cout << "\nNothing to delete, the list is empty!\n------------------------------------------------\n------------------------------------------------\n";
else if(first == end)
{
cout << "\nYou removed <" << first->data << ">!" << endl;
delete first;
first = NULL;
end = NULL;
cout <<"------------------------------------------------\n------------------------------------------------\n";
}
else
{
node* temp = first;
cout << "You removed <" << temp->data << ">!" << endl;
first = first->next;
delete temp;
cout <<"------------------------------------------------\n------------------------------------------------\n";
}
}
//skriver ut listen alfabetisk
void showList(node* first)
{
node * temp = first;
if(isEmpty(first))
{
cout << "\nThe list is empty!\n";
}
else
{
cout << "\nThe list contains: \n\n";
while(temp != NULL)
{
cout << temp->data << endl;
temp = temp->next;
}
}
cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";
}
//skriver ut listen omvendt alfabetisk
void printBackwards(node* end)
{
node * temp = end;
if(isEmpty(end))
{
cout << "\nThe list is empty!\n";
}
else
{
cout << "\nThe list contains: \n\n";
while(temp != NULL)
{
cout << temp->data << endl;
temp = temp->prev;
}
}
cout << "------------------------------------------------\n";
cout << "------------------------------------------------\n";
}
//mainfunksjon
int main()
{
node* first = NULL;
node* end = NULL;
char choice;
string data;
do
{
choice = menu();
switch(choice)
{
case '1': cout <<"\nPlease add something to the list: ";
cin >> data;
insert(first, end, data);
cout << "------------------------------------------------\n------------------------------------------------\n";
break;
case '2': remove(first, end, data);
break;
case '3': showList(first);
break;
case '4': printBackwards(end);
break;
case '5': cout << "\nSystem exit...\n";
break;
default: cout << "\nInvalid input!\n------------------------------------------------\n------------------------------------------------\n";
}
}while(choice != '5');
return 0;
}
Just ignore the lines of hyphens, they are just there to make the program look prettier when it is run.
Please note that I am a beginner! There might be lots of other mistakes too, other than my problem. Also ignore the remove function, as I haven't gotten to that problem yet...
check two lines that I added with comments //1.this line and //2.this line the fist one is called when temp<n so you have to compare temp with previous node of n, and the second one is for when temp>=n so you have to do is to compare temp with next node of n. with what I said so far it is clear that you have to remove the line that I commented with //4.you should remove this line : n = temp->next; this is what cause your last problem. Also you have another mistake, you have to remove the break; it leaves your listed in the middle of the sort so your sort never completed and that is what cause the other problem.
while(n)
{
if(n->data > temp->data)
{
cout << "temp BEFORE n" << endl;
temp->prev = n->prev;
temp->next = n;
n->prev = temp;
if(temp->prev)
{
temp->prev->next = temp;
}
else
{
first = temp;
}
n = temp->prev; //1.This line
}
else if(n->data <= temp->data)
{
cout << "temp AFTER n" << endl;
//temp = new node;
//temp->data = data;
temp->prev = n;
temp->next = n->next;
n->next = temp;
if(temp->next)
{
temp->next->prev = temp;
}
else
{
end = temp;
}
n = temp->next; //2.This line
//3.you should comment this line : break;
}
//4.you should remove this line : n = temp->next;
}
I got some help from more people, and we found a solution. Here is my insert fucntion:
//hvis lista ikke er tom, sett inn noden sortert:
void insert(node* &first, node* &end, string data)
{
if(isEmpty(first))
{
insertAsFirstElement(first, end, data);
}
else
{
node* temp = new node;
temp->data = data;
temp->next = first;
temp->prev = NULL;
first->prev = temp;
first = temp;
}
//sortering
if(first != NULL)
{
node* current = first;
node* prev = NULL;
node* tempNode = NULL;
while(current->next != NULL)
{
tempNode = current->next;
//cout << "current: " << current->data << " tempNode: " << tempNode->data << endl;
if(current->data > tempNode->data)
{
//cout << "swapped " << "current: " << current->data << " tempNode: " << tempNode->data << endl;
swap(current->data, tempNode->data);
}
else
{
prev = current;
current = current->next;
}
}
}
}
This sorts the data if it is a string, with a little bug on numbers, interpreted as a string, which can be solved with converting all numbers to an int.
EDIT:
That bug with the nubmers isn't really a bug at all. The list thinks 11 is smaller than 5, but that is because every input is interprated as a string. So then if you think that 1 = a and 2 = b, then it makes perfect sense that 11 < 5. So if you want them to be sorted how it would logically be done, having 11 > 5, you'd have to convert all numbers to an int.