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

WAP of stack using structure and that needs to perform push & pop operation

/*WAP of stack using structure and that needs to perform push & pop operation*/
#include<iostream.h>
#include<conio.h>
//using namespace std;
void stack();
void push(int);
int pop();
struct stack
{
//private:
int arr[5],top;
//public:
}s;
void stack()
{
s.top=-1;
}
void push(int v)
{
if(s.top==4)
cout<<"Stack is full"<<endl;
else
{
s.arr[++s.top]=v;
cout<<"Data pushed successfully"<<endl;
}
}
int pop()
{
if(s.top==-1)
{
cout<<"Stack is empty"<<endl;
return NULL;
}
else
return s.arr[s.top--];
}
main()
{
///stack s;
int ch,k;
stack();
cout<<"1 for push\n2for pop";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Variable";
cin>>k;
push(k);
break;
case 2:
cout<<pop();
break;
}
getch();
}

WAP of stack that perform push,pop and peep operation using functions

/*stack-push,pop and peep*/
#include<stdio.h>
#include<conio.h>
int top=-1;
const int size=10;
int a[10];
void push();
void pop();
void peep();
void display();
void main()
{
int n;
char ch='y';
clrscr();
while(ch=='y')
{
printf("\n1.push\n2.pop\n3.peep\n4.display\nEnter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peep();
break;
}
case 4:
{
display();
break;
}
default:
{
printf("Invalid choice....");
break;
}
}
printf("\nDo you want to continue?");
fflush(stdin);
scanf("%c",&ch);
}
getch();
}
void push()
{
int item;
if(top>=size)
{
printf("\nStack overflow.....\n");
}
else
{
top+=1;
printf("Enter element:");
scanf("%d",&item);
a[top]=item;

}
}
void pop()
{
int item;
if(top<0)
{
printf("\nStack underflow....\n");
}
else
{
item=a[top];
top-=1;
printf("\nValue deleted is:%d",item);
}

}
void display()
{
int i;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
for(i=0;i<=top;i++)
{
printf("\n%d",a[i]);
}
}
}
void peep()
{
int item;
if(top<0)
{
printf("\nStack underflow......");
}
else
{
item=a[top];
printf("the top most element is %d",item);
}
}


WAP of LINKED lIST insert front , print , delet front in single linked list

/*WAP of LINKED lIST insert front , print , delet front in single linked list*/
#include<stdio.h>
#include<conio.h>

void insert_front_sl();
void print_sl();
void insert_end_sl();

struct node
{
	int data;
	struct node *link;
};
struct node *header;

void main()
{
	clrscr();
	
	header = (struct node *) malloc(sizeof(struct node));
	header->data = NULL;
	header->link = NULL;
	
	insert_front_sl();
	insert_front_sl();
	print_sl();
	insert_end_sl();
	insert_end_sl();
	print_sl();
	
	getch();
}

void insert_front_sl()
{
	struct node *temp,*ptr;
	int X;
	temp = (struct node *) malloc(sizeof(struct node));
	
	if(temp == NULL)
	{
		printf("Memory Insufficient");
	}
	else
	{
		ptr = header->link;
		
		printf("\nEnter the value to insert at front: ");
		scanf("%d", &X);
		temp->data = X;
		temp->link = ptr;
		header->link = temp;
	}
}

void print_sl()
{
	struct node *ptr;
	ptr = header;
	
	while(ptr->link != NULL)
	{
		ptr = ptr->link;
		printf("%d ",ptr->data);
	}
}

void insert_end_sl()
{
	struct node *temp,*ptr;
	int X;
	temp = (struct node *) malloc(sizeof(struct node));
	
	if(temp == NULL)
	{
		printf("Memory Insufficient");
	}
	else
	{
		ptr = header;
		while(ptr->link != NULL)
		{
			ptr = ptr->link;
		}
		
		printf("\nEnter the value to insert at end: ");
		scanf("%d", &X);
		temp->data = X;
		temp->link = NULL;
		ptr->link = temp;
	}
}


WAP of Queue


