Showing posts with label Data Structures. Show all posts
Showing posts with label Data Structures. Show all posts

Saturday, 2 July 2016

Compare two linked lists

int CompareLists(Node *headA, Node *headB)
{
    while((headA != NULL) && (headB != NULL)) {
        if(headA->data != headB->data) {
            break;
        }
        headA = headA->next;
        headB = headB->next;
    }
    if((headA == NULL) && (headB == NULL)) {
        return 1;
    } else {
        return 0;
    }
}

Sunday, 5 July 2015

Singly Linked List Operation


                                  SINGLY LINKED LIST OPERATION
#include<stdio.h>
#include<conio.h>


// Syntax for Creating Node
struct node
{
int data;                           // Type of data store in nodes
struct node *link;           // Link pointer for joining nodes
};
struct node *head=NULL;    // head pointer for pointing the first node of linked list
int item;                                // Integer temporary variable for inserting the data in linked list

void main()
{
 int choice;
 clrscr();
 printf("\nEnter 1:ADDEND \n 2:ADDBEG \n 3:ADDAFTER \n 4:DELBEG \n 5:DELEND \n 6:DELAFTER \n 7:DISPLAY \n 8:COUNT \n 9:EXIT");
  while(1)
  {
   printf("\nEnter Choice ");
   scanf("%d",&choice);
    switch(choice)
    {
    case 1:
    AddEnd();
    break;
    case 2:
    AddBeg();
    break;
    case 3:
    AddAfter();
    break;
    case 4:
    DelBeg();
    break;
    case 5:
    DelEnd();
    break;
    case 6:
    DelAfter();
    break;
    case 7:
    Display();
    break;
    case 8:
    Count();
    break;
    case 9:
    exit();
   }
 }
}

// Function for adding node in the last of the Linked List

int AddEnd()
{
 struct node *temp,*q;
  if(head==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=NULL;
   head=temp;
  }
  else
  {
   q=head;
    while(q->link!=NULL)
    {
     q=q->link;
    }
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=NULL;
   q->link=temp;
  }
 return 0;
}

// Function for adding node in the beginning of the Linked List

AddBeg()
{
 struct node *temp,*q;
  if(head==NULL)
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   head=temp;
  }
  else
  {
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   temp->link=head;
   head=temp;
  }
 return 0;
}

// Function for adding node after some nodes(in the middle) of the Linked List

AddAfter()
{
 int pos,n;
 printf("\nEnter position to Insert new node");
 scanf("%d",&pos);
 n=Count();
  if(pos>n)
   printf("\nNo Available node are less");
  else if(pos==n)
   AddEnd();
  else if(pos==0)
   AddBeg();
  else if(pos>=1 && pos<n)
  {
   int i;
   struct node *q,*temp,*r;
   temp=(struct node *)malloc(sizeof(struct node));
   printf("\nEnter Item ");
   scanf("%d",&item);
   temp->data=item;
   q=head;
    for(i=1;i<pos;i++)
    {
     q=q->link;
      //r=q->link->link;
    }
   temp->link=q->link;
   q->link=temp;
  }
 return 0;
}

// Function for deleting node from beginning of the Linked List

DelBeg()
{
 struct node *q;
  if(head==NULL)
   printf("\nNo Node to Delete");
  else
  {
   printf("\n%d Data has Deleted",head->data);
   q=head;
   q=q->link;
   free(head);
   head=q;
  }
 return 0;
}

// Function for deleting node from last of the Linked List

DelEnd()
{
 struct node *q;
 q=head;
  if(head==NULL)
   printf("\nNo node to Delete");
  else
   {
    if(head->link==NULL)
     {
     printf("\n%d Data has Deleted",head->data);
     free(head);
     head=NULL;
     }
    else
    {
     while(q->link->link!=NULL)
      {
       q=q->link;
      }
     printf("\n%d Data has Deleted",q->link->data);
     free(q->link);
     q->link=NULL;
    }
  }
 return 0;
}

// Function for deleting node after some nodes(in the middle) Linked List

DelAfter()
{
 int pos,n,i;
 struct node *q,*r;
 printf("\nEnter position which is to be Delete ");
 scanf("%d",&pos);
 n=Count();
  if(head==NULL)
   printf("\nNo Available Node to Delete");
  else if(head->link==NULL)
  {
   printf("\n%d Data is Deleted",head->data);
   head=NULL;
   free(head);
  }
 if(pos==0)
 {
  printf("\nDELBEG Function Calling");
  DelBeg();
 }
 else if(pos==n)
 {
  printf("\nDELEND Function Calling");
  DelEnd();
 }
 else if(pos>=1 && pos<n)
 {
  q=head;
  r=head->link;
  for(i=1;i<pos;i++)
   {
    q=q->link;
    r=r->link;
   }
  printf("\n%d Data is Deleted",r->data);
  q->link=r->link;
  free(r);
 }
 return 0;
}

// Function for Counting nodes in Linked List

int Count()
{
 int i,counter=1;
 struct node *q;
 q=head;
  if(head==NULL)
   printf("\nTotal No. of Node is : %d",0);
  else
   {
    while(q->link!=NULL)
     {
      counter++;
      q=q->link;
     }
    printf("\nTotal no. of Node is : %d",counter);
    return(counter);
   }
 return 0;
}

// Function for displaying the Linked List

Display()
{
 struct node *q;
  if(head==NULL)
   printf("\nNo Linked List Available to Show");
  else
   {
    q=head;
     while(q!=NULL)
      {
       printf(" %d",q->data);
       q=q->link;
      }
   }
 return 0;
}

Saturday, 22 November 2014

Difference between Structure and Union

vishalgarg837@gmail.com

Difference between Structure and Union

 Basically I know that struct uses all the memory of its member and union uses the largest members memory space.
With a union, you're only supposed to use one of the elements, because they're all stored at the same spot. This makes it useful when you want to store something that could be one of several types. A struct, on the other hand, has a separate memory location for each of its elements and they all can be used at once.

e.g.

union test {
  int a;   // can not use both a and b at once
  char b;
} test;
Here union will need 4 bytes memory.2 bytes for int & 2 bytes for char. 

struct fun {
  int a;   // can use both a and b simultaneously
  char b;
} fun;
Here structure will need 3 bytes memory.2 bytes for int & 1 byte for char. 

union test x;
x.a = 3; // OK
x.b = 'c'; // NO this affects the value of x.a

struct fun y;
y.a = 3; // OK
y.b = 'c'; // OK