Showing posts with label malloc. Show all posts
Showing posts with label malloc. Show all posts

Monday, April 4, 2016

C program to implement sorting in link list

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
 int info;
 struct node* ptr;
}*START;
void create()
{
 int n;
 struct node *temp;
 temp=(struct node*)malloc(sizeof(node));
 if(temp==NULL)
  {
   printf("Memory not available");
  }
 printf("Enter data to enter");
 scanf("%d",&n);
 temp->info=n;
 temp->ptr=NULL;
 if(START==NULL)
  {
   START=temp;
  }
 else
  {
   struct node *t =START;
   while(t->ptr!=NULL)
    {
     t=t->ptr;
    }
   t->ptr=temp;
  }
}
void display()
{
 struct node *t=START;
 printf("\n");
 while(t!=NULL)
  {
   printf("%d ",t->info);
   t=t->ptr;
  }
}
void sort()
{
 struct node *t1=START;
 struct node *t2;
 int count=0;
 int i=0,j;
 while(t1!=NULL)
 {
  count++;
  t1=t1->ptr;
 }
 for(i=0;i<count;i++)
  {
   t1=START;
   for(j=0;j<count-i-1;j++)
    {
      if(t1->info>(t1->ptr)->info)
       {
int temp=t1->info;
t1->info=(t1->ptr)->info;
(t1->ptr)->info=temp;
       }
       t1=t1->ptr;
    }
  }
}
void main()
{
 START=NULL;
 int ch;
 do
 {
  printf("\nEnter choice 1.Insert 2.display 3.Sort 4.Exit");
  scanf("%d",&ch);
  switch(ch)
   {
    case 1:
     create();
     break;
    case 2:
     display();
     break;
    case 3:
     sort();
     break;
    case 4:
     exit(0);
     break;
   }
 }while(ch!=4);
}
output

Thursday, March 31, 2016

C program to implement link list using malloc

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<process.h>
struct node{
int info;
struct node *link;
}*start;
void addfirst()
 {
  struct node *ptr;
  int n;
  ptr=start;
  struct node *nw;
  nw=(node *)malloc(sizeof(node));
  printf("Enter item to insert");
  scanf("%d",&nw->info);
  if(nw==NULL)
   {
    printf("Overflow");
    exit(0);
   }
  if(start==NULL)
   {
    nw->link=NULL;
    start=nw;
   }
  else
   {
    nw->link=start;
    start=nw;
   }
 }
void addlast()
 {
  int n;
  struct node *nw,*ptr;
  nw=(node *)malloc(sizeof(node));
  if(nw==NULL)
   {
    printf("Overflow");
    exit(0);
   }
  printf("Enter data to enter");
  scanf("%d",&nw->info);
  ptr=start;
  while(ptr->link!=NULL)
   ptr=ptr->link;
  nw->link=NULL;
  ptr->link=nw;
 }
void addafter()
 {
  int n,pos;
  struct node *nw,*ptr;
  nw=(node *)malloc(sizeof(node));
  if(nw==NULL)
   {
    printf("overflow");
    exit(0);
   }
  printf("Enter data to insert");
  scanf("%d",&nw->info);
  printf("\n\nEnter node no afterwhich you want to insert node");
  scanf("%d",&pos);
  ptr=start;
  for(n=1;n<pos;n++)
   ptr=ptr->link;
  nw->link=ptr->link;
  ptr->link=nw;
 }
void deletenode()
 {
  int n;
  printf("Enter position of the node you want to delete\n\n");
  scanf("%d",&n);
  struct node *ptr;
  ptr=start;
  for(int i=1;i<n-1;i++)
   ptr=ptr->link;
  if(i==1)
   {
    start=start->link;
   }
  else
   {
    ptr->link=ptr->link->link;
   }
 }
void display()
 {
  struct node *ptr;
  ptr=start;
  while(ptr!=NULL)
   {
    printf(" ->%d",ptr->info);
    ptr=ptr->link;
   }
 }
void main()
{
 int n;
 start=0;
 do
 {
  printf("\n\n1.ADDFIRST\n\n2.ADDAFTER\n\n3.ADDLAST\n\n4.DISPLAY\n\n5.DELETE\n\n6.EXIT\n\n");
  scanf("%d",&n);
  if(n==1)
   {
    addfirst();
   }
  if(n==2)
   {
    addafter();
   }
  if(n==3)
   {
    addlast();
   }
  if(n==4)
   {
    display();
   }
  if(n==5)
   {
    deletenode();
   }
 }while(n!=6);
}
output
1. ADDFIRST
2.ADDAFTER
2.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
1
Enter item to insert 5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
3
Enter data to enter
5
1. ADDFIRST
2.ADDAFTER
3.ADDLAST
4.DISPLAY
5.DELETE
6.EXIT
4
->3->5

Saturday, March 12, 2016

C program to dyncamically allocate and deallocate memory in an array.

#include<stdio.h>
#include<stdlib.h>
void main()
{
  int *a,*b;
 int x,y,i;
 printf("Please enter the size of array");
 scanf("%d",&x);
 a=(int*)malloc(x*sizeof(int));
 printf("\n Enter %d values in array",x);
 for(i=0;i<x;i++)
  {
   scanf("%d",(a+i));
  }
 printf("\nValue in the array is");
 for(i=0;i<x;i++)
  {
   printf("%d ",*(a+i));
  }
 free(a);
 }

output

Wednesday, March 9, 2016

C program for queue implementation using link list

#include <stdio.h>
#include <stdlib.h>
//structure for the link list
struct node
{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;
//enq for insertion in queue
void enq(int data);
//deq for deletion from queue
void deq();
//for displaying queue data
void display();
//called once for the initialization
void create();
int count = 0;
void main()
{
    int no, ch, e;
    printf("\n 1 - Enque");
    printf("\n 2 - Deque");
    printf("\n 3 - Display");
    printf("\n 4 - Exit");
    create();
    while (1)
    {
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
   printf("Enter data : ");
   scanf("%d", &no);
   enq(no);
   break;
case 2:
   deq();
   break;
case 3:
   display();
   break;
case 4:
   exit(0);
   break;
default:
   printf("Wrong choice, Please enter correct choice  ");
   break;
}
    }
}
void create()
{
    front = rear = NULL;
}
void enq(int data)
{
    if (rear == NULL)
    {
rear = (struct node *)malloc(1*sizeof(struct node));
rear->ptr = NULL;
rear->info = data;
front = rear;
    }
    else
    {
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
    }
    count++;
}
void display()
{
    front1 = front;
    if ((front1 == NULL) && (rear == NULL))
    {
printf("Queue is empty");
return;
    }
    while (front1 != rear)
    {
printf("%d ->", front1->info);
front1 = front1->ptr;
    }
    if (front1 == rear)
printf("%d->", front1->info);
}
void deq()
{
    front1 = front;
    //check if the queue is empty
    if (front1 == NULL)
    {
printf("\n Error: Trying to display elements from empty queue");
return;
    }
    else
if (front1->ptr != NULL)
{
   front1 = front1->ptr;
   printf("\n Dequed value : %d", front->info);
   free(front);
   front = front1;
}
else
{
   printf("\n Dequed value : %d", front->info);
   free(front);
   front = NULL;
   rear = NULL;
}
count--;
}

output
1 - Enque
2 - Deque
3 - Display
4 - Exit
Enter choice : 1
Enter data : 4
Enter choice :1
Enter data  8
Enter choice :3
4->3->
Enter choice :2
Dequed value : 4
Enter choice :1
Enter data : 5
Enter choice :3
3->5->
Enter choice :4