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.

Showing posts with label list. Show all posts
Showing posts with label list. Show all posts

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);
}
Read More >>


Program Screenshot
Output

#include <iostream>
using namespace std;
struct node {
int info;
node* link;
}*start,*nNode;
node * createNew(int in) {
nNode = new node;
nNode->info=in;
return (nNode);
}
node display(node* temp) {
while(temp!=NULL) {
cout<<temp->info<<"  ";
temp=temp->link;
}
cout<<"\n";
}
node * search(node* tempPtr,int item) {
while(tempPtr!=NULL && tempPtr->info!=item) {
tempPtr=tempPtr->link;
}
return (tempPtr);
}
int main()
{
int n=2,s,item;
int a[8]={7,9,8,5,4,6,2,1};
start=NULL;
node* tempPtr;
for (int i = 0; i < 8; ++i)
{
if(start==NULL) {
start=createNew(a[i]);
start->link=NULL;
} else {
nNode=createNew(a[i]);
nNode->link=start;
start=nNode;
}
}
cout<<"Elements of linked list: \n";
display(start);
cout<<"Enter element you want to search: ";
cin>>item;
tempPtr=search(start,item);
if (tempPtr!=NULL && tempPtr->info==item) {
cout<<"Item found in list.\n";
} else {
cout<<"Item doesn't exists in list.\n";
}
return(0);
}
Read More >>


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