05 DSA Lab Exercise Chapter Three
05 DSA Lab Exercise Chapter Three
cout << "Please enter the name of the person: "; temp2=new node;
cin >> temp->name; cout << "Please enter the name of the person: ";
cout << "Please enter the age of the person : "; cin >> temp2->name;
cin >> temp->age; cout << "Please enter the age of the person : ";
temp->nxt = NULL; cin >> temp2->age;
if (start_ptr == NULL) temp2->nxt =NULL;
start_ptr = temp; temp->nxt=temp2;
//add at the begining } //display
//char a[5]="duga" temp = start_ptr;
temp=new node; while (temp != NULL)
cout << "Please enter the name of the person: "; { // Display details for what temp points to
cin >> temp->name; cout << "Name : " << temp->name << endl;
cout << "Please enter the age of the person : "; cout << "Age : " << temp->age << endl;
cin >> temp->age; cout << endl; // Blank line
temp->nxt= start_ptr; // Move to next node (if present)
start_ptr=temp; temp = temp->nxt;
//2. adding at the end }
if(start_ptr==NULL) }
{
Add anywhere
#include<iostream.h> temp=new node;
struct node cout << "Please enter the name of the person: ";
{ cin >> temp->name;
char name[20]; // Name of up to 20 letters cout << "Please enter the age of the person : ";
int age ; cin >> temp->age;
node *nxt;// Pointer to next node temp->nxt= NULL;
}; }
void main() else
{ { node *temp2;
struct node *start_ptr = NULL; temp= start_ptr;
node *temp; while(temp->nxt!= NULL)
temp = new node; temp=temp->nxt;
cout << "Please enter the name of the person: "; temp2=new node;
cin >> temp->name; cout << "Please enter the name of the person: ";
cout << "Please enter the age of the person : "; cin >> temp2->name;
cin >> temp->age; cout << "Please enter the age of the person : ";
temp->nxt = NULL; cin >> temp2->age;
if (start_ptr == NULL) temp2->nxt =NULL;
start_ptr = temp; temp->nxt=temp2;
//add at the begining }
//char a[5]="duga" //adding at somewhere
temp=new node; int loc;
cout << "Please enter the name of the person: "; cout<<"enter the loc";
cin >> temp->name; cin>>loc;
cout << "Please enter the age of the person : "; node *temp2;
cin >> temp->age; temp= start_ptr;
temp->nxt= start_ptr; for(int i=0;i<loc;i++)
start_ptr=temp; {
//2. // adding at the end temp=temp->nxt;
if(start_ptr==NULL) if(temp==NULL)
{
cout<<"there are less than the element u
specified"; }
} //display
temp2=new node; temp = start_ptr;
cout << "Please enter the name of the person: "; while (temp != NULL)
cin >> temp2->name; { // Display details for what temp points to
cout << "Please enter the age of the person : "; cout << "Name : " << temp->name << endl;
cin >> temp2->age; cout << "Age : " << temp->age << endl;
temp2->nxt =temp->nxt; cout << endl; // Blank line
temp->nxt =temp2; // Move to next node (if present)
//5.//count the number of nodes temp = temp->nxt;
int c=0; }
temp=start_ptr; /*char a[2];
while(temp!=NULL) cout<<"are you sure to delete?(Y/N)";
{temp=temp->nxt; cin>>a; */
c++;} }
cout<<"there are:"<<c<<"-nodes"<<endl;
//7. //sort the linked list using bubble sort
int bub_temp,k=c;
for( i=0;i<c-1;i++,k--)
{
temp= start_ptr;
temp2=temp->nxt;
for(int j=1;j<k;j++)
{
if(temp->age > temp2->age)
{
bub_temp=temp->age;
temp->age=temp2->age;
temp2->age=bub_temp;
}
temp=temp->nxt;
temp2=temp2->nxt;
}
/*1. Create a linked list structure and show cin >> temp->name;
how to add a node at the cout << "Please enter the age of the person : ";
beginning of the list and display the content. cin >> temp->age;
(node has name and age) */ temp->nxt= NULL;
#include<iostream.h> }
struct node else
{ { node *temp2;
char name[20]; // Name of up to 20 letters temp= start_ptr;
int age ; while(temp->nxt!= NULL)
node *nxt;// Pointer to next node temp=temp->nxt;
};
void main() temp2=new node;
{ cout << "Please enter the name of the person: ";
struct node *start_ptr = NULL; cin >> temp2->name;
node *temp; cout << "Please enter the age of the person : ";
temp = new node; cin >> temp2->age;
cout << "Please enter the name of the person: "; temp2->nxt =NULL;
cin >> temp->name; temp->nxt=temp2;
cout << "Please enter the age of the person : "; } //counting the nodes
cin >> temp->age; int c=0;
temp->nxt = NULL; temp=start_ptr;
if (start_ptr == NULL) while(temp!=NULL)
start_ptr = temp; {temp=temp->nxt;
temp=new node; c++;}
cout << "Please enter the name of the person: "; cout<<"there are:"<<c<<"-nodes"<<endl;
cin >> temp->name;
cout << "Please enter the age of the person : "; temp = start_ptr;
cin >> temp->age; while (temp != NULL)
temp->nxt= start_ptr; { // Display details for what temp points to
start_ptr=temp; cout << "Name : " << temp->name << endl;
if(start_ptr==NULL) cout << "Age : " << temp->age << endl;
{ cout << endl; // Blank line
temp=new node; // Move to next node (if present)
cout << "Please enter the name of the person: "; temp = temp->nxt;}}
//2. show how to add a node at the end of {
the list and display the content temp=new node;
cout << "Please enter the name of the person: ";
#include<iostream.h> cin >> temp->name;
struct node cout << "Please enter the age of the person : ";
{ cin >> temp->age;
char name[20]; // Name of up to 20 letters temp->nxt= NULL;
int age ; }
node *nxt;// Pointer to next node else
}; { node *temp2;
void main() temp= start_ptr;
{ while(temp->nxt!= NULL)
struct node *start_ptr = NULL; temp=temp->nxt;
node *temp;
temp = new node; temp2=new node;
cout << "Please enter the name of the person: ";
cout << "Please enter the name of the person: "; cin >> temp2->name;
cin >> temp->name; cout << "Please enter the age of the person : ";
cout << "Please enter the age of the person : "; cin >> temp2->age;
cin >> temp->age; temp2->nxt =NULL;
temp->nxt = NULL; temp->nxt=temp2;
if (start_ptr == NULL) } //display
start_ptr = temp; temp = start_ptr;
//add at the begining while (temp != NULL)
//char a[5]="duga" { // Display details for what temp points to
temp=new node; cout << "Name : " << temp->name << endl;
cout << "Please enter the name of the person: "; cout << "Age : " << temp->age << endl;
cin >> temp->name; cout << endl; // Blank line
cout << "Please enter the age of the person : "; // Move to next node (if present)
cin >> temp->age; temp = temp->nxt;
temp->nxt= start_ptr; }
start_ptr=temp;
//2. adding at the end
if(start_ptr==NULL) }
/*1. Create a linked list structure and show cin >> temp->name;
how to add a node at the cout << "Please enter the age of the person : ";
beginning of the list and display the content. cin >> temp->age;
(node has name and age) */ temp->nxt= NULL;
#include<iostream.h> }
struct node else
{ { node *temp2;
char name[20]; // Name of up to 20 letters temp= start_ptr;
int age ; while(temp->nxt!= NULL)
node *nxt;// Pointer to next node temp=temp->nxt;
};
void main() temp2=new node;
{ cout << "Please enter the name of the person: ";
struct node *start_ptr = NULL; cin >> temp2->name;
node *temp; cout << "Please enter the age of the person : ";
temp = new node; cin >> temp2->age;
cout << "Please enter the name of the person: "; temp2->nxt =NULL;
cin >> temp->name; temp->nxt=temp2;
cout << "Please enter the age of the person : "; } //counting the nodes
cin >> temp->age; int c=0;
temp->nxt = NULL; temp=start_ptr;
if (start_ptr == NULL) while(temp!=NULL)
start_ptr = temp; {temp=temp->nxt;
temp=new node; c++;}
cout << "Please enter the name of the person: "; cout<<"there are:"<<c<<"-nodes"<<endl;
cin >> temp->name;
cout << "Please enter the age of the person : "; temp = start_ptr;
cin >> temp->age; while (temp != NULL)
temp->nxt= start_ptr; { // Display details for what temp points to
start_ptr=temp; cout << "Name : " << temp->name << endl;
if(start_ptr==NULL) cout << "Age : " << temp->age << endl;
{ cout << endl; // Blank line
temp=new node; // Move to next node (if present)
cout << "Please enter the name of the person: "; temp = temp->nxt;}}