8
#include<iostream.h> #include<conio.h> #include<process.h> void pass(); void manue(); struct Node { int marks,rollno; Node*next; }; Node*temp,*start,*current; void InputLinkList()//Input a Value in Link List { temp=new Node; start=new Node; cout<<"Enter a Roll Number "; cin>>start->rollno; cout<<"Enter a marks (-1 for end) "; cin>>start->marks; current=start; temp=current; while(current->marks!=-1) { current->next=new Node; current=current->next; temp=current; cout<<"Enter a Roll Number "; cin>>current->rollno; cout<<"Enter a marks (-1 for end) "; cin>>current->marks; } } void OutPutLinkList(Node*current1)//Link List Output { current1=start; cout<<"Roll No\t\tMarks"<<endl; if(current1==temp) cout<<"\nLink List is empty \n";

Link List using C++

Embed Size (px)

Citation preview

Page 1: Link List using C++

#include<iostream.h>#include<conio.h>#include<process.h>void pass();void manue();struct Node{

int marks,rollno;Node*next;

};Node*temp,*start,*current;void InputLinkList()//Input a Value in Link List{

temp=new Node;start=new Node;cout<<"Enter a Roll Number ";cin>>start->rollno;cout<<"Enter a marks (-1 for end) ";cin>>start->marks;current=start;temp=current;while(current->marks!=-1){ current->next=new Node; current=current->next; temp=current; cout<<"Enter a Roll Number "; cin>>current->rollno; cout<<"Enter a marks (-1 for end) "; cin>>current->marks;}

}void OutPutLinkList(Node*current1)//Link List Output{

current1=start;cout<<"Roll No\t\tMarks"<<endl;if(current1==temp)cout<<"\nLink List is empty \n";while(current1!=temp){ cout<<current1->rollno<<"\t\t"<<current1->marks<<endl; current1=current1->next;}

}void search(Node*current1,int item)//search the marks in link list

Page 2: Link List using C++

{while(current1!=temp){ if(item==current1->marks) { cout<<"Item is found"<<endl; break; } current1=current1->next;}if(item!=current1->marks)cout<<"not found "<<endl;

}int FindMaximum(int max,Node*current1)//find maximum value in link list{

while(current1!=temp){ if(current1->marks>max) max=current1->marks; current1=current1->next;}return max;

}void sortlintlist(Node*head1,Node*current2) //this finction sort a link list{

cout<<"\nThe Sorted Link List is: \n";Node*i,*j;Node*temp1;//create a nodetemp1=new Node;//allocate memory for temp1 nodeif(head1==temp)cout<<" empty \n";for(i=head1;i!=temp;i=i->next){ for(j=i->next;j!=temp;j=j->next) { if(i->marks>j->marks) {

temp1->marks=i->marks; i->marks=j->marks; j->marks=temp1->marks;

} }}for(i=head1;i!=temp;i=i->next){ for(j=i->next;j!=temp;j=j->next) {

Page 3: Link List using C++

if(i->rollno>j->rollno) {

temp1->rollno=i->rollno; i->rollno=j->rollno; j->rollno=temp1->rollno;

} }}

while(current2!=temp){ cout<<current2->rollno<<"\t\t"<<current2->marks<<endl; current2=current2->next;}

}void insertfirst(Node*head,int item,int rno) //Insert first in link list{

Node*ptr;//create a temporary nodeptr=new Node;//allocate space for nodeptr->rollno=rno;ptr->marks=item;// store data(first field)ptr->next=head;// store the address of the pointer head(second field)head=ptr;// transfer the address of 'ptr' to 'head'start=head;

}int SizeOfLinkList(Node*current1)//LinkList Lenth Function{ int count=0;

current1=start;while(current1!=temp){ count++; current1=current1->next;}return count;

}

main(){

clrscr();char choice;int select;char c;cout<<"Press Y or y to input data in link list ";cin>>c;if(c=='y'||c=='Y'){ InputLinkList();

Page 4: Link List using C++

pass(); do { clrscr(); manue(); cin>>select; int item; switch(select) { case 1:

OutPutLinkList(start);break; case 2:

cout<<"\nEnter a number which you want to search in link list "; cin>>item; search(start,item);break;

case 3: cout<<"\nMaximum marks is="<<FindMaximum(0,start); cout<<"\nMaximum marks 0 indicate link list is empty ";break;

case 4: sortlintlist(start,start);break;

case 5: int rno; cout<<"Enter a Roll Number "; cin>>rno; cout<<"\nEnter a marks you want to insert first in link list "; cin>>item; insertfirst(start,item,rno);

case 6: cout<<"\nThe size of Link List is "<<SizeOfLinkList(start);break;

case 7: exit(0);

} cout<<"\nWould you like to continue press y or Y "; choice=getch(); } while(choice=='y'||choice=='Y');}

}

Page 5: Link List using C++

void manue(){

cout<<"\t\t||===================================||"<<endl;cout<<"\t\t|| LINK LIST MAIN MANUE ||"<<endl;cout<<"\t\t||-----------------------------------------------------------||"<<endl;cout<<"\t\t|| ||"<<endl;cout<<"\t\t|| 1 See Link List OutPut ||"<<endl;cout<<"\t\t|| 2 Search element in Link List ||"<<endl;cout<<"\t\t|| 3 Find Maximum in Link List ||"<<endl;cout<<"\t\t|| 4 Sort Link List Elements ||"<<endl;cout<<"\t\t|| 5 Insetr first to Link List ||"<<endl;cout<<"\t\t|| 6 See size of Link List ||"<<endl;cout<<"\t\t|| 7 Exit ||"<<endl;cout<<"\t\t||===================================||"<<endl;

}void pass(){

char waste;char A[12]={"pakistanius"};char b[12];int w=0;int c=0;int i=0;for(w=0;w<3;w++){ cout<<"\nEnter a Password To See Link List OutPut Manue "; for(int j=0;j<11;j++) cin>>b[j]; for(i=0;i<11;i++) { if(A[i]!=b[i]) {

cout<<"\nWorng Password ";c=25;break;

} else c=11; } if(w==1&&c==25) cout<<"\nLast try ";

Page 6: Link List using C++

if(c==i) { cout<<"\t\t||========================================||"<<endl; cout<<"\t\t|| Welcome To Link List Program ||"<<endl; cout<<"\t\t|| This Program cantaning a following manue ||"<<endl; cout<<"\t\t|| Press any Key to see Link List Manue ||"<<endl; cout<<"\t\t||========================================||"<<endl; getch(); clrscr(); break; }}if(w==3&&c==25){

cout<<"\nProgram close due to security threat ";cout<<"\nEnter any character ";cin>>waste;exit(0);}

}