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).

Upcoming Student career counselling implementation

I've been working on student career counselling and will be coming with its implementation in few more days...



Wednesday, August 24, 2011

Find a largest and smallest element in array in Java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package largestandsmallest;
import java.io.*;
/**
 *
 * @author Gurpreet
 */
public class Largestandsmallest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       DataInputStream in=new DataInputStream(System.in);
       int array[]=new int[5];
       int i,j;
       int temp;
       int choice;
       try
       {
           for(i=0;i<5;i++)
           {
                System.out.printf("a[%d]=",i);
                array[i]=Integer.parseInt(in.readLine());
               
           }
          
           j=1;
        while(j==1)
        {
           System.out.printf("\n Enter 1 for checking largest\n Enter 2 for checking smallest\n Enter Your Choice:");
           choice=Integer.parseInt(in.readLine());
           switch(choice)
           {
               case 1:
          
           temp=array[0];
           for(i=0;i<5;i++)
           {
              if(temp<array[i])
              {
                  temp=array[i];
              }
           }
           System.out.printf("\n Largest Element is=%d",temp);
                   break;
               case 2:
                  
           temp=array[0];
           for(i=0;i<5;i++)
           {
              if(temp>array[i])
              {
                  temp=array[i];
              }
           }
           System.out.printf("\n Smallest Element is=%d",temp);
                   break;
               default:
                   System.out.printf("\n\t\t***** INVALID CHOICE *****");
           }
        }
           }   catch(IOException e) {}
           
   
   
    }
}

Wednesday, May 4, 2011

PROGRAM TO REVERSE A NUMBER IN PROLOG


gurpreet:- write('Enter number'),nl,read(X),Z is X mod 10, X1 is X/10, R is (Z*10)+(X1),write(R),!.