A blog to help students to learn programming in various languages including C, C++, Java, Python and many more.

Subscribe For Free Latest Updates!

We'll not spam mate! We promise.

Sunday 24 January 2016



Program Screenshot
Output
#include <iostream>
using namespace std;
struct node {
int info;
node* link;
}*start,*nNode;
node * createNew() {
nNode = new node;
cout<<"Enter info for node: ";
cin>>nNode->info;
return (nNode);
}
node display(node* temp) {
while(temp!=NULL) {
cout<<temp->info<<"\n";
temp=temp->link;
}
}
int main() {
int n=2,s,af;
start=NULL;
node* tempPtr;
cout<<"Press 1 to enter new node: ";
cin>>n;
while(n==1) {
cout<<"Where you want to insert node: \n1- In begining\t\t2- In between\t\t3- At end\n";
cin>>s;
switch(s) {
case 1:
if(start==NULL) {
start=createNew();
start->link=NULL;
} else {
nNode=createNew();
nNode->link=start;
start=nNode;
}
break;
case 2:
cout<<"\nInfo in linked list: \n";
display(start);
cout<<"After which info you want new element: ";
cin>>af;
tempPtr=start;
while(tempPtr!=NULL && tempPtr->info!=af) {
tempPtr=tempPtr->link;
}
if (tempPtr==NULL) {
cout<<"Entered info not found.\n";
} else {
nNode=createNew();
nNode->link=tempPtr->link;
tempPtr->link=nNode;
}
break;
case 3:
tempPtr=start;
while(tempPtr->link!=NULL && tempPtr!=NULL) {
tempPtr=tempPtr->link;
}
nNode=createNew();
tempPtr->link=nNode;
nNode->link=NULL;
break;
default:
cout<<"\n**************Error**************\n";
}
cout<<"Press 1 to enter new node or other no. to exit: ";
cin>>n;
}
tempPtr=start;
cout<<"\nInfo in linked list:\n";
display(start);
return (0);
}

0 comments :

Post a Comment



Copyright © 2016 - ProgrmIt - All Rights Reserved
(Articles Cannot Be Reproduced Without Author Permission.)
Design By : | Powered By: Blogger