Saturday, 13 August 2011

Stack using Doubly Linked List

#include
#include
struct node
{
int data;
struct node *pre;
struct node *next;
};
struct node *top,*bottom;

void main()
{
int num,option;
char choice='y';
int pop();

void push(int num);
void display();

clrscr();

while(choice=='y')
{
printf("\n\n1.PUSH");
printf("\n\n2.POP");
printf("\n\n3.DISPLAY\n\n");

printf("\n\nEnter Your choice:");
scanf("%d",&option);

switch(option)
{
case 1:
printf("\n\nEnter The number:");
scanf("%d",&num);
push(num);

printf("\n\nDo you want to continue:");
break;

case 2:
num=pop();
printf("\n\nPOP data=%d",num);
printf("\n\nDo you want to continue:");
break;

case 3:

display();
printf("\n\nDo you want to continue:");
break;

default :
printf("\n\nInvalid option");
printf("\n\nDo you want to continue:");
break;

}

choice=getch();
}
getch();
}//End of Main function


/* function to push data into stack */

void push(int num)
{

if(top==NULL && bottom==NULL)
{
top=bottom=(struct node *)malloc(sizeof(struct node));
top->data=num;
top->next=NULL;
top->pre=NULL;
}

else
{
top->next=(struct node *)malloc(sizeof(struct node));
top->next->data=num;
top->next->next=NULL;
top->next->pre=top;
top=top->next;
}

}

/*fuction to display stack data */

void display()
{
struct node *q=bottom;

if(top==NULL && bottom==NULL)
{
printf("\n\nStack is empty");
return;
}
else
{
printf("\n\n");
while(q!=NULL)
{
printf("%d, ",q->data);
q=q->next;
}
}
}


/*function to pop data from stack */

int pop()
{
int result;

if(top==NULL && bottom==NULL)
{
printf("\n\nStack is Empty\n");
return -32768;
}
else
{
result=top->data;
top=top->pre;

if(top!=NULL)
{
free(top->next);
top->next=NULL;
}

if(top==NULL)
{
free(bottom);
bottom=NULL;
}
return(result);
}
}


Queue using Doubly Linked List

#include
#include

struct node
{
int data;
struct node *next;
struct node *pre;
};

struct node *front,*rear;

void main()
{
int num,option;
char choice='y';
void add(int num);
int r();
void display();
clrscr();

front=rear=NULL;

while(choice=='y')
{
printf("\n\n1.ADD");
printf("\n\n2.REMOVE");
printf("\n\n3.DISPLAY");

printf("\n\nEnter the option:");
scanf("%d",&option);

switch(option)
{
case 1:
printf("\n\nEnter The no.:");
scanf("%d",&num);
add(num);

printf("\n\nDo you want to continue:");
break;

case 2:
num=r();
printf("\n\nRemove item=%d",num);
printf("\n\nDo you want to continue:");
break;

case 3:
display();
printf("\n\nDo you want to continue:");
break;

default :
printf("\n\nInvalid option");
printf("\n\nDo you want to continue:");

}

choice=getch();
}
getch();
}

void add(int num)
{
if(front==NULL &&rear==NULL)
{
front=rear=(struct node *)malloc(sizeof(struct node));
rear->data=num;
rear->next=NULL;
rear->pre=NULL;
}

else
{
rear->next=(struct node *)malloc(sizeof(struct node));
rear->next->data=num;
rear->next->next=NULL;
rear->next->pre=rear;
rear=rear->next;
}
}


int r()
{
int result;
struct node *q=front;

if(front==NULL &&rear==NULL)
{
printf("\n\nQueue is empty");
return -32768;
}

else
{
result=front->data;
front=front->next;
front->pre=NULL;
q->next=NULL;
free(q);

if(front==NULL)
{
rear=NULL;
}

return(result);
}
}

void display()
{
struct node *q=front;

if(front==NULL &&rear==NULL)
{
printf("\n\nQueue is empty");

}
else
{
while(q!=NULL)
{
printf("%d,",q->data);
q=q->next;
}
}
}


Linked List Operation

#include
#include
#include
struct emp
{
int empno;
struct emp *next;

}*first1,*first2,*first3,*first4,*old1,*old2,*old3,*old4,*newrec1,*newrec2,*newrec3,*newrec4;
void CreateList1();
void CreateList2();
void CreateList3();
void CreateList4();

