Wednesday, October 29, 2014

C Struct Pro



Implement a Doubly Linked List & provide Insertion, Deletion & Display Operations

#include
#include
#include
struct node
{
    struct node *prev;
    int n;
    struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();
int count = 0;
void main()
{
    int ch;
    h = NULL;
    temp = temp1 = NULL;
    printf("\n 1 - Insert at beginning");
    printf("\n 2 - Insert at end");
    printf("\n 3 - Insert at position i");
    printf("\n 4 - Delete at i");
    printf("\n 5 - Display from beginning");
    printf("\n 6 - Display from end");
    printf("\n 7 - Search for element");
    printf("\n 8 - Sort the list");
    printf("\n 9 - Update an element");
    printf("\n 10 - Exit");
    while (1)
    {
        printf("\n Enter choice : ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            insert1();
            break;
        case 2:
            insert2();
            break;
        case 3:
            insert3();
            break;
        case 4:
            delete();
            break;
        case 5:
            traversebeg();
            break;
        case 6:
            temp2 = h;
            if (temp2 == NULL)
                printf("\n Error : List empty to display ");
            else
            {
                printf("\n Reverse order of linked list is : ");
                traverseend(temp2->n);
            }
            break;
        case 7:
            search();
            break;
        case 8:
            sort();
            break;
        case 9:
            update();
            break;
        case 10:
            exit(0);
        default:
            printf("\n Wrong choice menu");
        }
    }
}
void create()
{
    int data;
    temp =(struct node *)malloc(1*sizeof(struct node));
    temp->prev = NULL;
    temp->next = NULL;
    printf("\n Enter value to node : ");
    scanf("%d", &data);
    temp->n = data;
    count++;
}
void insert1()
{
    if (h == NULL)
    {
        create();
        h = temp;
        temp1 = h;
    }
    else
    {
        create();
        temp->next = h;
        h->prev = temp;
        h = temp;
    }
}
void insert2()
{
    if (h == NULL)
    {
        create();
        h = temp;
        temp1 = h;
    }
    else
    {
        create();
        temp1->next = temp;
        temp->prev = temp1;
        temp1 = temp;
    }
}
 void insert3()
{
    int pos, i = 2;
    printf("\n Enter position to be inserted : ");
    scanf("%d", &pos);
    temp2 = h;
    if ((pos < 1) || (pos >= count + 1))
    {
        printf("\n Position out of range to insert");
        return;
    }
    if ((h == NULL) && (pos != 1))
    {
        printf("\n Empty list cannot insert other than 1st position");
        return;
    }
    if ((h == NULL) && (pos == 1))
    {
        create();
        h = temp;
        temp1 = h;
        return;
    }
    else
    {
        while (i < pos)
        {
            temp2 = temp2->next;
            i++;
        }
        create();
        temp->prev = temp2;
        temp->next = temp2->next;
        temp2->next->prev = temp;
        temp2->next = temp;
    }
}
void delete()
{
    int i = 1, pos;
    printf("\n Enter position to be deleted : ");
    scanf("%d", &pos);
    temp2 = h;
    if ((pos < 1) || (pos >= count + 1))
    {
        printf("\n Error : Position out of range to delete");
        return;
    }
    if (h == NULL)
    {
        printf("\n Error : Empty list no elements to delete");
        return;
    }
    else
    {
        while (i < pos)
        {
            temp2 = temp2->next;
            i++;
        }
        if (i == 1)
        {
            if (temp2->next == NULL)
            {
                printf("Node deleted from list");
                free(temp2);
                temp2 = h = NULL;
                return;
            }
        }
        if (temp2->next == NULL)
        {
            temp2->prev->next = NULL;
            free(temp2);
            printf("Node deleted from list");
            return;
        }
        temp2->next->prev = temp2->prev;
        if (i != 1)
            temp2->prev->next = temp2->next;   
            if (i == 1)
            h = temp2->next;
        printf("\n Node deleted");
        free(temp2);
    }
    count--;
}
 void traversebeg()
{
    temp2 = h;

    if (temp2 == NULL)
    {
        printf("List empty to display \n");
        return;
    }
    printf("\n Linked list elements from begining : ");
    while (temp2->next != NULL)
    {
        printf(" %d ", temp2->n);
        temp2 = temp2->next;
    }
    printf(" %d ", temp2->n);
}
void traverseend(int i)
{
    if (temp2 != NULL)
    {
        i = temp2->n;
        temp2 = temp2->next;
        traverseend(i);
        printf(" %d ", i);
    }
}
void search()
{
    int data, count = 0;
    temp2 = h;
     if (temp2 == NULL)
    {
        printf("\n Error : List empty to search for data");
        return;
    }
    printf("\n Enter value to search : ");
    scanf("%d", &data);
    while (temp2 != NULL)
    {
        if (temp2->n == data)
        {
            printf("\n Data found in %d position",count + 1);
            return;
        }
        else
             temp2 = temp2->next;
            count++;
    }
    printf("\n Error : %d not found in list", data);
}
 void update()
{
    int data, data1;
    printf("\n Enter node data to be updated : ");
    scanf("%d", &data);
    printf("\n Enter new data : ");
    scanf("%d", &data1);
    temp2 = h;
    if (temp2 == NULL)
    {
        printf("\n Error : List empty no node to update");
        return;
    }
    while (temp2 != NULL)
    {
        if (temp2->n == data)
        {
            temp2->n = data1;
            traversebeg();
            return;
        }
        else
            temp2 = temp2->next;
    }
     printf("\n Error : %d not found in list to update", data);
}
 void sort()
{
    int i, j, x;
    temp2 = h;
    temp4 = h;
    if (temp2 == NULL)
    {
        printf("\n List empty to sort");
        return;
    }
    for (temp2 = h; temp2 != NULL; temp2 = temp2->next)
    {
        for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
        {
            if (temp2->n > temp4->n)
            {
                x = temp2->n;
                temp2->n = temp4->n;
                temp4->n = x;
            }
        }
    }
    traversebeg();
}

Output

1 - Insert at beginning
2 - Insert at end
3 - Insert at position i
4 - Delete at i
5 - Display from beginning
6 - Display from end
7 - Search for element
8 - Sort the list
9 - Update an element
10 - Exit
Enter choice : 1
Enter value to node : 10
Enter choice : 2
Enter value to node : 50
Enter choice : 4
Enter position to be deleted : 1
Node deleted
Enter choice : 1
Enter value to node : 34
Enter choice : 3
Enter position to be inserted : 2
Enter value to node : 13
Enter choice : 4
Enter position to be deleted : 4
Error : Position out of range to delete
Enter choice : 1
Enter value to node : 15
Enter choice : 1
Enter value to node : 67
Enter choice : 3
Enter position to be inserted : 2

Enter value to node : 34
Enter choice : 4
Enter position to be deleted : 3
Node deleted
Enter choice : 7
Enter value to search : 15
Error : 15 not found in list
Enter choice : 8
Linked list elements from begining :  13  34  34  50  67
Enter choice : 9
Enter node data to be updated : 45
Enter new data : 89
Error : 45 not found in list to update
Enter choice : 9
Enter node data to be updated : 50
Enter new data : 90
Enter choice : 5
Linked list elements from begining :  13  34  34  90  67
Enter choice : 6
Reverse order of linked list is :  67  90  34  34  13
Enter choice : 7
Enter value to search : 90
Data found in 4 position
Enter choice : 8
Linked list elements from begining :  13  34  34  67  90
Enter choice : 7
Enter value to search : 90
Data found in 5 position
Enter choice : 9
Enter node data to be updated : 34
Enter new data : 56
Linked list elements from begining :  13  56  34  67  90
Enter choice : 10

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

No comments:

Post a Comment