Wednesday, October 29, 2014

C Linked list



Add Corresponding Positioned Elements of 2 Linked Lists

#include 
#include 
#include 
#include 
struct node
{
    int num;
    struct node *next;
};
int feednumber(struct node **);
struct node *addlist(struct node *, struct node *, int, int);
void release(struct node **);
void display(struct node *);
int main()
{
    struct node *p = NULL;
    struct node *q = NULL;
    struct node *res = NULL;
    int pcount = 0, qcount = 0;
    printf("Enter first number\n");
    pcount = feednumber(&p);
    printf("Enter second number\n");
    qcount = feednumber(&q);
    printf("Displaying list1: ");
    display(p);
    printf("Displaying list2: ");
    display(q);
    res = addlist(p, q, pcount, qcount);
    printf("Displaying the resulting list: ");
    display(res);
    release(&p);
    release(&q);
    release(&res);
     return 0;
}
 int feednumber(struct node **head)
{
    char ch, dig;
    int count = 0;
    struct node *temp, *rear = NULL;
    ch = getchar();
    while (ch != '\n')
    {
        dig = atoi(&ch);
        temp = (struct node *)malloc(sizeof(struct node));
        temp->num = dig;
        temp->next = NULL;
        count++;
        if ((*head) == NULL)
        {
            *head = temp;
            rear = temp;
        }
        else
        {
            rear->next = temp;
            rear = rear->next;
        }
        ch = getchar();
    }
     return count;
  }
   void display (struct node *head)
   {
    while (head != NULL)
    {
        printf("%d", head->num);
        head = head->next;
    }
    printf("\n");
}
void release (struct node **head)
{
    struct node *temp = *head;
    while ((*head) != NULL)
    {
        (*head) = (*head)->next;
        free(temp);
        temp = *head;
    }
}
 struct node *addlist(struct node *p, struct node *q, int pcount, int qcount)
{
    struct node *ptemp, *qtemp, *result = NULL, *temp;
    int i, carry = 0;
    while (pcount != 0 && qcount != 0)
    {
        ptemp = p;
        qtemp = q;
        for (i = 0; i < pcount - 1; i++)
        {
            ptemp = ptemp->next;
        }
        for (i = 0; i < qcount - 1; i++)
        {
            qtemp = qtemp->next;
        }
        temp = (struct node *) malloc (sizeof(struct node));
        temp->num = ptemp->num + qtemp->num + carry;
        carry = temp->num / 10;
        temp->num = temp->num % 10;
        temp->next = result;
        result = temp;
        pcount--;
        qcount--;
    }
    while (pcount != 0)
    {
        ptemp = p;
        for (i = 0; i < pcount - 1; i++)
        {
            ptemp = ptemp->next;
        }
        temp = (struct node *) malloc (sizeof(struct node));
        temp->num = ptemp->num + carry;
        carry = temp->num / 10;
        temp->num = temp->num % 10;
        temp->next = result;
        result = temp;
        pcount--;
    }
    while (qcount != 0)
    {
        qtemp = q;
        for (i = 0; i < qcount - 1; i++)
        {
            qtemp = qtemp->next;
        }
        temp = (struct node *) malloc (sizeof(struct node));
        temp->num = qtemp->num + carry;
        carry = temp->num / 10;
        temp->num = temp->num % 10;
        temp->next = result;
        result = temp;
        qcount--;
    }
 
    return result;
}
 
Output
 
            Enter first number
12345
Enter second number
5678903
Displaying list1: 12345
Displaying list2: 5678903
Displaying the resulting list: 5691248

Check whether 2 Lists are Same

#include 
#include 
struct node
{
    int num;
    struct node *next;
};
void feedmember(struct node **);
int compare (struct node *, struct node *);
void release(struct node **);
int main()
{
    struct node *p = NULL;
    struct node *q = NULL;
    int result;
    printf("Enter data into first list\n");
    feedmember(&p);
    printf("Enter data into second list\n");
    feedmember(&q);
    result = compare(p, q);
    if (result == 1)
    {
        printf("The 2 list are equal.\n");
    }
    else
    {
        printf("The 2 lists are unequal.\n");
    }
    release (&p);
    release (&q);
    return 0;
}
int compare (struct node *p, struct node *q)
{
    while (p != NULL && q != NULL)
    {
        if (p->num != q-> num)
        {
            return 0;
        }
        else
        {
            p = p->next;
            q = q->next;
        }
    }
    if (p != NULL || q != NULL)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
void feedmember (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;
    while ((*head) != NULL)
    {
        (*head) = (*head)->next;
        free(temp);
        temp = *head;
    }
}
 
Output
 
Enter data into first list
Enter number: 12
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 28
Do you wish to continue [1/0]: 1
Enter number: 9
Do you wish to continue [1/0]: 0
Enter data into second list
Enter number: 12
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 28
Do you wish to continue [1/0]: 1
Enter number: 9
Do you wish to continue [1/0]: 0
The 2 list are equal.


        

No comments:

Post a Comment