They will ask u question about single linked list?. Write
Code for to insert delete node.
Answers were Sorted based on User's Feedback
Answer / vadivelt
This code conatains the answer for add, del, insert node in
sigle linked list and some other possible operations
#include<stdio.h>
#include<conio.h>
#define ADDATBEG 1
#define ADDATEND 2
#define INSERT 3
#define DEL 4
#define SEARCH 5
#define SORT 6
#define REVERSE 7
#define NODEFROMEND 8
struct node
{
int data;
struct node *next;
};
struct node *BaseNode = '\0';
/*Function prototypes*/
void AddAtBeg(struct node **BaseNode, int data);
void AddAtEnd(struct node **BaseNode, int data);
void AddAtMid(struct node **BaseNode, int data, int pos);
void DelNode(struct node **BaseNode, int pos);
void DisList(struct node **BaseNode);
void SearchNode(struct node **BaseNode, int data);
void funchoice(int choice);
void SortList(struct node **BaseNode);
void NthNodeFrmEnd(struct node **BaseNode, int n);
void RevList(struct node **BaseNode);
int main()
{
int i, choice;
printf("ENTER THE CHOICE:\n");
printf("1.TO ADD THE NODE AT BEGINING - STACK \n2.TO ADD
THE NODE AT THE END - QUEUE\n");
printf("\nCHOICE:");
scanf("%d", &choice);
funchoice(choice);
printf("\n\nENTER CHOICE \n");
printf("3.TO INSERT NODE\n");
printf("4.TO DEL NODE\n");
printf("5.TO SEARCH GIVEN NODE IN THE LIST\n");
printf("6.TO SORT THE LIST\n");
printf("7.TO REVERSE THE LIST\n");
printf("8.VALUE OF 'N'th FROM LAST\n");
printf("\nCHOICE:");
scanf("%d", &choice);
funchoice(choice);
getch();
}
void funchoice(int choice)
{
int no = 0, element = 0,i;
switch(choice)
{
case ADDATBEG:
printf("\nENTER THE NO OF ELEMENTS TO BE ADDED\n");
scanf("%d", &no);
printf("\nENTER THE ELEMENTS TO BE ADDED IN THE
LIST\n");
for(i = 0; i<no; i++)
{
scanf("%d", &element);
AddAtBeg(&BaseNode, element);
}
DisList(&BaseNode);
break;
case ADDATEND:
printf("\nENTER THE NO OF ELEMENTS TO BE ADDED\n");
scanf("%d", &no);
printf("\nENTER THE ELEMENTS TO BE ADDED IN THE
LIST\n");
for(i = 0; i<no; i++)
{
scanf("%d", &element);
AddAtEnd(&BaseNode, element);
}
DisList(&BaseNode);
break;
case INSERT:
printf("\nENTER THE NODE POSTION WHERE THE NEW NODE
TO BE INSERTED\n");
scanf("%d", &no);
printf("\nENTER THE NO TO BE INSERTED\n");
scanf("%d", &element);
AddAtMid(&BaseNode, element, no);
DisList(&BaseNode);
break;
case DEL:
printf("\nENTER THE NODE POSTION OF THE NODE TO BE
DELETED\n");
scanf("%d", &no);
DelNode(&BaseNode, no);
DisList(&BaseNode);
break;
case SEARCH:
printf("\nENTER THE VALUE TO BE SEARCHED IN THE
LIST\n");
scanf("%d", &element);
SearchNode(&BaseNode, element);
break;
case SORT:
SortList(&BaseNode);
DisList(&BaseNode);
break;
case REVERSE:
RevList(&BaseNode);
DisList(&BaseNode);
break;
case NODEFROMEND:
printf("\nENTER THE NODE POSITION FROM END TO WHICH
THE VALUE HAS TO BE FOUND\n");
scanf("%d", &no);
NthNodeFrmEnd(&BaseNode, no);
break;
default:
exit(0);
break;
}
}
/*To print the data in the linked list*/
void DisList(struct node **BaseNode)
{
struct node *temp;
temp = *BaseNode;
printf("\nNODES IN THE LIST IS \n");
while(temp != '\0')
{
printf("%d ", temp->data);
temp = temp->next;
}
}
/*Stack Implementation*/
void AddAtBeg(struct node **BaseNode, int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp != '\0')
{
temp->data = data;
temp->next = *BaseNode;
*BaseNode = temp;
}
}
/*Queue Implementation*/
void AddAtEnd(struct node **BaseNode, int data)
{
struct node *temp, *temp1;
temp = *BaseNode;
if(temp == '\0')
{
/*List is empty - Create first node*/
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = '\0';
*BaseNode = temp1;
}
}
else
{
/*Node is existing already, So create another node*/
while(temp->next != '\0')
{
temp = temp->next;
}
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = '\0';
temp->next = temp1;
}
}
}
/*To insert a node between two nodes*/
void AddAtMid(struct node **BaseNode, int data, int pos)
{
/*To insert a node in between to the given node*/
struct node *temp, *temp1;
int count = 0;
temp = *BaseNode;
/*count, the no of nodes available in the list*/
while(temp->next != '\0')
{
count++;
temp = temp->next;
}
/*Check whether the enough nodes are available or not*/
if(pos <= (count+1))
{
temp = *BaseNode;
while(pos-1)
{
temp = temp->next;
pos--;
}
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = temp->next;
temp->next = temp1;
}
}
else
{
printf("NEW NODE CANT BE ADDED:THERE IS NO ENOUGH
NODES IN THE LIST\n");
}
}
/*To delete a particular node in the list*/
void DelNode(struct node **BaseNode, int pos)
{
struct node *temp, *temp1;
int count = 0, pos1;
temp = *BaseNode;
pos1 = pos;
/*count, the no of nodes available in the list*/
while(temp->next != '\0')
{
count++;
temp = temp->next;
}
/*Check whether the enough nodes are available or not*/
if(pos <= (count+1))
{
temp = *BaseNode;
temp1 = temp->next;
while(pos-2)
{
temp = temp->next;
temp1 = temp1->next;
pos--;
}
temp->next = temp1->next;
printf("\n\nTHE DATA '%d' AT THE NODE '%d' IS
DELETED \n\n", temp1->data, pos1);
free(temp1);
}
else
{
printf("\n\nGIVEN NODE CANT BE DELETED:THERE IS NO
ENOUGH NODES IN THE LIST\n");
}
}
/*To search the given data in the linked list*/
void SearchNode(struct node **BaseNode, int data)
{
struct node *temp;
int count = 0;
temp = *BaseNode;
while(temp != '\0')
{
count++;
if(temp->data == data)
{
printf("\nTHE NODE WITH DATA '%d' FOUND AT
THE POSITION '%d'\n", data, count);
break;
}
else if(temp->next == '\0')
{
printf("\nTHE NODE IS NOT FOUND IN THE
LIST\n");
}
temp = temp->next;
}
}
/*To Sort the linked list in ascending order*/
void SortList(struct node **BaseNode)
{
struct node *temp, *temp1;
int localvar;
temp = *BaseNode;
temp1 = temp->next;
while(temp1 != '\0')
{
if(temp->data > temp1->data)
{
localvar = temp->data;
temp->data = temp1->data;
temp1->data = localvar;
}
temp = temp->next;
temp1 = temp1->next;
}
}
/*To find the 'n'th node from last node of linked list by
traversing the list only once*/
void NthNodeFrmEnd(struct node **BaseNode, int n)
{
struct node *temp, *temp1;
int count = 0;
temp1 = temp = *BaseNode;
while(temp1 != '\0')
{
count++;
temp1 = temp1->next;
}
if(n <= count)
{
count = 0;
temp1 = *BaseNode;
while(temp1->next != '\0')
{
count++;
if(count > n-1)
{
temp = temp->next;
}
temp1 = temp1->next;
}
printf("\nNODE %d FROM LAST, HOLD THE VALUE %d\n",
n, temp->data);
}
else
{
printf("\nTHERE IS NO ENOUGH NODES IN THE LIST\n");
}
}
/*To Reverse a given linked list*/
void RevList(struct node **BaseNode)
{
struct node *Fst, *Sec, *Thrd;
Sec = (*BaseNode)->next;
Thrd = Sec->next;
(*BaseNode)->next = '\0';
while(Thrd != '\0')
{
Sec->next = *BaseNode;
*BaseNode = Sec;
Sec = Thrd;
Thrd = Thrd->next;
}
Sec->next = *BaseNode;
*BaseNode = Sec;
}
Is This Answer Correct ? | 4 Yes | 1 No |
Answer / soumya
Represent a list of integers as singly linked list. Write
functions for ADD, DELETE and SEARCH operations.
#include<iostream>
using namespace std;
class student
{
int age;
student *nxt;
public:
void add_at_front();
void add_at_end();
void add_at_any();
void del_at_front();
void del_at_end();
void del_at_any();
void display();
void search();
};
student *start=NULL;
void student::add_at_front()
{
student *s1;
s1=new student;
cout<<"Enter the age";
cin>>s1->age;
s1->nxt=start;
start=s1;
}
void student:: add_at_end()
{
student *s1;
s1=new student;
cout<<"Enter the age";
cin>>s1->age;
s1>nxt=NULL;
if(start==NULL)
{
start=s1;
}
else
{
student *temp;
temp=new student;
temp=start;
while(temp->nxt!=NULL)
{
temp=temp->nxt;
}
temp->nxt=s1;
}
}
void student::add_at_any()
{
student *s1;
s1=new student;
int x;
cout<<"Enter age of the node you want to specify:";
cin>>x;
cout<<"Enter the age of the new node";
cin>>s1>age;
student *temp;
temp=new student;
temp=start;
while((temp->age!=x)&&(temp->nxt!=NULL))
{
temp=temp->nxt;
}
if(temp->nxt=NULL)
{
cout<<"Error";
}
else
{
s1->nxt=temp->nxt;
temp->nxt=s1;
}
}
void student::del_at_front()
{
student *temp;
if(start==NULL)
{
cout<<"Empty list";
}
else
{
temp=start;
start=start->nxt;
delete temp;
}
}
void student::del_at_end()
{
student *temp,*temp1;
if(start==NULL)
{
cout<<"Empty list";
}
else
{
temp=start;
temp1=start;
while(temp->nxt!=NULL)
{
temp1=temp;
temp=temp->nxt;
}
}
delete temp;
temp1->nxt=NULL;
}
void student::del_at_any()
{
student *temp,*temp1;
int x;
cout<<"Enter the age of the node to be deleted:";
cin>>x;
temp=start;
temp1=start;
while((temp->nxt!=NULL)&&(temp->age!=x))
{
temp1=temp;
temp=temp->nxt;
}
if(temp->nxt==NULL)
{
cout<<"Error in inputs:";
}
else
{
temp1->nxt=temp->nxt;
delete temp;
}
}
int main()
{
int opt;
char ch;
student s;
do
{
cout<<"*******OPERATIONS******\n:1.add_front\n2.add_end\n3.add_any\
n4.del_fround\n5.del
_end\n6.del_any\n7.display\n8.search\n9.exit\nEnter your
choice:";
cin>>opt;
switch(opt)
{
case 1:s.add_at_front();
break;
case 2:s.add_at_end();
break;
case 3:s.add_at_any();
break;
case 4:s.del_at_front();
break;
case 5:s.del_at_end();
break;
case 6:s.del_at_any();
break;
case 7:s.display();
break;
case 8:s.search();
break;
case 9:break;
default : cout<<"Error";
break;
}
//cout<<"Do you want to continue (Y/N)";
//cin>>ch;
}
while(opt!=9);
}
void student::display()
{
student *temp;
temp=start;
while(temp!=NULL)
{
cout<<temp->age<<"\n" ;
temp=temp->nxt;
}
}
void student::search()
{
student *s1;
s1=new student;
int x,n,f=0;
n=1;
cout<<"Enter the age you want to search ";
cin>>x;
s1=start;
while(s1->age!=x)
{
s1=s1->nxt;
n=n+1;
if((s1->nxt==NULL)&&(s1->age!=x))
{
f=1;
break;
}
}
if(f==1)
cout<<"Element not fount";
else
cout<<s1->age<<"is at position"<<n<<"\n\n";
}
Is This Answer Correct ? | 2 Yes | 1 No |
A mXn matrix is given and rows and column are sorted as shown below.Write a function that search a desired entered no in the matrix .with minimum complexity 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
What do you understand by pure virtual function? Write about its use?
What is a manipulative person?
Is set c++?
Can I create my own functions in c++?
What do you mean by “this” pointer?
What is iomanip c++?
Where must the declaration of a friend function appear?
What is the difference in size of this two clasees? Class A { int a; char c; float f; } Class B { float f; char c; int a; }
this is to swap to strings....but in output the whole strings are swapped leaving first as it is...why it is so #include<iostream.h> int main() { char a[]="ajeet"; char b[]="singh"; long x=*a; long y=*b; cout<<x<<":"<<y; x=x+y; y=x-y; x=x-y; *a=x; *b=y; cout<<x<<":"<<y; cout<<&a<<endl; cout<<&b<<endl; }
How much do c++ programmers make?
How do you import payscale data from non SAP to SAP?is it through LSMW or any other way is there?