#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
struct ListNode
{
string name;
int roll;
ListNode * next;
};
ListNode * head = NULL;
void append (string, int);
void insertname (string, int);
void insertroll (string, int);
void deletenode (int);
void deletenode (string);
void deleteall ();
void printing ();
void sorting ();
void exitprog ();
main ()
{
int select, roll, num;
string name;
while (1)
{
cout<<"Press 1 for appending the node"<<endl;
cout<<"Press 2 for inserting the node"<<endl;
cout<<"Press 3 for deleting the node"<<endl;
cout<<"Press 4 for deleting the list"<<endl;
cout<<"Press 5 for printing the list"<<endl;
cout<<"Press 6 for sorting the list"<<endl;
cout<<"Press 7 to exit the program"<<endl;
cin>>select;
switch (select)
{
case 1:
cout<<"Please enter the name of the student: ";
cin.ignore (10000,'\n');
getline (cin, name);
cout<<"Please enter the roll number of the student: ";
cin>>roll;
append (name, roll);
break;
case 2:
cout<<"Press 1 to sort on the basis of name or 2 to sort on the basis of roll number: ";
cin>>num;
cout<<"Please enter the name of the student: ";
cin.ignore (10000,'\n');
getline (cin, name);
cout<<"Please enter the roll number of the student: ";
cin>>roll;
if (num == 1)
{
insertname (name, roll);
}
else if (num == 2)
{
insertroll (name, roll);
}
break;
case 3:
cout<<"Press 1 to delete the node on the basis of name or 2 to delete on the basis of roll number: ";
cin>>num;
if (num == 1)
{
string value;
cout<<"Please enter the name: ";
cin.ignore (10000,'\n');
getline (cin, value);
deletenode (value);
}
else if (num == 2)
{
int value;
cout<<"Please enter the roll number: ";
cin>>value;
deletenode (value);
}
else
{
cout<<"You have entered an invalid input...!"<<endl;
}
break;
case 4:
deleteall ();
break;
case 5:
printing ();
break;
case 6:
sorting ();
break;
case 7:
exitprog ();
default:
cout<<"You have entered an invalid input...!"<<endl;
break;
}
}
}
void append (string name, int roll)
{
ListNode * temphead = head;
ListNode * temp = new ListNode;
temp->name = name;
temp->roll = roll;
temp->next = NULL;
if (head == NULL)
{
head = temp;
}
else
{
while (temphead->next)
{
temphead = temphead->next;
}
temphead->next = temp;
}
}
void insertname (string name, int roll)
{
ListNode * temp = new ListNode;
temp->next = NULL;
temp->name = name;
temp->roll = roll;
if (head == NULL)
{
head = temp;
}
else
{
ListNode * temphead = head;
ListNode * previous = NULL;
while (temphead != NULL && name > temphead->name)
{
previous = temphead;
temphead = temphead->next;
}
if (previous == NULL)
{
head = temp;
temp->next = temphead;
}
else if (temphead == NULL)
{
previous->next = temp;
}
else
{
previous->next = temp;
temp->next = temphead;
}
}
}
void insertroll (string name, int roll)
{
ListNode * temp = new ListNode;
temp->next = NULL;
temp->name = name;
temp->roll = roll;
if (head == NULL)
{
head = temp;
}
else
{
ListNode * temphead = head;
ListNode * previous = NULL;
while (temphead != NULL && roll > temphead->roll)
{
previous = temphead;
temphead = temphead->next;
}
if (previous == NULL)
{
head = temp;
temp->next = temphead;
}
else if (temphead == NULL)
{
previous->next = temp;
}
else
{
previous->next = temp;
temp->next = temphead;
}
}
}
void deletenode (int roll)
{
ListNode * temphead = head;
ListNode * previous = NULL;
ListNode * tempnext = NULL;
while (temphead->roll != roll && temphead != NULL)
{
previous = temphead;
temphead = temphead->next;
}
if (temphead == NULL)
{
cout<<"Sorry no data found!!!"<<endl;
}
else if (previous == NULL)
{
tempnext = temphead->next;
head = tempnext;
delete temphead;
}
else
{
tempnext = temphead->next;
previous->next = tempnext;
delete temphead;
}
}
void deletenode (string name)
{
ListNode * temphead = head;
ListNode * previous = NULL;
ListNode * tempnext = NULL;
while (temphead->name != name && temphead != NULL)
{
previous = temphead;
temphead = temphead->next;
}
if (temphead == NULL)
{
cout<<"Sorry no data found!!!"<<endl;
}
else if (previous == NULL)
{
tempnext = temphead->next;
head = tempnext;
delete temphead;
}
else
{
tempnext = temphead->next;
previous->next = tempnext;
delete temphead;
}
}
void deleteall()
{
ListNode * temphead = head, * temp;
while (temphead)
{
temp = temphead->next;
delete temphead;
temphead = temp;
}
head = NULL;
}
void printing ()
{
if (head == NULL)
{
cout<<"There is nothing to print."<<endl;
return;
}
ListNode * temphead = head;
while (temphead)
{
cout<<temphead->name<<' '<<temphead->roll<<endl;
temphead = temphead->next;
}
}
void sorting ()
{
ListNode * temphead = head;
//ListNode * tempnode = NULL;
int temproll;
string tempname;
int counter = 0;
while (temphead)
{
temphead = temphead->next;
counter++;
}
temphead = head;
for (int j=0; j<counter; j++)
{
while (temphead->next) //iterate through list until next is null
{
if (temphead->roll > temphead->next->roll)
{
/*tempnode = temphead;
temphead = temphead->next;
temphead->next = tempnode;*/
temproll = temphead->roll;
temphead->roll = temphead->next->roll;
temphead->next->roll = temproll;
tempname = temphead->name;
temphead->name = temphead->next->name;
temphead->next->name = tempname;
temphead = temphead->next;//increment node
}
else
temphead = temphead->next;//increment node
}
temphead = head;//reset temphead
}
}
void exitprog ()
{
exit (0);
}
Can Someone disect this program?
Page 1 of 11 Replies - 1177 Views - Last Post: 18 March 2013 - 01:57 PM
#1
Can Someone disect this program?
Posted 18 March 2013 - 12:42 PM
I have an assignment involving linked lists due monday, 24th. While surfing the web, I came across this linked list code that works flawlessly but would like to understand how it works. Could one of your programmers read the code and send me back a reply explaining what each part does? I think it will help me understand linked lists more. I know what each function does but I don't get how each function does its job. Can someone out there help me? You can ignore the exit function, just need help with everything else. Thank you.
Replies To: Can Someone disect this program?
#2
Re: Can Someone disect this program?
Posted 18 March 2013 - 01:57 PM
So you're copying code to turn in and you want us to explain it? Sorry, but you're going to have to do some work there. We have a bunch of Linked List tutorials on DIC, and there are many more on the web.
Topic closed.
Topic closed.
Page 1 of 1
|
|

New Topic/Question
This topic is locked



MultiQuote






|