Wednesday, September 14, 2016

Code to Sort a singly link list in C

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
int sData;
struct node *next;

};
struct node *start=NULL;
struct node *temp=NULL;

void insert(int lValue)
{
struct node *temp1;
temp1=malloc(sizeof(struct node *));

temp=start;
if(start == NULL )
{
start=temp1;
temp1->sData=lValue;
temp1->next=NULL;
}
else
{
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=temp1;
temp1->sData=lValue;
temp1->next=NULL;
}
}
void display()
{
struct node *temp=start;
if( NULL == start)
{
printf("Link list is empty\n");
}
else
{
while(temp->next != NULL )
{
printf(" Element is  [%d]\n",temp->sData);
temp=temp->next;
}
printf("Last Element is  [%d]\n",temp->sData);
}
}
void sort()
{
struct node *temp,*temp1;
if(NULL == start)
{
printf("Cant Sort: Link List is empty\n");
}
else
{
temp1=start;
while(temp1->next !=NULL)
{
temp=start;
while(temp->next !=NULL)
{
if(temp->sData > temp->next->sData)
{
int temp1=temp->sData;
temp->sData=temp->next->sData;
temp->next->sData=temp1;
}
temp=temp->next;
}
temp1=temp1->next;
}
}
}

int main()
{
int choice;
int lData;
while(1)
{
printf("1. Insert into Linked list\n 2. Display Link List\n 3. Sort Linked List\n 4. Exit\n Enter Your Choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Element:\t");
scanf("%d",&lData);
insert(lData);
break;
case 2:
display();
break;
case 3:
sort();
break;
case 4:
exit(1);
break;
default:
break;
}
}
return 0;
}

Monday, September 12, 2016

