Linked list sorting
Mar 10, 2014 at 9:08pm UTC
Create an organized linked list in ascending order i.e., all the entries should be added in the list in ascending order.
I have problem in insert function i don't know what i am doing wrong this is my code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
struct queue
{
int data;
queue * next;
}* front=NULL;
queue*rear=NULL;
void insert(int val)//as a PUSH function
{
queue * newNode = new queue;
queue * temp;
queue *temp1;
temp=front;
temp1=front;
newNode->data = val;
newNode->next = NULL;
if (rear == NULL){
front = rear = newNode;
}
else {
while (temp!=NULL)
{
if (newNode->data>=temp->data){
if (temp->next!=NULL)
{
temp->next=newNode;
newNode->next=temp->next->next;
return ;
}
if (temp->next==NULL)
{
temp->next=newNode;
newNode->next=NULL;
rear=newNode;
return ;
}
}
//temp->next=newNode;
//newNode->next=temp->next->next;
////rear=newNode;
//return;
}
if (newNode->data<=temp->data){
cout<<"SHIT\n" ;
cout<<"temp data :" <<temp->data;
cout<<"NEW NODE:" <<newNode->data<<endl;
cout<<"Temp->data->next :" <<temp->next->data<<endl;
temp =newNode;
newNode->next=temp1;
newNode=temp;
front = temp;
//temp=newNode->next;
return ;
}
}
}
void del()//as a POP function
{
queue * temp = front;
if (front == NULL){
cout<<"Queue Empty" ;
}
else if (front == rear){
cout<<"Item deleted " ;
front = rear = NULL;
delete temp;
}
else {
cout<<"Item deleted " ;
front = front->next;
delete temp;
}
}
void Display()
{
queue * head;
head=front;
while (head!= NULL)
{
cout<< head->data;
head = head->next;
}
}
void main()
{
insert(5);
insert(6);
insert(7);
//insert(1);
//insert(2);
//insert(3);
Display();
getch();
}
Mar 11, 2014 at 9:16pm UTC
Sharan123 wrote:I have problem in insert function i don't know what i am doing wrong this is my code
If you don't know what you are doing wrong and you wrote the code, who knows what you are doing wrong?
What problems are you experiencing with the code?
Mar 11, 2014 at 9:30pm UTC
If you don't know what you are doing wrong and you wrote the code, who knows what you are doing wrong?
This must be from a fortune cookie. Great! I love it :) Smac89+
Topic archived. No new replies allowed.