Some Code Analyze
Jul 2, 2012 at 10:28pm UTC
Hello,
as the forums says, I'm a beginner at C++ coding.
I Have two questions.
Please tell me what this code does. thanks. I need to describe the code to someone.
1 2 3 4 5 6 7
list(int shomareParvaz1,string mabda1,string maghsad1,int gheimat1,list *next1=NULL){
mabda=mabda1;
maghsad=maghsad1;
shomareParvaz=shomareParvaz1;
gheimat=gheimat1;
next=next1;
}
And my second question is, I have a warning with this code!
1 2 3 4 5 6 7 8
if (next != NULL){
prive -> next = next->next; //This line
delete next;
system("cls" );
cout << " \n \t Succesfully Removed \n" <<endl;
system("Pause" );
system("cls" );
}
Thanks in advanced!
And this is my whole source code, just in case.
By the way, is there any other way to write this easier and better?
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
#include <iostream>
#include <string>
using namespace std;
struct list{
string mabda;
string maghsad;
int shomareParvaz;
int gheimat;
list(int shomareParvaz1,string mabda1,string maghsad1,int gheimat1,list *next1=NULL){
mabda=mabda1;
maghsad=maghsad1;
shomareParvaz=shomareParvaz1;
gheimat=gheimat1;
next=next1;
}
list *next;
list *prev;
list *temp;
list *tempezafi;
}; list *head;
class flight {
public :
flight(); // constructor
~flight();// destructor
void Add_List(int shomareParvaz, string mabda, string maghsad , int gheimat);
void Delete1(int shomareParvaz);
private :
};
flight::flight(){
head=NULL;
}
flight::~flight(){
list *temp=head;
while ( temp != NULL ){
list *tempezafi=temp;
temp=temp->next;
delete tempezafi;
}
}
void flight::Delete1(int shomareParvaz){
list *prive;
if (head==NULL){
cout<<"List Is Empty" <<endl;
return ;}
if (head->shomareParvaz==shomareParvaz){
list *next=head;
head=head->next;
delete next;
system("cls" );
cout <<"\n \t Succesfully Removed \n" <<endl;
system("Pause" );
system("cls" );}
else {
list *next = head;
while (next != NULL && next->shomareParvaz != shomareParvaz){
list *prev = next;
next=next->next;
}
if (next != NULL){
prive -> next = next->next;
delete next;
system("cls" );
cout << " \n \t Succesfully Removed \n" <<endl;
system("Pause" );
system("cls" );
}
}
}
Last edited on Jul 3, 2012 at 5:38am UTC
Jul 3, 2012 at 2:48am UTC
Please tell me what this code does.
I have a feeling that someone is stealing a homework project.
Jul 3, 2012 at 10:45am UTC
I'm working on Linked List, and this is just a practice I got from a friend of mine.
Someone please answer, I'm short in time. thanks
Jul 4, 2012 at 11:55am UTC
You can think of
1 2 3 4 5 6 7 8 9 10 11 12
list(int shomareParvaz1,
string mabda1,
string maghsad1,
int gheimat1,
list *next1=NULL)
{
mabda = mabda1;
maghsad = maghsad1;
shomareParvaz = shomareParvaz1;
gheimat = gheimat1;
next = next1;
}
as a constructor where you pass in data to populate the structure. That's all this function does.
1 2 3 4 5 6 7 8 9
if (next != NULL)
{
prive->next = next->next; //This line
delete next;
system("cls" );
cout << " \n \t Succesfully Removed \n" << endl;
system("Pause" );
system("cls" );
}
probably that's where you get the warning. If you have the full warning, it will be useful.
Jul 4, 2012 at 6:49pm UTC
Thank you very much ThangDo.
here is the warning:
" warning C4700: uninitialized local variable 'prive' used"
We didn't used private. is there a problem?
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
#include <iostream>
#include <string>
using namespace std;
struct list{
string mabda;
string maghsad;
int shomareParvaz;
int gheimat;
list(int shomareParvaz1,string mabda1,string maghsad1,int gheimat1,list *next1=NULL){
mabda=mabda1;
maghsad=maghsad1;
shomareParvaz=shomareParvaz1;
gheimat=gheimat1;
next=next1;
}
list *next;
list *prev;
list *temp;
list *tempezafi;
}; list *head;
class flight {
public :
flight(); // constructor
~flight();// destructor
void Delete1(int shomareParvaz);
private :
};
Last edited on Jul 4, 2012 at 6:56pm UTC
Jul 4, 2012 at 6:59pm UTC
We didn't used private prive. is there a problem?
Yes you did. In the following code, the lines to care about are the first and the last.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
list *prive;
if (head==NULL){
cout<<"List Is Empty" <<endl;
return ;}
if (head->shomareParvaz==shomareParvaz){
list *next=head;
head=head->next;
delete next;
system("cls" );
cout <<"\n \t Succesfully Removed \n" <<endl;
system("Pause" );
system("cls" );}
else {
list *next = head;
while (next != NULL && next->shomareParvaz != shomareParvaz){
list *prev = next;
next=next->next;
}
if (next != NULL){
prive -> next = next->next;
In the first line, you create a pointer, and you give it no value. So it will have some random value. It will be poitning into some random piece of memory somewhere.
In the last line, you write a value (
next->next
) into wherever
prive
is pointing! It's just some random memory somewhere. You could be writing over the top of data, or you could cause a segFault. This is very bad.
Last edited on Jul 4, 2012 at 7:04pm UTC
Jul 5, 2012 at 2:19pm UTC
Thanks a lot dear Moschops.
=====================
Is there any other thing necessary? I made every functions public, and private is empty. should I put anything in Private?
Topic archived. No new replies allowed.