Code to Reverse A Singly Link List in C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;
}
}
void display()
{
struct node *temp1=start;
if(temp == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
void reverse()
{
struct node *prev=NULL;
struct node *current=start ;
struct node *temp;
while(current != NULL)
{
temp=current->next;
current->next=prev;
prev=current;
current=temp;
}
start=prev;
}
int main()
{
int choice;
while(1)
{
printf(" 1. For Insert into link list\n 2. Display link list\n 3. Reverse a link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
display();
break;
case 3:
reverse();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Linked list Delete Middle Element Code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert()
{
int lData=0;
struct node *temp1;
printf("Please enter data:\t");
scanf("%d",&lData);
temp=malloc(sizeof(struct node *));
temp1=start;

if(temp1 == NULL )
{
start=temp;
temp->data=lData;
temp->next=NULL;
}
else
{
printf("here1\n");
while(temp1->next != NULL)
{
temp1=temp1->next;
}
temp1->next=temp;
temp->data=lData;
temp->next=NULL;
}
}
void display()
{
struct node *temp1=start;
if(temp == NULL )
printf("Linked list is empty\n");
else
{
while(temp1->next != NULL )
{
printf("Final Element is [%d]\n",temp1->data);
temp1=temp1->next;
}
printf("Final Element is [%d]\n",temp1->data);
}
}
int calculate_length()
{
struct node *temp1=start;
int count=0;
if(temp ==  NULL )
return 0;
else
{
while(temp1->next != NULL )
{
temp1=temp1->next;
count++;
}
count++;
}
return count;
}
void delete_middle()
{
struct node *temp1=start;
int count =1;
int length=0;
int middle=-1;
length=calculate_length();
if(length%2 == 0 )
{
middle=length/2;
}
else
{
middle=(length/2)+1;
}
while(1)
{
printf("count [%d] middle [%d]\n",count,middle);
if(count == middle - 1)
{
struct node *del=temp->next;
temp1->next=temp1->next->next;
free(del);
break;
}
else
temp1=temp1->next;
count++;
}
}
int main()
{
int choice;
while(1)
{
printf(" 1. For Insert into link list\n 2. For Delete Middle\n 3. Display link list\n 4. Exit\n\n Enter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete_middle();
printf("delete middle\n");
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}

Sunday, July 17, 2016

C Linked List Operations : Insert, Delete, Show, Size, Delete at Position, Insert at position

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int gDebug=0;

struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *temp=NULL;

void insert(int data)
{
struct node *temp1,*ptr;
temp1=malloc(sizeof(struct node *));
ptr=start;
if(NULL == start)
{
temp1->data=data;
temp1->next=NULL;
start=temp1;
}
else
{
while(ptr->next != NULL)
{
ptr=ptr->next;
}
ptr->next=temp1;
temp1->next=NULL;
temp1->data=data;
}

}
void display()
{
struct node *ptr;
ptr=start;
if(NULL == ptr )
{
printf("Linked list is empty\n");
}
else
{
while(ptr->next != NULL)
{
printf("Element is [%d]\n",ptr->data);
ptr=ptr->next;
}
printf("Element is [%d]\n",ptr->data);

}
}
void delete_at_beg()
{
struct node *ptr;
ptr=start;
if(NULL == ptr )
{
printf("Cannot delete: Linked list is empty\n");
}
else
{
free(start);
ptr=ptr->next;
start=ptr;
}
}
void delete_at_end()
{
struct node *ptr;
ptr=start;
if(NULL == ptr )
{
printf("Cannot delete at end : Linked list is empty\n");
}
else
{
if(ptr->next !=NULL )
{
printf("ptr->next !=NULL\n");
while(ptr->next->next !=NULL)
{
ptr=ptr->next;
}
free(ptr->next->next);
ptr->next=NULL;
}
else
{
printf("ptr->next ==NULL\n");
free(ptr->next);
start=NULL;
}
}

}
int showSize()
{
int lLinkedListLen=0;
struct node *temp;
temp=start;
if(NULL ==temp )
{
return 0;
}
else
{
while(temp->next != NULL )
{
temp=temp->next;
lLinkedListLen++;
}
lLinkedListLen++;
}
return lLinkedListLen;
}
void insert_at_beg()
{
int lData;
struct node *temp;
struct node *temp1;
temp1=malloc(sizeof(struct node *));
printf("Enter data:\t");
scanf("%d",&lData);
if(NULL == start )
{
temp1->data=lData;
temp1->next=NULL;
start=temp1;
}
else
{
temp1->data=lData;
temp1->next=start;
start=temp1;
}
}
void insert_at_pos(int lPos,int lData)
{
int lLinkedListLen=0;
int lCount=0;
struct node *temp1,*prev;
struct node *ptr;
lLinkedListLen=showSize();
if( 0 == lPos)
{
if(NULL == start )
{
temp1->data=lData;
temp1->next=NULL;
start=temp1;
}
else
{
temp1->data=lData;
temp1->next=start;
start=temp1;
}
}
else
{
temp1=malloc(sizeof(struct node *));
ptr=start;
while(lCount != (lPos))
{
prev=ptr;
ptr=ptr->next;
lCount++;
}
temp1->data=lData;
temp1->next=ptr;
prev->next=temp1;
}
}
int main()
{
int lChoice;
int lData;
int lLinkedListLength=0;
int lPos;
while(1)
{
printf("\n\nLinked List Operations\n 1.Insert\n 2.Display\n 3.Delete at beg\n 4.Delete at end\n 5.Show Size\n 6.Insert at beg\n 7.Insert at pos\n 8.Exit\n Enter your Choice:\t");
scanf("%d",&lChoice);
switch(lChoice)
{
case 1:
printf("Enter Data:\t");
scanf("%d",&lData);
insert(lData);
break;
case 2:
display();
break;
case 3:
delete_at_beg();
break;
case 4:
delete_at_end();
break;
case 5:
lLinkedListLength=showSize();
if(1 == gDebug)
printf("Length of Linked list is ::  [%d]\n",lLinkedListLength);
break;
case 6:
if(1 == gDebug)
printf("CALL INSERT AT BEG\n");
insert_at_beg();
break;
case 7:
printf("Enter the position at which you want to enter:\t");
scanf("%d",&lPos);
printf("Enter data:\t");
scanf("%d",&lData);
insert_at_pos(lPos,lData);
break;
case 8:
exit(0);
break;
default:
printf("Invalid Choice....Exiting !!!\n");
break;
}
}
return 0;
}

Monday, December 22, 2014

Cloud Computing and Data Mining IEEE Level Research Paper Writing for M.Tech


Subject: IEEE/SPRINGER Level Research Paper(s) For M.Tech -- FASTER DELIVERY
 

Hi Friends,

The purpose of this post is to communicate and help candidates across the globe who are doing research in their bachelor or master degree (B.Tech / M.Tech) courses in the field of Computer Science.I provide help in deciding the core area of research,Problem definition, Research paper writing (IEEE/Springer level).At the same time I provide help in exploring the tools like cloud sim, cloud analyst,NS2 etc which ongoing research candidates find extremely difficult to explore.

Feel free to contact and support is 24*7.

Regards,
Gurpreet Singh

Wednesday, August 7, 2013

VIRTUAL FUNCTION - c++


VIRTUAL FUNCTION :- 

Virtual Function is a function that is declared within a base class and redefined in the derived class. Virtual functions are declared by preceding the class declaration with a keyword "virtual". When a virtual function is declared C++ decides to execute a function based on the type of object pointed by the base pointer and not on the type of pointer.
Example:
#include <iostream.h>
#include<conio.h>
#include<stdio.h>
class BB
  {
    public:
       void  show() { cout  << " BASE BASE\n" ; }
       virtual void print() { cout << "base base\n"; }
  };
class DD : public BB
  {
    public:
      void show() { cout << "DERIVED DERIVED\n"; }
      void print() { cout << "derived derived\n"; }
  };
int main()
  {
    
     BB B;
     DD D;
     BB *ptr;
    
     cout << "ptr points to base class\n" ;
     ptr =  &B;
     ptr->show();
     ptr->print();
  
     cout << "ptr points derived class\n";
     ptr = &D;
     ptr->show();
     ptr->print();
     getch();
     return 0;
  }

OUTPUT:
ptr points to base class
BASE BASE
base base
ptr points derived class
BASE BASE
derived derived

Sunday, July 28, 2013

Difference Between Pointers and References


               REFERENCES                                         VS                              POINTER
        

* We can not change the location to which the reference belongs that is why we have to initialize the references when we do declare them unlike we can change the locations to which the  pointer belongs.

* We have  to dereference the pointers explicitly but the references are pre-dereferenced.

* we cant use ++ on reference variables but we can use ++operator on pointers(as they then after points to the next memory of its type).