#include<conio.h>
#include<stdio.h>
void enqueue();
int dequeue();
int e[5],n=5,rear=-1,front=-1,i,ch='y',item;
void main()
{
 int c;
 clrscr();

      while(ch=='y')
      {

 printf("\n\n1.Enqueue");
 printf("\n\n2.Dequeue");
 printf("\n\n3.Exit");
 printf("\n\nEnter Choice:");
 scanf("%d",&c);

 switch (c)
 {
  case 1:

  enqueue();
  break;

  case 2:
  dequeue();
  break;

  case 3:
  exit (0);

 }

 }
getch();
}
void enqueue()
{

  if(rear==n)
  {
   printf("\n\nQueue is full");
  }
  else
  {
   if(rear==front==-1)
   {
    front=0;
   }
   rear=rear+1;
   printf("\n Enter Your element::");
   scanf("%d",&item);

   e[rear]=item;
   printf("%d",e[i]);

   printf("\n\n%d is inserted\n\n",item);
  }


 for(i=front;i<=rear;i++)
 {
  printf("\n\n%d",e[i]);
 }
 getch();

}
int dequeue()
{
 int i,n;

  if(front==-1)
  {
   printf("\n\nQueue is empty");
  }
  else
  {
   int item;
   item=e[front];
   printf("\n\n%d is pop\n\n",item);

   e[item]=NULL;
   front++;
  }

 for(i=front;i<=rear;i++)
 {
  printf("\n\n%d",e[i]);
 }
 return 0;
}

WAP that take id password and check whether it is correct or not


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char id[5],pass[5];

 clrscr();

  printf("\n\n\n\n\n\t\t\t\t\t Enter ID:- ");
  gets(id);
  printf("\n\t\t\t\t\t Enter Password:- ");
  gets(pass);

 if(strcmp(id,"admin") == 0 && strcmp(pass,"12345")==0)
  {
  clrscr();
  printf("welcome");
  }
 else
  {
  clrscr();
  printf("sorry wrong password");
  }


 getch();

}

wap to find whether the given int is odd or even


/*wap to find whether the given int is odd or even*/

#include<stdio.h>
#include<conio.h>

void main()

{
	int n;
	clrscr();

	printf("enter any int:");
	scanf("%d",&n);

	if(n%2==0)
		printf("the number is even");
	else
		printf("the number is odd");

	getch();
}

WAP to Create Single Linked List


#include <stdlib.h>
#include <string.h>
#include<stdio.h>
#include<conio.h>
typedef struct node {
 int sid;
 char sname[25];
 int ssem;
 struct node *link;
} NODE;

NODE *InsFront(NODE *, int, char *, int);
NODE *InsBack(NODE *, int, char *, int);
NODE *InsPos(NODE *, int, char *, int, int);
NODE *DelNode(NODE *, int);
NODE *SrchUpdate(NODE *, int);
void Display(NODE *);

main() {
 NODE *start = NULL; /* Main Program */
 int opn, id, sem, p, insopn;
 char name[25];
 do {
  clrscr();
  printf("\n ### Linked List Operations ### \n\n");
  printf("\n Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exit\n");
  printf("\n Your option ? ");
  scanf("%d", &opn);
  switch (opn) {
  case 1:
   printf("Insertion at: Press 1->Front 2->Back 3->Pos ? ");
   scanf("%d", &insopn);
   printf("\n\nRead the Sid,Name, and Sem details ?");
   scanf("%d%s%d", &id, name, &sem);
   if (insopn == 1)
    start = InsFront(start, id, name, sem);
   else if (insopn == 2)
    start = InsBack(start, id, name, sem);
   else if (insopn == 3) {
    printf(" At What Position ? ");
    scanf("%d", &p);
    start = InsPos(start, id, name, sem, p);
   }
   break;

  case 2:
   printf(" Read the Student Id of the Node to be deleted ? ");
   scanf("%d", &id);
   start = DelNode(start, id);
   break;
  case 3:
   printf(" Read the Student Id of the Node to be Searched ? ");
   scanf("%d", &id);
   start = SrchUpdate(start, id);
   break;
  case 4:
   printf(" Linked List is \n");
   Display(start);
   break;
  case 5:
   printf("\n\n Terminating \n\n");
   break;
  default:
   printf("\n\nInvalid Option !!! Try Again !! \n\n");
   break;
  }
  printf("\n\n\n\n  Press a Key to Continue . . . ");
  getch();
 } while (opn != 5);
}

NODE *InsFront(NODE *st, int id, char *name, int sem) {
 NODE *temp;
 temp = (NODE *) malloc(sizeof(NODE));
 if (temp == NULL) {
  printf(" Out of Memory !! Overflow !!!");
  return (st);
 } else {
  temp->sid = id;
  strcpy(temp->sname, name);
  temp->ssem = sem;
  temp->link = st;
  printf(" Node has been inserted at Front Successfully !!");
  return (temp);
 }
}

NODE *InsBack(NODE *st, int id, char *name, int sem) {
 NODE *temp, *t;
 temp = (NODE *) malloc(sizeof(NODE));
 if (temp == NULL) {
  printf(" Out of Memory !! Overflow !!!");
  return (st);
 } else {
  temp->sid = id;
  strcpy(temp->sname, name);
  temp->ssem = sem;
  temp->link = NULL;
  if (st == NULL)
   return (temp);
  else {
   t = st;
   while (t->link != NULL)
    t = t->link;
   t->link = temp;
   printf(" Node has been inserted at Back Successfully !!");
   return (st);
  }
 }
}

