You are on page 1of 4

Code 1 :

Question 1: (Linked List)


Write a function that takes three arguments—a pointer to a linked list of integers and two
integers, n and j—and inserts n after the jth element of the list. If j is 0, nis inserted at the head
of the list. If j is greater than the number of elements in the list, n is inserted after the last one.
Solution
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int a;
struct node *next;
};
void insert(struct node **p, int n, int j)
{
struct node *nn=(struct node*)malloc(sizeof(struct node));
nn->a=n;nn->next=NULL;
if(j==0)
{
nn->next=*p;*p=nn;
}
else
{
int i;struct node *t1=*p, *t2=*p;
for(i = 0; t1 != NULL; i++, t2 = t1, t1 = t1 -> next)
{
if(i == j) break;
}
if(t1==NULL)//if no of nodes are less than j
{
t2->next=nn;return;
}
nn->next=t1;
t2->next=nn;
}
}
void display(struct node **p)//displays list
{
for(struct node *temp=*p;temp != NULL;temp=temp->next)
{
cout<<temp->a<<" ";
}
cout<<endl;
}
int main()
{
struct node *p;
p=(struct node *)malloc(sizeof(struct node));
p->a=1;
p->next=NULL;
while(1)
{
cout<<"enter 0 to break, 1 to continue "<<endl;
int ch;cin>>ch;
if(ch==0) break;
cout<<"enter integer and position"<<endl;
int num, pos;cin>>num>>pos;
insert(&p, num, pos);display(&p);
}
return 0;
}

-----------------------------------------------------------------------------------------------------------------------------
Code 2:
A stack, S1, contains some numbers in arbitrary order. Using another stack, S2, for temporary
storage, show how to sort the numbers in S1 such that the smallest is at the top of S1 and the
largest is at the bottom.
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
cin>>n
do
{
printf("\n Enter the Choice:");
cin>>choice;
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}case 4:
{
printf("\n\t EXIT ");
break;
}
default:
{
cout<<"\n\t Please select from(1-2-3-4)";
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
cout<<"\n\tSTACK is over flow";
}
else
{
cout<<" Enter a value to be pushed:";
cin>>x
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
cout<<"\n\t Stack is under flow\n";
}
else
{
cout<<stack[top]<<endl;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"\n The elements in STACK \n";
for(i=top; i>=0; i--)
cout<<"\n%d",stack[i];
cout<<"\n Press Next Choice";
}
else
{
cout<<"\n The STACK is empty";
}case 4:
{
Cout<<"\n\t EXIT ";
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1-2-3-4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The retrived elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}}

You might also like