hi evey one my name is ahmmad and I,m learning c++ but there thing i don,t undarstan
so can you help me in this !???
I leaing the struc in c++ and list but whene i see a list example i have proplm

whene i use thr strut and lis in sem class I will show you the coode i hope you andstan me :S


ok....

#include<iostream.h>
#include<conio.h>
struct list{int d;list*next;}; // this is pointer next to the name of struct nexe ok...
void main(void);
list *head,*node,*tile,*k;   //all this for lis... the tail use in the and of node and the head  use  beginning of the node .. 
int i=0,x ; 
node=new list;cin>>node->d;node->next=NULL;   // this to do new lis  and make the end of first list to be nothing >> NULL
tile=head=node;   /// here why tail=head=node ?
while(i<3){   
node=new list; //
cin>>node->d;
node->next=NULL;
tile->next=node;
tile=node;i++;
}
node=head;i=1;
while(node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;} 
cout<<"\n ENTER VAL\n";cin>>x;
node=head;
while(node!=NULL)
{if(node->d==x){k=new list;cin>>k->d;k->next=node->next;node->next=k;break;}
node=node->next;}
node=head;i=1;
while(node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;}
getch();
return 0 ; 

}

while(node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;} Please explain this node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;
cout<<"\n ENTER VAL\n";cin>>x;
node=head; why Please explain
while(node!=NULL)
{if(node->d==x){k=new list;cin>>k->d;k->next=node->next;node->next=k;break;} Please explain k=new list;cin>>k->d;k->next=node->next;node->next=k;brea
node=node->next;} Please explain node=node->next;} ????
node=head;i=1; and ??
while(node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;} Please explain
getch();
return 0 ;

}

Before I look at this at all, you need some formatting lessons. Please, please take a look at http://www.gidnetwork.com/b-38.html. At least you got it into code tags, but no one can read what you have above, with everything jammed onto one line. Please repost it with proper formatting.

main should return an int, but yours doesn't even exist, you just end it with a semicolon. Pick a thread that has a main and functions and note what the layout looks like.

Edited 5 Years Ago by jonsca: n/a

thanfs for your help it is kind from you ,, i will take a look , wished me good luck

You're welcome, thanks for being willing to take a look at that stuff. Without the fundamentals you'll be lost in whatever you have to code.

while(node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;}

Explanation:

This block prints all the elements from start to end.

condition: node=NULL, means the end of the list.

i is just used as an index.

node->next has the address of the next item in the list:
node=node->next : node is a pointer of type list, value of node is now incremented to next location. This continues until the value of node become NULL that is the end of the list

node=head;

Exp:

head: head is the first element of the list.

here initialization of 'node' with the first element of list takes place.

{if(node->d==x){k=new list;cin>>k->d;k->next=node->next;node->next=k;break;}

Exp:
Here insertion of new element to the list takes place.
If the element of current node is equal to x,
k=new list, here a new node of list is created
Element of node 'k' is read from console: cin>>k->d;
k->next=node->next;
node->next=k;
These statements inserting the newly created node in current position.

node=head;i=1;
while(node!=NULL){cout<<endl<<i<<" "<<node->d;i++;node=node->next;}

Exp:
This is already explained. This block prints all the elements in the list.

Wish you all the best

This article has been dead for over six months. Start a new discussion instead.