Sie sind auf Seite 1von 7

Link List

============================================
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
///////////////////STRUCT LINK LIST/////////////////
struct LList{
int rollno;
char name[20];
struct LList *next;
};
///////////////////CLASS LINK LIST/////////////////
class linkList{
//Public
public:
linkList();
~linkList();
//Methods
void addNode(int,char*);
void deleteNode(int);
void insertNode(int,char*,int);
void modifyNode(int,char*,int);
void displayNode(int);
void displayAll();
void displayAll2(struct LList*);
void pushNode(int,char*);
void popNode();
int countNode();
struct LList* getHead(){return head;}
//Private
private:
struct LList *head;
struct LList *runner;
struct LList *temp;
};

///////////////////CONSTRUCTOR////////////////////
linkList::linkList(){
head=NULL;
runner=NULL;
}
///////////////////DESTRUCTOR////////////////////
linkList::~linkList(){
runner=head;
do{
runner=head;
head=head->next;
free(runner);
}while(head!=NULL);
}
///////////////////ADD NODE////////////////////
void linkList::addNode(int roll,char *n){
if(head==NULL){
head=(struct LList*)malloc(sizeof(struct LList));
head->rollno=roll;
strcpy(head->name,n);
head->next=NULL;
}
else{
runner=head;
while(runner->next!=NULL)
runner=runner->next;
runner->next=(struct LList*)malloc(sizeof(struct LList));
runner=runner->next;
runner->rollno=roll;
strcpy(runner->name,n);
runner->next=NULL;

}
}
///////////////////DELETE NODE/////////////////
void linkList::deleteNode(int index){
if(index==0){
runner=head;
head=head->next;
free(runner);
}
else{
runner=head;
int i=0;
while(runner->next!=NULL && i<index-1){
runner=runner->next;
i++;
}
temp=runner->next;
runner->next=temp->next;
free(temp);
}
}
///////////////////INSERT NODE/////////////////
void linkList::insertNode(int roll,char *n,int index){
int i=0,res=0,c=0;
c=countNode();
if(index>c){
cout<<"\nINDEX IS GREATER THAN USED!!!"<<endl;
exit(0);
}
runner=head;
while(runner!=NULL){
if(i==index){
temp=(struct LList*)malloc(sizeof(struct LList));
temp->rollno=roll;
strcpy(temp->name,n);
temp->next=runner->next;
runner->next=temp;
res=1;
break;
}
runner=runner->next;
i++;
}
if(res==0)
cout<<"\nNOT FOUND!!!";
}
///////////////////MODIFY NODE/////////////////
void linkList::modifyNode(int roll,char *n,int index){
int i=0,res=0,c=0;
c=countNode();
if(index>c){
cout<<"\nINDEX IS GREATER THAN USED!!!"<<endl;
exit(0);
}
runner=head;
while(runner!=NULL){
if(i==index){
runner->rollno=roll;
strcpy(runner->name,n);
res=1;
break;
}
runner=runner->next;
i++;
}
if(res==0)
cout<<"\nNOT FOUND!!!";
}
///////////////////DISPLAY NODE/////////////////
void linkList::displayNode(int index){
int i=0,res=0,c=0;
c=countNode();
if(index>c){
cout<<"\nINDEX IS GREATER THAN USED!!!"<<endl;
exit(0);
}
runner=head;
while(runner!=NULL){
if(i==index){
cout<<runner->rollno<<endl;
cout<<runner->name<<endl;
res=1;
break;
}
runner=runner->next;
i++;
}
if(res==0)
cout<<"\nNOT FOUND!!!";
}
/////////////////DISPLAY ALL NODE///////////////
void linkList::displayAll(){
runner=head;
while(runner!=NULL){
cout<<runner->rollno<<endl;
cout<<runner->name<<endl;
runner=runner->next;
}
}
/////////////////DISPLAY ALL NODE///////////////
void linkList::displayAll2(struct LList* ptr){
if(ptr!=NULL){
cout<<ptr->rollno<<endl;
cout<<ptr->name<<endl;
displayAll2(ptr->next);
}
}
///////////////////COUNT NODE////////////////////
int linkList::countNode(){
int i=0;
if(head==NULL)
return 0;
else{
runner=head;
while(runner!=NULL){
runner=runner->next;
i++;
}
}
return i;
}
///////////////////PUSH NODE///////////////////
void linkList::pushNode(int roll,char *n){
temp=head;
head=(struct LList*)malloc(sizeof(struct LList));
head->rollno=roll;
strcpy(head->name,n);
head->next=temp;
}
///////////////////POP NODE////////////////////
void linkList::popNode(){
cout<<head->rollno<<endl;
cout<<head->name<<endl;
temp=head;
head=head->next;
free(temp);
}
///////////////////CLIENT PROGRAM/////////////////
void main(){
int ch;
int roll;
char name[20];
linkList ob;
for(;;){
cout<<"\n1-Add Node\n";
cout<<"2-Delete Node\n";
cout<<"3-Insert Node\n";
cout<<"4-Modify Node\n";
cout<<"5-Display Node\n";
cout<<"6-Display All\n";
cout<<"7-Count Node\n";
cout<<"8-Push\n";
cout<<"9-Pop\n";
cout<<"10-Display All-2\n";
cout<<"11-Exit\n\n\n";
cout<<"Enter ur choice: ";
cin>>ch;
if(ch==1){
cout<<"Enter Roll No: ";
cin>>roll;
cout<<"Enter Name: ";
cout.flush();
gets(name);
ob.addNode(roll,name);
}
else if(ch==2){
int n;
cout<<"\nEnter node to delete: ";
cin>>n;
ob.deleteNode(n);
}
else if(ch==3){
int n;
cout<<"\nEnter node to insert: ";
cin>>n;
cout<<"Enter Roll No: ";
cin>>roll;
cout<<"Enter Name: ";
cout.flush();
gets(name);
ob.insertNode(roll,name,n);
}
else if(ch==4){
int n;
cout<<"\nEnter node to modify: ";
cin>>n;
cout<<"Enter Roll No: ";
cin>>roll;
cout<<"Enter Name: ";
cout.flush();
gets(name);
ob.modifyNode(roll,name,n);
}
else if(ch==5){
int n;
cout<<"\nEnter node to display: ";
cin>>n;
ob.displayNode(n);
}
else if(ch==6)
ob.displayAll();
else if(ch==7)
cout<<"\nCount: "<<ob.countNode()<<endl<<endl;
else if(ch==8){
cout<<"Enter Roll No: ";
cin>>roll;
cout<<"Enter Name: ";
cout.flush();
gets(name);
ob.pushNode(roll,name);
}
else if(ch==9)
ob.popNode();
else if(ch==10)
ob.displayAll2(ob.getHead());
else if(ch==11)
exit(0);
}
}

http://www.ravianeducation.blogspot.com
FARHAN: 03008855006

Das könnte Ihnen auch gefallen