Friday, December 26, 2014

Linked List in Reverse in c pgm



Print Middle Most Node of a Linked List

#include 
#include 
#include 
 
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;
        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;
    }
}

 
Output
 
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
 
The middle node of the list is: 3

Read a Linked List in Reverse

#include 
#include 
#include 
 
struct node
{
    int num;
    struct node *next;
};
 
void create(struct node **);
void reversedisplay(struct node *);
void release(struct node **);
void display(struct node *);
 
int main()
{
    struct node *p = NULL;
    struct node_occur *head = NULL;
    int n;
 
    printf("Enter data into the list\n");
    create(&p);
    printf("Displaying the nodes in the list:\n");
    display(p);
    printf("Displaying the list in reverse:\n");
    reversedisplay(p);
    release(&p);
 
    return 0;
}
 
void reversedisplay(struct node *head)
{
    if (head != NULL)
    {
        reversedisplay(head->next);
        printf("%d\t", head->num);
    }
}
 
void create(struct node **head)
{
    int c, ch;
    struct node *temp, *rear;
 
    do
    {
        printf("Enter number: ");
        scanf("%d", &c);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = c;
        temp->next = NULL;
        if (*head == NULL)
        {
            *head = temp;
        }
        else
        {
            rear->next = temp;
        }
        rear = temp;
        printf("Do you wish to continue [1/0]: ");
        scanf("%d", &ch);
    } while (ch != 0);
    printf("\n");
}
 
void display(struct node *p)
{
    while (p != NULL)
    {
        printf("%d\t", p->num);
        p = p->next;
    }
    printf("\n");
}
 
void release(struct node **head)
{
    struct node *temp = *head;
    *head = (*head)->next;
    while ((*head) != NULL)
    {
        free(temp);
        temp = *head;
        (*head) = (*head)->next;
    }
}

Output
 
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
1          2          3          4          5          
Displaying the list in reverse:
5          4          3          2          1

No comments:

Post a Comment