void Display1();
void Display2();
void Display3();
void Display4();

void Concat();
void Merge();
void Union();
void Intersection();
void del();
void main()
{
clrscr();
int n,i=0;
printf("\n How many times you want to add in list 1 :->");
scanf("%d",&n);
while(i");
scanf("%d",&n);
i=0;
while(iempno);
newrec1->next=NULL;
if(first1==NULL)
first1=newrec1;
else
old1->next=newrec1;
old1=newrec1;

}

void CreateList2()
{

newrec2=(struct emp *)malloc(sizeof(struct emp));
printf("\n Dynamic memory is allocated successfully !");
printf("\n Enter the Emp Number. ");
scanf("%d",&newrec2->empno);
newrec2->next=NULL;
if(first2==NULL)
first2=newrec2;
else
old2->next=newrec2;
old2=newrec2;

}

void CreateList3()
{

newrec3=(struct emp *)malloc(sizeof(struct emp));
newrec3->next=NULL;
if(first3==NULL)
first3=newrec3;
else
old3->next=newrec3;
//old3=newrec3;

}
void CreateList4()
{

newrec4=(struct emp *)malloc(sizeof(struct emp));
newrec4->next=NULL;
if(first4==NULL)
first4=newrec4;
else
old4->next=newrec4;
//old3=newrec3;

}

void Concat()
{
newrec1=first1;
while(newrec1!=NULL)
{
CreateList3();
newrec3->empno=newrec1->empno;
old3=newrec3;

newrec1=newrec1->next;
}

newrec2=first2;
while(newrec2!=NULL)
{
CreateList3();
newrec3->empno=newrec2->empno;
old3=newrec3;

newrec2=newrec2->next;
}
}

void Merge()
{
newrec1=first1;
newrec2=first2;
//newrec3->next=NULL;

while(newrec1!=NULL || newrec2!=NULL)
{
CreateList4();

newrec4->empno=newrec1->empno;
newrec1=newrec1->next;
old4=newrec4;

CreateList4();

newrec4->empno=newrec2->empno;
newrec2=newrec2->next;
old4=newrec4;

}
Display4();

}

void Union()
{
newrec1=first1;
newrec2=first2;
int temp;
int c=0;
del();

while(newrec1!=NULL)
{
CreateList4();
old4=newrec4;
newrec4->empno=newrec1->empno;
newrec1=newrec1->next;
}

newrec4=first4;

while(newrec2!=NULL)
{
temp=newrec2->empno;
while(newrec4!=NULL)
{
if(temp!=newrec4->empno)
c=1;
else
{
c=0;
break;
}
newrec4=newrec4->next;
}
if(c==1)
{
CreateList4();
old4=newrec4;
newrec4->empno=newrec2->empno;
}
newrec2=newrec2->next;
newrec4=first4;
}


Display4();
}

void Intersection()
{
newrec1=first1;
newrec2=first2;
int temp;
int c=0;
del();


while(newrec2!=NULL)
{
temp=newrec2->empno;
while(newrec1!=NULL)
{
if(temp==newrec1->empno)
{
c=1;
break;
}
else if(temp!=newrec1->empno)
{
c=0;

}

newrec1=newrec1->next;
}
if(c==1)
{
CreateList4();
old4=newrec4;
newrec4->empno=newrec2->empno;
c=0;
}
newrec2=newrec2->next;
newrec1=first1;
}


Display4();
}



void del()
{
newrec4=first4;
while(newrec4!=NULL)
{
free(newrec4);
newrec4=newrec4->next;
}
}

void Display1()
{
newrec1=first1;
while(newrec1!=NULL)
{
printf("\n Employee Number. %d",newrec1->empno);
newrec1=newrec1->next;
}
newrec1->next=NULL;
}

void Display2()
{
newrec2=first2;
while(newrec2!=NULL)
{
printf("\n Employee Number. %d",newrec2->empno);
newrec2=newrec2->next;
}
newrec2->next=NULL;
}
void Display3()
{
newrec3=first3;
while(newrec3!=NULL)
{
printf("\n Employee Number. %d",newrec3->empno);
newrec3=newrec3->next;
}
newrec3->next=NULL;
}
void Display4()
{
newrec4=first4;
while(newrec4!=NULL)
{
printf("\n Employee Number. %d",newrec4->empno);
newrec4=newrec4->next;
}
newrec4->next=NULL;
}





Hashing

#include
#include
#define MAX 10
#define HTMAX 20
int h[HTMAX]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},a[MAX];
void DoubleHT(int);
void Search(int);
void main()
{
int n,i,x ;
clrscr();
printf("\n\t\t---:DOUBLE HASHING TECHNIQUE(LINEAR PROBING):---\n");
printf("\n\t Enter the no. of elements of array you want to enter :->");
scanf("%d",&n);
for(i=0;i",i+1);
scanf("%d",&a[i]);
}
printf("\n Original Array\n");
for(i=0;i");
scanf("%d",&x);
Search(x);
getch();
}
void DoubleHT(int n)
{
int addr,i,j,k,flag=1;
for(i=0;i {

addr=(a[i]%10)%20;
if(h[addr]==-1)
h[addr]=a[i];
else
{
// Loop upto the HashTable Array Limit
for(j=1;j {
addr=(a[i]%10+j*(a[i]%10))%20;
if(h[addr]==-1)
{
h[addr]=a[i];
break;
}
else if(h[addr]!=-1) // Code for Liner Probing starts
{
addr++;
while(flag==1)
{
if(h[addr]!=-1)
addr++;
else
flag=0;
}
h[addr]=a[i];
flag=1;
break; // Ends here !
}

}
}
}
printf("\n Printing Hashtable \n");
for(i=0;i printf(" %d",h[i]);

}
void Search(int x)
{
int addr=0;
addr=x%10;
while(x!=h[addr] && addr addr++;
if(x==h[addr])
printf("\n\n Search is succsessfull!!!!!");

else
printf("\n\n Requested Element is not found !!!");

}





Doubly Linked List

#include
#include
#include


typedef struct student node;

void Create();
void Display(node *);
void Count();
void Insert();
void Del();
void Search();
void Copy();
void Reverse();

/*Structure Body*/
struct student
{
int rollno;
struct student *prev,*next;
}*first,*first1,*newrec,*newrec1,*last;



void main()
{
int choice=0;
clrscr();
while(choice!=9|| choice>9)
{
printf("\n===============MENU=================");
printf("\n| 1. Create Doubly Linked List |");
printf("\n| 2. Display |");
printf("\n| 3. Count |");
printf("\n| 4. Insert |");
printf("\n| 5. Delete |");
printf("\n| 6. Search |");
printf("\n| 7. Copy |");
printf("\n| 8. Reverse |");
printf("\n|9. Exit |");
printf("\n====================================");
printf("\n Enter the Choice :->");
scanf("%d",&choice);
switch(choice)
{
case 1: Create();
break;
case 2: Display(first);
break;
case 3: Count();
break;
case 4: Insert();
break;
case 5: Del();
break;
case 6: Search();
break;
case 7: Copy();
break;
case 8: Reverse();
break;
case 9: exit(1);
break;
default:
printf("\n invalid choice entery !");

}
}
getch();
}
void Create()
{
node *ptr;
newrec=(node *)malloc(sizeof(node));
printf("\n Enter the Roll no : ");
scanf("%d",&newrec->rollno);
newrec->next=NULL;
newrec->prev=NULL;
if(first==NULL)
first=newrec;
else
{
ptr=first;
while(ptr->next!=NULL)
ptr=ptr->next;

ptr->next=newrec;
newrec->prev=ptr;
}

last=newrec;
}

void Display(node *first)
{
node *ptr;
int srno=0;
ptr=first;
printf("\n------------------------");
printf("\nSr. No. Record Value ");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->next;
}
printf("\n------------------------");

}
void Count()
{
int c=0;
newrec=first;
while(newrec!=NULL)
{
newrec=newrec->next;
c++;
}
printf("\n Total Number of Records are : %d ",c);
}

void Insert()
{
int i=1,choice=0;
node *ptr;
printf("\n1.Insert Before first \n2.Insert in the last \n3.Insert in between \nEnter Choice ");
scanf("%d",&choice);

newrec=(node *)malloc(sizeof(node));
printf("\n Enter the Roll no : ");
scanf("%d",&newrec->rollno);

ptr=first;

if(choice==1)
{
if(ptr==first || ptr==NULL)
{
newrec->next=first;
first->prev=newrec;
newrec->prev=NULL;
first=newrec;
}

}
else if(choice==2)
{
while(ptr->next!=NULL)
ptr=ptr->next;

newrec->prev=ptr;
ptr->next=newrec;
newrec->next=NULL;

}
else if(choice==3)
{

int p;
printf("\n Enter position at which you want to insert record :");
scanf("%d",&p);

//In insertion we need to goto one position before the place where we
//want to insert new node thats why p-1

while(i<(p-1)) { ptr=ptr->next;
i++;
}

newrec->next=ptr->next;
newrec->prev=ptr;
ptr->next->prev=newrec;
ptr->next=newrec;

}
else
printf("\n You have entered invalid choice ");
}

void Del()
{
int choice;
node *ptr,*prevptr;
printf("\n1.Delete first \n2.Delete last \n3.Delete node from between :");
scanf("%d",&choice);

ptr=first;

if(first==NULL)
{
printf("\n There is no node to delete ");
return;
}
if(choice==1)
{
first=first->next;
first->prev=NULL;
free(ptr);
}
else if(choice==2)
{
ptr=first;
while(ptr->next!=NULL)
{
prevptr=ptr;
ptr=ptr->next;
}

prevptr->next=NULL;
free(ptr);
}
else if(choice==3)
{
int p,i=1;
printf("\n Enter position of the node that you want to delete :");
scanf("%d",&p);
//In delete we need to go to the node that we want to delete
//ptr=ptr->next by below refer to the node that to be deleted
while(inext;
i++;
}
prevptr->next=ptr->next;
ptr->next->prev=prevptr;
free(ptr);
}
}

void Search()
{
int data;
node *ptr;
printf("\n Enter the Student's Roll no that you want to search : ");
scanf("%d",&data);

ptr=first;

while(ptr!=NULL)
{
if(data==ptr->rollno)
{
printf("\n Record found !");
break;
}
else
ptr=ptr->next;
}
if(ptr==NULL)
printf("\n Requested Record not found !");


}

void Copy()
{
node *ptr,*ptrcopy;
ptr=first;
while(ptr!=NULL)
{
newrec1=(node *)malloc(sizeof(node));

newrec1->next=NULL;
newrec1->prev=NULL;
newrec1->rollno=ptr->rollno;

if(first1==NULL)
first1=newrec1;
else
{
ptrcopy=first1;
while(ptrcopy->next!=NULL)
ptrcopy=ptrcopy->next;

ptrcopy->next=newrec1;
newrec1->prev=ptrcopy;
}

ptr=ptr->next;
}

printf("\n Original List ");
Display(first);
printf("\n Copied List ");
Display(first1);
}
void Reverse()
{
int srno=0;
node *ptr;
ptr=last;
printf("\n----------Reverse List------------");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->prev;
}
printf("\n----------------------------------");
}

Singly Linked List

#include
#include
#include

typedef struct student node;
void Create();
void Create1();
void Create2(int);

void Display(node *,int);
void Concate();
void Merge();
void Union();
void Intersection();
void FreeList(node *);


struct student
{
int rollno;
struct student *next;
}*first,*first1,*first2,*newrec,*newrec1,*newrec2,*last,*last1,*last2;


void main()
{
int choice=0,i,n;
clrscr();
while(choice!=8)
{
printf("\n===============MENU=================");
printf("\n| 1. Create 1st Singly Linked List |");
printf("\n| 2. Create 2nd Singly Linked List |");
printf("\n| 3. Display |");
printf("\n| 4. Concate |");
printf("\n| 5. Merge |");
printf("\n| 6. Union |");
printf("\n| 7. Intersection |");
printf("\n| 8. Exit |");
printf("\n====================================");
printf("\n Enter the Choice :->");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("\n ENTER NUMBER OF NODES IN SINGLY LINKED LIST 1 :-");
scanf("%d",&n);
for(i=0;irollno);
newrec->next=NULL;
if(first==NULL)
first=newrec;
else
last->next=newrec;

last=newrec;

}
void Create1()
{
node *ptr;
newrec1=(node *)malloc(sizeof(node));
printf("\n Enter the Roll no : ");
scanf("%d",&newrec1->rollno);
newrec1->next=NULL;
if(first1==NULL)
first1=newrec1;
else
last1->next=newrec1;

last1=newrec1;

}


void Create2(int data)
{
node *ptr;
newrec2=(node *)malloc(sizeof(node));
newrec2->rollno=data;
newrec2->next=NULL;
if(first2==NULL)
first2=newrec2;
else
last2->next=newrec2;

last2=newrec2;

}

void Display(node *first,int n)
{
node *ptr;
int srno=0;
ptr=first;
if(ptr==NULL)
{
printf("\n Linked List no. %d is empty ",n);
return;
}
printf("\n------------------------");
printf("\nSr. No. Record Value ");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->next;
}
printf("\n------------------------");

}

void Concate()
{
node *ptr;
ptr=first;

FreeList(first2);

while(ptr!=NULL)
{
Create2(ptr->rollno);
ptr=ptr->next;
}
ptr=first1;
while(ptr!=NULL)
{
Create2(ptr->rollno);
ptr=ptr->next;
}

}
void Merge()
{
node *ptr,*ptr1;
ptr=first;
ptr1=first1;

FreeList(first2);

while(ptr!=NULL && ptr1!=NULL)
{
Create2(ptr->rollno);
Create2(ptr1->rollno);
ptr=ptr->next;
ptr1=ptr1->next;
}

}

void Union()
{
int value,flag;
node *ptr,*ptr1;

FreeList(first2);

ptr=first;
ptr1=first1;

while(ptr!=NULL)
{
Create2(ptr->rollno);
ptr=ptr->next;
}
ptr=first;
while(ptr1!=NULL)
{
ptr=first;
while(ptr!=NULL)
{
if(ptr->rollno!=ptr1->rollno)
{
flag=1;
value=ptr1->rollno;
}
else
{
flag=0;
break;
}
ptr=ptr->next;
}
if(flag==1)
Create2(value);
ptr1=ptr1->next;
}
}

void Intersection()
{
node *ptr,*ptr1;
ptr=first;
ptr1=first1;
FreeList(first2);

while(ptr!=NULL)
{
ptr1=first1;
while(ptr1!=NULL)
{
if(ptr->rollno==ptr1->rollno)
Create2(ptr->rollno);
ptr1=ptr1->next;
}
ptr=ptr->next;
}
}

//It will take first node of any link list

void FreeList(node *first)
{
node *ptr,*temp;
ptr=first;
while(ptr!=NULL)
{
temp=ptr;
ptr=ptr->next;
free(temp);
}
first=NULL;
}

Stack Implementation

#include
#include
#include
typedef struct student node;

void Push();
void Pop();
void Display();

struct student
{
int rollno;
node *next;
}*top,*newrec;

void main()
{
clrscr();
int choice=0,n,i;
while(choice!=4)
{
printf("\n==============Menu===============");
printf("\n| 1. Insert Item into the Stack |");
printf("\n| 2. Delete from the Stack |");
printf("\n| 3. Display the Stack Items |");
printf("\n| 4. Exit the Application |");
printf("\n=================================");

printf("\n Enter the Choice :->");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n HOW MANY ITEMS YOU WANT TO PUSH INTO THE STACK :->");
scanf("%d",&n);
for(i=0;i");
scanf("%d",&newrec->rollno);
if(top==NULL)
newrec->next=NULL;
else
newrec->next=top;
top=newrec;
}

void Pop()
{
node *ptr;
if(top==NULL)
printf("\n Stack is underflowing !!!");
else
{
ptr=top;
top=ptr->next; //Imagine linked list from opposite direction and top as head.
free(ptr);

}
}

void Display()
{
node *ptr;
int srno=0;
ptr=top;
if(ptr==NULL)
{
printf("\n Stack is Empty");
return;
}
printf("\n------------------------");
printf("\nSr. No. Record Value ");
while(ptr!=NULL)
{
srno++;
printf("\n %d %d ",srno,ptr->rollno);
ptr=ptr->next;
}
printf("\n------------------------");
}