mittalpradeep84
BAN USER#include<stdio.h>
struct node
{
int value;
struct node *next;
};
struct node *head;
void createLinkList(void);
void swap_element(int elementPosition);
void printList();
main()
{
int elementPosition;
createLinkList();
printf("Enter the Kth Postion to swap: ");
scanf("%d",&elementPosition);
swap_element(elementPosition);
printList();
}
void createLinkList(void)
{
int ch;
struct node *previousNode,*nextNode;
head = previousNode = (struct node *)malloc(sizeof(struct node));
printf("\nPress -1 for Not entering Node. \n\n");
do
{
nextNode = (struct node *)malloc(sizeof(struct node));
if(nextNode)
{
scanf("%d",&nextNode->value);
nextNode->next = NULL;
previousNode->next = nextNode;
previousNode = nextNode;
}
//fflush(ch);
printf("\nEnter -1 for terminate the link list.: ");
scanf("%d",&ch);
}while(ch != -1);
}
void swap_element(int elementPosition)
{
int counter=0;
struct node *swapNode1 = NULL, *swapNode2 =head, *startNode=head;
while(startNode)
{
if(counter == elementPosition)
swapNode1 = startNode;
if(counter>=elementPosition)
swapNode2 = swapNode2->next;
counter++;
startNode = startNode->next;
}
if(swapNode1 && swapNode2)
{
int temp = swapNode1->value;
swapNode1->value = swapNode2->value;
swapNode2->value = temp;
}
}
void printList()
{
struct node *Node=head->next;
while(Node)
{
printf("%d ",Node->value);
Node = Node->next;
}
}
#include<stdafx.h>
#include<iostream>
using namespace std;
int main()
{
int a[6] = {1,2,3,4,5,6};
int b[6] = {7,8,9,10,11,12};
int c[6] ={13,14,15,16,17,18};
int x,y,z;
for(int i=5; i>=0;i--)
{
if(a[i] <=b[0] || i ==0)
{
x= a[i];
y = b[0];
break;
}
}
for(int i =5; i>=0;i--)
{
if(y>=c[i] || i ==0)
{
z=c[i];
break;
}
}
cout << x << " "<<y<<" "<<z;
}
Find the max(x,y,z) - min(x,y,z), result will be minimum.
#include<stdafx.h>
#include<iostream>
using namespace std;
int main()
{
const int size = 10;
int a[size] = {1,0,3,2,5,4,7,6,9,8};
for(int i =0; i<size;i++)
{
if((i%2) != 0)
{
if((a[i]%2) == 0)
{
int temp = a[i];
a[i] = a[size-1];
a[size-1] =temp;
}
}
else
{
if((a[i]%2) != 0)
{
int temp = a[i];
a[i] = a[size-1];
a[size-1] =temp;
}
}
}
for(int i =0; i<size;i++)
cout << a[i] << " ";
}
#include<stdio.h>
- mittalpradeep84 June 21, 2013struct node
{
int value;
struct node *next;
};
struct node *head;
void createLinkList(void);
void swap_element(int elementPosition);
void printList();
main()
{
int elementPosition;
createLinkList();
printf("Enter the Kth Postion to swap: ");
scanf("%d",&elementPosition);
swap_element(elementPosition);
printList();
}
void createLinkList(void)
{
int ch;
struct node *previousNode,*nextNode;
head = previousNode = (struct node *)malloc(sizeof(struct node));
printf("\nPress -1 for Not entering Node. \n\n");
do
{
nextNode = (struct node *)malloc(sizeof(struct node));
if(nextNode)
{
scanf("%d",&nextNode->value);
nextNode->next = NULL;
previousNode->next = nextNode;
previousNode = nextNode;
}
//fflush(ch);
printf("\nEnter -1 for terminate the link list.: ");
scanf("%d",&ch);
}while(ch != -1);
}
void swap_element(int elementPosition)
{
int counter=0;
struct node *swapNode1 = NULL, *swapNode2 =head, *startNode=head;
while(startNode)
{
if(counter == elementPosition)
swapNode1 = startNode;
if(counter>=elementPosition)
swapNode2 = swapNode2->next;
counter++;
startNode = startNode->next;
}
if(swapNode1 && swapNode2)
{
int temp = swapNode1->value;
swapNode1->value = swapNode2->value;
swapNode2->value = temp;
}
}
void printList()
{
struct node *Node=head->next;
while(Node)
{
printf("%d ",Node->value);
Node = Node->next;
}
}