Coding | Mcqs | Multiple choice questions | Informative | Computer Science | Engineering | Aptitude | Quants | Verbal

INFEED

KRCE | KRCT | Singly LinkedList | 11 May

Middle element in the linked list

Given a singly linked list, find the middle element of the linked list. The size of the linked list will always be odd.


Input Format


First line contain size of linked list 'n'. Next line contains n integers present in the linked list.


Constraints


1<=n<=100


Output Format


A single integer representing the middle of the linked list.


Sample Input 0


5

2 4 5 3 4 

Sample Output 0


5

Sample Input 1


7

1 1 1 1 1 1 1

Sample Output 1


1

-----------------------

CODE IN C:

 

#include <stdio.h>

#include <stdlib.h>


struct node {

    int data;

    struct node* next;

};


void printMiddle(struct node* head)

{

    int count = 0;

    struct node* mid = head;


    while (head != NULL) {

        if (count & 1)

            mid = mid->next;


        ++count;

        head = head->next;

    }

    if (mid != NULL)

        printf("%d", mid->data);

}


void push(struct node** head_ref, int new_data)

{

    /* allocate node */

    struct node* new_node

        = (struct node*)malloc(sizeof(struct node));


    new_node->data = new_data;


    new_node->next = (*head_ref);


    (*head_ref) = new_node;

}


int main()

{

    struct node* head = NULL;

    int a;scanf("%d\n",&a);

    int m;


    for (int i = 0; i < a; i++) {

        scanf("%d ",&m);

        push(&head, m);

    }

        printMiddle(head);



    return 0;

}

***********************************

Pairwise  Swap 1


Write a program to perform pairwise swap of linked list

Input Format

The first line consist of an upper limit of the list(including the integer).

The second line consists of lower limit of the list(excluding the integer).

Constraints

The upper and lower limits should be positive integers.

Output Format

The first line consists of elements of linked list.

The second line consists of pairwise swapped elements of the list.

Sample Input 0

5
2
Sample Output 0

3 4 5 
4 3 5
Sample Input 1

6
3
Sample Output 1

4 5 6 
5 4 6


C Code:
#include <stdio.h>
#include <stdlib.h>

/* A linked list node */
struct Node {
    int data;
    struct Node* next;
};

/*Function to swap two integers at addresses a and b */
void swap(int* a, int* b);

/* Function to pairwise swap elements of a linked list */
void pairWiseSwap(struct Node* head)
{
    struct Node* temp = head;

    /* Traverse further only if there are at-least two nodes left */
    while (temp != NULL && temp->next != NULL) {
        /* Swap data of node with its next node's data */
        swap(&temp->data, &temp->next->data);

        /* Move temp by 2 for the next pair */
        temp = temp->next->next;
    }
}

/* UTILITY FUNCTIONS */
/* Function to swap two integers */
void swap(int* a, int* b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

/* Function to add a node at the beginning of Linked List */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

    /* put in the data */
    new_node->data = new_data;

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref) = new_node;
}

/* Function to print nodes in a given linked list */
void printList(struct Node* node)
{
    while (node != NULL) {
        printf("%d ", node->data);
        node = node->next;
    }
}

/* Driver program to test above function */
int main()
{
    struct Node* start = NULL;
int a,b;scanf("%d\n%d",&a,&b);
    for(int i=a;i>b;i--)
    push(&start, i);

    printList(start);
printf("\n");
    pairWiseSwap(start);

    printList(start);

    return 0;
}

Post a Comment

Previous Post Next Post