NODE *InsPos(NODE *st, int id, char *name, int sem, int pos) {
 NODE *temp, *t, *prev;
 int cnt;
 temp = (NODE *) malloc(sizeof(NODE));
 if (temp == NULL) {
  printf(" Out of Memory !! Overflow !!!");
  return (st);
 } else {
  temp->sid = id;
  strcpy(temp->sname, name);
  temp->ssem = sem;
  temp->link = NULL;
  if (pos == 1) /* Front Insertion */
  {
   temp->link = st;
   return (temp);
  } else {
   t = st;
   cnt = 1;
   while (t != NULL && cnt != pos) {
    prev = t;
    t = t->link;
    cnt++;
   }
   if (t) /* valid Position  Insert new node*/
   {
    prev->link = temp;
    temp->link = t;
   } else
    printf(" Invalid Position !!!");
   printf(" Node has been inserted at given Position  Successfully !!");
   return (st);
  }
 }
}
NODE *DelNode(NODE *st, int id) {
 NODE *t, *prev;
 if (st == NULL) {
  printf(" Underflow!!!");
  return (st);
 } else {
  t = st;
  if (st->sid == id) /* Front Deletion */
  {
   st = st->link;
   t->link = NULL;
   free(t);
   return (st);
  } else {
   while (t != NULL && t->sid != id) {
    prev = t;
    t = t->link;
   }
   if (t) /* node to be deleted  found*/
   {
    prev->link = t->link;
    t->link = NULL;
    free(t);
   } else
    printf(" Invalid Student Id !!!");
   return (st);
  }
 }
}

NODE *SrchUpdate(NODE *st, int id) {
 NODE *t;
 if (st == NULL) {
  printf(" Empty List !!");
  return (st);
 } else {
  t = st;
  while (t != NULL && t->sid != id) {
   t = t->link;
  }
  if (t) /* node to be Updated found*/
  {
   printf(" Node with Student Id %d found inthe List !\n", id);
   printf(" Read the New Id,Name and Sem forthe Student\n");
   scanf("%d%s%d", t->sid, t->sname, t->ssem);
  } else
   printf(" Invalid Student Id !!!");
  return (st);
 }
}

void Display(NODE *st) {
 NODE *t;
 if (st == NULL)
  printf("Empty List\n");
 else {
  t = st;
  printf("Start->");
  while (t) {
   printf("[%d,%s,%d]->", t->sid, t->sname, t->ssem);
   t = t->link;
  }
  printf("Null\n");
 }
}

WAP to create double linked list : C Application

