Sort singly linked list:
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push(int element, struct Node* head){
struct Node* temp = head;
struct Node* newNode;
newNode =(struct Node *)malloc(1*sizeof(struct Node));
newNode->data = element;
newNode->next = NULL;
while(temp->next){
temp = temp->next;
}
temp->next = newNode;
}
void sort(struct Node* head){
struct Node *current = head, *index;
int temp;
while(current){
index = current->next;
while(index){
if(current->data > index->data){
temp = current->data;
current->data = index->data;
index->data = temp;
}
index = index->next;
}
current = current->next;
}
display(head);
}
void display(struct Node* head){
printf("<----------List traversal -------->");
struct Node* temp = head->next;
while(temp){
printf("\n %d", temp->data);
temp = temp->next;
}
}
void main(){
int choice = 0, element;
struct Node* head;
head->data = 0;
head->next = NULL;
printf("\n Available Operation : ");
printf("\n 1 - Push");
printf("\n 2 - Sort");
printf("\n 3 - Display");
printf("\n 4 - End");
while(choice !=4){
printf("\n Choose operation : ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("\n Enter element to add : ");
scanf("%d", &element);
push(element, head);
break;
case 2:
sort(head);
break;
case 3:
display(head);
break;
case 4:
printf("\n operation completed");
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
--------------------------::::--------
#include <stdio.h>
#include <stdlib.h>
// function to return max of two numbers
int max(int a, int b) { return (a > b) ? a : b; }
// Function to return longest alternating subsequence length
int zzis(int arr[], int n)
{
/*las[i][0] = Length of the longest alternating subsequence
ending at index i and last element is greater
than its previous element
las[i][1] = Length of the longest alternating subsequence
ending at index i and last element is smaller
than its previous element */
int las[n][2];
/* Initialize all values from 1 */
for (int i = 0; i < n; i++)
las[i][0] = las[i][1] = 1;
int res = 1; // Initialize result
/* Compute values in bottom up manner */
for (int i = 1; i < n; i++)
{
// Consider all elements as previous of arr[i]
for (int j = 0; j < i; j++)
{
// If arr[i] is greater, then check with las[j][1]
if (arr[j] < arr[i] && las[i][0] < las[j][1] + 1)
las[i][0] = las[j][1] + 1;
// If arr[i] is smaller, then check with las[j][0]
if( arr[j] > arr[i] && las[i][1] < las[j][0] + 1)
las[i][1] = las[j][0] + 1;
}
/* Pick maximum of both values at index i */
if (res < max(las[i][0], las[i][1]))
res = max(las[i][0], las[i][1]);
}
return res;
}
/* Driver program */
int main()
{
int arr[] = { 10, 22, 9, 33, 49, 50, 31, 60 };
int n = sizeof(arr)/sizeof(arr[0]);
printf("Length of Longest alternating subsequence is %d\n",
zzis(arr, n) );
return 0;
}
*******************************
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void push(int element, struct Node* head){
struct Node* temp = head;
struct Node* newNode;
newNode =(struct Node *)malloc(1*sizeof(struct Node));
newNode->data = element;
newNode->next = NULL;
while(temp->next){
temp = temp->next;
}
temp->next = newNode;
}
void arrange(struct Node* head){
struct Node* temp = head->next;
struct Node* swap;
int val;
while(temp){
if(temp->data%2 == 0){
swap = temp->next;
while(swap->data){
if(swap->data%2 == 0){
break;
}
swap = swap->next;
}
val = swap->data;
swap->data = temp->data;
temp->data = val;
}
temp = temp->next;
}
}
void display(struct Node* head){
printf("<----------Queue traversal -------->");
struct Node* temp = head->next;
while(temp){
printf("\n %d", temp->data);
temp = temp->next;
}
}
void main(){
int choice = 0, element;
struct Node* head;
head->data = 0;
head->next = NULL;
printf("\n Available Queue operation : ");
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Display");
printf("\n 4 - End");
while(choice !=4){
printf("\n Choose operation : ");
scanf("%d", &choice);
switch(choice){
case 1:
printf("\nEnter element to add : ");
scanf("%d", &element);
push(element, head);
break;
case 2:
arrange(head);
break;
case 3:
display(head);
break;
case 4:
printf("\n operation completed");
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
Comments
Post a Comment