Wednesday, October 29, 2014

C Pratical Pro



Find the Largest Element in a Doubly Linked List

#include 
#include 
#include 
struct node
{
    int num;
    struct node *next;
    struct node *prev;
};
void create(struct node **);
int max(struct node *);
void release(struct node **);
int main()
{
    struct node *p = NULL;
    int n;
    clrscr();
    printf("Enter data into the list\n");
    create(&p);
    n = max(p);
    printf("The maximum number entered in the list is %d.\n", n);
    release (&p);
    return 0;
}
int max(struct node *head)
{
    struct node *max, *q;
    q = max = head;
    while (q != NULL)
    {
        if (q->num > max->num)
        {
            max = q;
        }
        q = q->next;
    }
     return (max->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;
        temp->prev = NULL;
        if (*head == NULL)
        {
            *head = temp;
        }
        else
        {
            rear->next = temp;
            temp->prev = rear;
        }
        rear = 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: 12
Do you wish to continue [1/0]: 1
Enter number: 7
Do you wish to continue [1/0]: 1
Enter number: 23
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 16
Do you wish to continue [1/0]: 0
The maximum number entered in the list is 23.


Solve the Magic Squares Puzzle without using Recursion

#include 
#include 
void magicsq(int, int [][10]);
int main( )
{
    int size;
    int a[10][10];
    clrscr();
    printf("Enter the size: ");
    scanf("%d", &size);
    if (size % 2 == 0)
    {
        printf("Magic square works for an odd numbered size\n");
    }
    else
    {
        magicsq(size, a);
    }
    return 0;
}
 void magicsq(int size, int a[][10])
{
    int sqr = size * size;
    int i = 0, j = size / 2, k;
    for (k = 1; k <= sqr; ++k) 
    {
        a[i][j] = k;
        i--;
        j++;
 
        if (k % size == 0) 
        { 
            i += 2; 
            --j; 
        }
        else 
        {
            if (j == size) 
            {
                j -= size;
            }
            else if (i < 0)
            {
                i += size;
            }
        }
    }
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    getch();
}
 
Output
 
Enter the size: 6
Magic square works for an odd numbered size
 
$ a.out
Enter the size: 5
17  24  1  8  15  
23  5  7  14  16  
4  6  13  20  22  
10  12  19  21  3  
11  18  25  2  9



No comments:

Post a Comment