0% found this document useful (0 votes)
6 views

Lab_7_adding_node_at_the_beggining_in_singly_linked_list_using_c++

Uploaded by

kalkingyahoo312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Lab_7_adding_node_at_the_beggining_in_singly_linked_list_using_c++

Uploaded by

kalkingyahoo312
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

/*
* A program that demonistrates implementation
* of linked and different operations on linked list.

*/
using namespace std;
struct Node{
int id;
char name[15];
int age;
Node* next;
}*head=NULL;

Node* getNode(){
Node* temp=new Node;
cout<<"Enter Id:";
cin>>temp->id;
cout<<"Enter name:";
cin>>temp->name;
cout<<"Enter Age:";
cin>>temp->age;
temp->next=NULL;
return temp;
}

int size(){
Node*curr=head;
int count=0;
while(curr!=NULL){
count++;
curr=curr->next;
}
return count;
}

void display(){
Node*curr=head;
cout<<endl<<"Displaying all the student data"<<endl;
while(curr!=NULL){
cout<<"Student ID"<<"\t"<<"Student Name"<<"\t"<<"Student Age"<<endl;
cout<<curr->id<<"\t \t"<<curr->name<<"\t \t"<<curr->age<<endl;
curr=curr->next;
cout<<endl;
}
}

void insertAtBeginning(){
Node *temp=new Node;

cout<<"Now Enter the data"<<endl;


cout<<endl;
cout<<"Enter Student Id :";
cin>>temp->id;
cout<<"Enter Student Name :";
cin>>temp->name;
cout<<"Enter Student AGE :";
cin>>temp->age;
cout<<"one student data inserted"<<endl;
cout<<endl;
if(head==NULL){
temp->next=NULL;

}
else{
temp->next=head;

}
head=temp;

}
int main(int argc, char** argv) {

int choice;
int index;
do{
cout<<"WELCOME TO LINKED LIST OPERATION EXAMPLE"<<endl;
cout<<"======MENU==========\n";
cout<<"Press 1 to insert node at the beginning.\n";
cout<<"Press 2 to display all nodes.\n";
cout<<"Press 3 to display the size of the linked list.\n";
cout<<"Press 4 to close the program.\n";

cout<<"\nEnter your choice :";


cin>>choice;
switch(choice){
case 1:
insertAtBeginning();
break;
case 2:
display();
break;
case 3:
cout<<"Size of the Linked list is :"<<size()<<endl;
case 4:
break;
default:
cout<<" SORRY!!!"<<" "<<"YOUR CHOICE IS NOT CORRECT !!!"<<"
"<<"TRY AGAIN\n";
break;

}while(choice!=4);

return 0;
}

You might also like