/*WAP to create double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void create(node *);
void display(node *);
void search(node *);
void insert_any(node *);
void delete_any(node *);

void main()
{
 node *header=NULL;
 int n;
 char c='y';
 clrscr();
 while(c=='y')
 {
  printf("\n1.Create\n2.Search\n3.display\n4.insert any\n5.Delete\n6.exit\nEnter your choice:");
  scanf("%d",&n);
  switch(n)
  {
   case 1:
   {
    header=(node*)malloc(sizeof(node));
    header->data=NULL;
    header->llink=NULL;
    header->rlink=NULL;
    create(header);
    break;
   }
   case 2:
   {
    search(header);
    break;
   }
   case 3:
   {
    display(header);
    break;
   }
   case 4:
   {
    insert_any(header);
    break;
   }
   case 5:
   {
    delete_any(header);
    break;
   }
   case 6:
   {
    exit(0);
   }
  }
 }
 getch();
}
void create(node *temphead)
{
 node *temp;
 char a='y';
 while(a=='y')
 {
  temp=(node *)malloc(sizeof(node));
  printf("Enter data:");
  scanf("%d",&temp->data);
  temp->rlink=NULL;
  temphead->llink=temphead;
  temphead->rlink=temp;
  printf("Do you want to create more??????");
  fflush(stdin);
  scanf("%c",&a);
  temphead=temp;
 }
}
void display(node *temphead)
{
 while(temphead!=NULL)
 {
  printf("\n%d",temphead->data);
  temphead=temphead->rlink;
 }
}
void search(node *temphead)
{
 int key,found=0;
 printf("Enter number you want to find:");
 scanf("%d",&key);
 while(temphead!=NULL)
 {

  if(temphead->data==key)
  {
   printf("\nSearch is sucessfull");
   found+=1;
   break;
  }
  temphead=temphead->rlink;
 }
 if(found==0)
 {
  printf("Search is unsucessfull");
 }
}
void insert_any(node *temphead)
{
 int key;
 node *temp;
 node *temp1;
 printf("After which number you want to insert?????");
 scanf("%d",&key);
 while(temphead->rlink!=NULL && temphead->data!=key)
 {
  temphead=temphead->rlink;
 }
 if(temphead->data!=key)
 {
  printf("Value not found insertion not possible");
 }
 else
 {
  temp=(node *)malloc(sizeof(node));
  printf("Enter data:");
  scanf("%d",&temp->data);
  temp1=temphead->rlink;
  temp->llink=temphead;
  temp->rlink=temp1;
  temphead->rlink=temp;
  printf("Insert sucessfully\n");
  if(temp1!=NULL)
  {
   temphead->llink=temp;
  }
 }
}



void delete_any(node *temphead)
{
 node *temp;
 node *p;
 int key;
 printf("Enter the value which you want to delete:");
 scanf("%d",&key);
 while(temphead->rlink!=NULL && temphead->data!=key)
 {
  temp=temphead;
  temphead=temphead->rlink;
 }
 if(temphead->data==key)
 {
  node *temp1;
  temp1=temphead->rlink;
  temp->rlink=temp1;
  temp1->llink=temp;
  free(temphead);
 }
 else
 {
  printf("Value not found......");
 }
}

WAP to create double linked list


/*WAP to create double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void create(node *);

void main()
{
 node *header=NULL;
 int n;
 clrscr();
    header=(node*)malloc(sizeof(node));
    header->data=NULL;
    header->llink=NULL;
    header->rlink=NULL;
    create(header);
 
 getch();
}
void create(node *temphead)
{
 node *temp;
 char a='y';
 while(a=='y')
 {
  temp=(node *)malloc(sizeof(node));
  printf("Enter data:");
  scanf("%d",&temp->data);
  temp->rlink=NULL;
  temphead->llink=temphead;
  temphead->rlink=temp;
  printf("Do you want to create more??????");
  fflush(stdin);
  scanf("%c",&a);
  temphead=temp;
 }
}

WAP to display from double linked list


/*WAP to display from double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void display(node *);
void main()
{
 node *header=NULL;
 int n;
 clrscr();
    display(header);
 
 getch();
}

void display(node *temphead)
{
 while(temphead!=NULL)
 {
  printf("\n%d",temphead->data);
  temphead=temphead->rlink;
 }
}

WAP to search any node from double linked list


/*WAP to search any node from double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void search(node *);

void main()
{
 node *header=NULL;
 int n;
 clrscr();
    search(header);
 
 getch();
}

void search(node *temphead)
{
 int key,found=0;
 printf("Enter number you want to find:");
 scanf("%d",&key);
 while(temphead!=NULL)
 {

  if(temphead->data==key)
  {
   printf("\nSearch is sucessfull");
   found+=1;
   break;
  }
  temphead=temphead->rlink;
 }
 if(found==0)
 {
  printf("Search is unsucessfull");
 }
}

WAP to insert any from double linked list

/*WAP to insert any from double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void insert_any(node *);

void main()
{
 node *header=NULL;
 int n;
 clrscr();
    insert_any(header);
  
 getch();
}

void insert_any(node *temphead)
{
 int key;
 node *temp;
 node *temp1;
 printf("After which number you want to insert?????");
 scanf("%d",&key);
 while(temphead->rlink!=NULL && temphead->data!=key)
 {
  temphead=temphead->rlink;
 }
 if(temphead->data!=key)
 {
  printf("Value not found insertion not possible");
 }
 else
 {
  temp=(node *)malloc(sizeof(node));
  printf("Enter data:");
  scanf("%d",&temp->data);
  temp1=temphead->rlink;
  temp->llink=temphead;
  temp->rlink=temp1;
  temphead->rlink=temp;
  printf("Insert sucessfully\n");
  if(temp1!=NULL)
  {
   temphead->llink=temp;
  }
 }
}

WAP to delet any double linked list


/*WAP to delet any node from double linked list*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *llink;
 struct node *rlink;
};
typedef struct node node;
void delete_any(node *);

void main()
{
 node *header=NULL;
 int n;
 clrscr();

    delete_any(header);
  
 getch();
}


void delete_any(node *temphead)
{
 node *temp;
 node *p;
 int key;
 printf("Enter the value which you want to delete:");
 scanf("%d",&key);
 while(temphead->rlink!=NULL && temphead->data!=key)
 {
  temp=temphead;
  temphead=temphead->rlink;
 }
 if(temphead->data==key)
 {
  node *temp1;
  temp1=temphead->rlink;
  temp->rlink=temp1;
  temp1->llink=temp;
  free(temphead);
 }
 else
 {
  printf("Value not found......");
 }
}