Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Linked List
Home > Programs > Linked List

30. Write an efficient program to find the middle element in the list

Answer:


#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;
    struct node *next;
};

void create(struct node **);
void middlenode(struct node *);
void release(struct node **);

int main()
{
    struct node *p = NULL;

    printf("Enter data into the list\n");
    create(&p);
    middlenode(p);
    release (&p);

    return 0;
}

void middlenode(struct node *head)
{
    struct node *p, *q;
    int flag = 0;

    q = p = head;
    /*for every two hops of q, one hop for p*/
    while (q->next != NULL)
    {
        q = q->next;
        if (flag)
        {
            p = p->next;
        }
        flag = !flag;
    }
    if (flag)
    {
        printf("List contains even number of nodes\nThe middle two node's values are: %d  %d\n", p->next->num, p->num);
    }
    else
    {
        printf("The middle node of the list is: %d\n", p->num);
    }
}

void create(struct node **head)
{
    int c, ch;
    struct node *temp;

    do
    {
        printf("Enter number: ");
        scanf("%d", &c);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = c;
        temp->next = *head;
        *head = temp;
        printf("Do you wish to continue [1/0]: ");
        scanf("%d", &ch);
    } while (ch != 0);
    printf("\n");
}

void release(struct node **head)
{
    struct node *temp = *head;
    *head = (*head)->next;
    while ((*head) != NULL)
    {
        free(temp);
        temp = *head;
        (*head) = (*head)->next;
    }
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here: