Learning coding means GreatToCode Be more than a Coder ! Greattocode , Join GreatToCode Community,1000+ Students Trusted On Us .If You want to learn coding, Then GreatToCode Help You.No matter what It Takes !


CODE YOUR WAY TO A MORE FULFILLING And HIGHER PAYING CAREER IN TECH, START CODING FOR FREE Camp With GreatToCode - Join the Thousands learning to code with GreatToCode
Interactive online coding classes for at-home learning with GreatToCode . Try ₹Free Per Month Coding Classes With The Top Teachers . Program for Data Structure C programming

Program for Data Structure C programming

 POLYADD.C 


#include <alloc.h>
#include <conio.h>

struct polynode *polycreate();
void polydisp(struct polynode *);
struct polynode *polyadd(struct polynode *, struct polynode *);


struct polynode
{
 int coeff, expo;
 struct polynode *link;
};

void main()
{
  struct polynode *poly1, *poly2, *poly3;

  printf("\nFirst Polynomial\n");
  poly1=polycreate();

  printf("\nSecond Polynomial\n");
  poly2=polycreate();

  poly3=polyadd(poly1,poly2);

  printf("\nFirst Polynomial:\n");
  polydisp(poly1);

  printf("\nSecond Polynomial:\n");
  polydisp(poly2);

  printf("\nAddition of above Polynomials:\n");
  polydisp(poly3);
  getch();
}


struct polynode *polycreate()
{
  struct polynode *first, *curr, *prev;
  int n,i;

 first=NULL;
 printf("How many terms:");
 scanf("%d",&n);
 printf("Enter Coeff and Expo of %d terms\n",n);
 for(i=1;i<=n;i++)
 { curr=(struct polynode *)malloc(sizeof(struct polynode));
   scanf("%d%d",&curr->coeff,&curr->expo);
   if(first==NULL)
   { first=curr;
     prev=curr;
   }
   else
   { prev->link=curr;
     prev=curr;
   }
 }//for
 curr->link=NULL;
 return(first);
}//polycreate

void polydisp(struct polynode *first)
{
  struct polynode *curr;

  curr=first;
  while(curr!=NULL)
  { printf(" %+dx^%d",curr->coeff,curr->expo);
    curr=curr->link;
  }
}


struct polynode *polyadd(struct polynode *poly1, struct polynode *poly2)
{
  struct polynode *poly3,*c1,*c2,*c3,*prev;
  poly3=NULL;
  c1=poly1; c2=poly2;

  while(c1!=NULL && c2!=NULL)
  {
   c3=(struct polynode *) malloc(sizeof(struct polynode));
   if (c1->expo==c2->expo)
   { c3->coeff=c1->coeff+c2->coeff;
     c3->expo=c1->expo;
     c1=c1->link; c2=c2->link;
   }
   else
   if(c1->expo > c2->expo)
   { c3->coeff=c1->coeff;
     c3->expo=c1->expo;
     c1=c1->link;
   }
   else
   {c3->coeff=c2->coeff;
    c3->expo=c2->expo;
    c2=c2->link;
   }

   if(poly3==NULL)//This is first term of add
   {
     poly3=c3; prev=c3;
   }
   else
   { prev->link=c3; prev=c3;
   }

  }//while

  while (c1!=NULL) //If first poly not finished
  { c3=(struct polynode *)malloc(sizeof(struct polynode));
    c3->coeff=c1->coeff;
    c3->expo=c1->expo;
    prev->link=c3; prev=c3;
    c1=c1->link;
  }
  while (c2!=NULL) //If Second poly not finished
  { c3=(struct polynode *)malloc(sizeof(struct polynode));
    c3->coeff=c2->coeff;
    c3->expo=c2->expo;
    prev->link=c3; prev=c3;
    c2=c2->link;
  }

  c3->link=NULL;

  return(poly3);
}//polyadd



Union and Intersection of two singly List


#include <alloc.h>
#include <conio.h>

#define NEWNODE (struct node*)malloc(sizeof(struct node))

struct node
{
 int data;
 struct node *link;
} *curr,*prev,*c1,*c2,*c3,*p3;

struct node *create();
void display(struct node *);
struct node *unionsl(struct node *f1,struct node *f2);
struct node *intersl(struct node *f1,struct node *f2);
struct node *attach(struct node *f3, struct node *c3);

void main()
{ struct node *f1,*f2,*f3;

  clrscr();
  printf("\nFirst Linked List");
  f1=create();
  display(f1);
  printf("\nSecond Linked List");
  f2=create();
  display(f2);

  printf("\nUnion of two linked list\n");
  f3=unionsl(f1,f2);
  display(f3);

  printf("\nIntersection of two linked list\n");
  f3=intersl(f1,f2);
  display(f3);

  getch();
}

struct node *create()
{
  struct node *first=NULL;
  int n,i;

  printf("\nHow many nodes:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    curr=NEWNODE;
    printf("\nEnter Data:");
    scanf("%d",&curr->data);
    if(first==NULL)
    { first=curr;
       prev=curr;
    }
    else
    {  prev->link=curr;
	prev=curr;
     }
   }
   curr->link=NULL;
   return(first);
}//create

void display(struct node *first)
{
 curr=first;
 while(curr!=NULL)
 { printf("%d\t",curr->data);
   curr=curr->link;
 }
}


struct node *unionsl(struct node *f1,struct node *f2)
{ struct node *f3=NULL;

  c1=f1; c2=f2;
  while(c1!=NULL && c2!=NULL)
  {
   c3=NEWNODE;
   if(c1->data<c2->data)
   {
     c3->data=c1->data;
     c1=c1->link;
   }
   else
   if(c1->data>c2->data)
   {
     c3->data=c2->data;
     c2=c2->link;
   }
   else
   {
    c3->data=c1->data;
    c1=c1->link; c2=c2->link;
   }

   f3=attach(f3,c3);

  }//while

  while(c1!=NULL) //copy remaining nodes of first list
  { c3=NEWNODE;
    c3->data=c1->data;
    c1=c1->link;
    f3=attach(f3,c3);
  }

  while(c2!=NULL) //copy remaining nodes of second list
  { c3=NEWNODE;
    c3->data=c2->data;
    c2=c2->link;
    f3=attach(f3,c3);
  }

  c3->link=NULL;
  return(f3);
}


struct node *intersl(struct node *f1, struct node *f2)
{
  struct node *f3=NULL;

  c1=f1; c2=f2;
  while(c1!=NULL && c2!=NULL)
  {
    if(c1->data<c2->data)
      c1=c1->link;
    else
    if(c1->data>c2->data)
      c2=c2->link;
    else
    { c3=NEWNODE;
      c3->data=c1->data;
      c1=c1->link;
      c2=c2->link;
      f3=attach(f3,c3);
    }
   }//while
   c3->link=NULL;
   return(f3);
 }//intersl


struct node *attach(struct node *f3, struct node *c3)
{
   if(f3==NULL)
   { f3=c3; p3=c3;
   }
   else
   { p3->link=c3; p3=c3;
   }
   return(f3);
}

Static representation of linked list


//Program to implement Linked List using array.
//Use array of struct.
struct node
{
  int data;
  int link;
};

void main()
{
  struct node a[10];
  int n,i,pos,ppos,dat,start=-1;
  clrscr();
  printf("How many elements:");
  scanf("%d",&n);
  printf("Enter position(0 to %d) and data of elements\n",n-1);
  for(i=0;i<n;i++)
  {
   scanf("%d%d",&pos,&dat);
   a[pos].data=dat;
   if(start==-1) //first element
   { start=pos;
     ppos=pos;
   }
   else
   { a[ppos].link=pos;
     ppos=pos;
   }
  } //for
  a[pos].link=-1;

  printf("Linked List using Array \n");
  printf("Start Index=%d\n",start);
  printf("Index\tData\tLink\n");
  for(i=0;i<n;i++)
   printf("%d\t%d\t%d\n",i,a[i].data,a[i].link);
  getch();
}//main

C program to evaluate postfix expresssion


#include <stdio.h>
int stk[20];
int top=-1;

void push(int);
int pop();
void solve(char);
int isoptr(char);

void main()
{
  char expr[20],op;
  int i;
  clrscr();
  printf("\nEnter Postfix Expression:");
  gets(expr);
  for(i=0;expr[i]!='\0';i++)
  {
   op=expr[i];
   if(isoptr(op))
     solve(op);
   else //operand
     push(op-'0');
  }

  printf("Result=%d",stk[top]);
}//main

void push(int op)
{
  stk[++top]=op;
}

int pop()
{
  return(stk[top--]);
}

void solve(char optr)
{
  int opnd1,opnd2,res;

   opnd2=pop();
   opnd1=pop();
   if(optr=='+')
     res=opnd1+opnd2;
   else
   if(optr=='-')
     res=opnd1-opnd2;
   else
   if(optr=='*')
     res=opnd1*opnd2;
   else
   if(optr=='/')
     res=opnd1/opnd2;

   push(res);
}

int isoptr(char op) //return 1 if op is operator else 0
{

/*  if(op=='+'|op=='-'|op=='*'|op=='/')
     return 1;
  else
     return 0;
*/
  switch(op)
  {
    case '+':
    case '-':
    case '*':
    case '/': return 1;
  }
  return 0;
}

Implementation of Ascending Priority Queue



//Insert: Element is inserted in ascending order
//Delete: Delete front element which highest priority(smallest value)

#include <alloc.h>

struct node
{
 int data;
 struct node *link;
} *front=NULL,*rear=NULL;

void inspq(int);
int delpq();

void main()
{

  int choice,x;
 clrscr();
 while(1)
 {
  printf("\n\n1.Insert\n2.Delete\n0.Exit");
  printf("\nEnter choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:printf("\nEnter data to insert:");
	  scanf("%d",&x);
	  inspq(x);
	  break;
   case 2:x=delpq();
	  printf("\nDeleted data:%d",x);
	  break;
   case 0: exit(0);
   default: printf("Invalid choice");
  }
 }
}//main


void inspq(int x)
{
  struct node *nnode,*curr,*prev;

  nnode=(struct node *)malloc(sizeof(struct node));
  nnode->data=x; nnode->link=NULL;

  curr=front;
  while(curr->data<=nnode->data)
  {
    prev=curr;
    curr=curr->link;
    if(curr==NULL) break;
  }
  prev->link=nnode;
  nnode->link=curr;

  if(curr==front)
    front=nnode;
}

int delpq()
{ int x;
  struct node *curr;

  if(front==NULL)
  {
    printf("PQ is empty..");
    return(-1);
  }
  curr=front;
  front=front->link;
  x=curr->data;
  free(curr);
  return(x);
}

Union and Intersection of two singly List



#include <alloc.h>
#include <conio.h>

#define NEWNODE (struct node*)malloc(sizeof(struct node))

struct node
{
 int data;
 struct node *link;
} *curr,*prev,*c1,*c2,*c3,*p3;

struct node *create();
void display(struct node *);
struct node *unionsl(struct node *f1,struct node *f2);
struct node *intersl(struct node *f1,struct node *f2);
struct node *attach(struct node *f3, struct node *c3);

void main()
{ struct node *f1,*f2,*f3;

  clrscr();
  printf("\nFirst Linked List");
  f1=create();
  display(f1);
  printf("\nSecond Linked List");
  f2=create();
  display(f2);

  printf("\nUnion of two linked list\n");
  f3=unionsl(f1,f2);
  display(f3);

  printf("\nIntersection of two linked list\n");
  f3=intersl(f1,f2);
  display(f3);

  getch();
}

struct node *create()
{
  struct node *first=NULL;
  int n,i;

  printf("\nHow many nodes:");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
    curr=NEWNODE;
    printf("\nEnter Data:");
    scanf("%d",&curr->data);
    if(first==NULL)
    { first=curr;
       prev=curr;
    }
    else
    {  prev->link=curr;
	prev=curr;
     }
   }
   curr->link=NULL;
   return(first);
}//create

void display(struct node *first)
{
 curr=first;
 while(curr!=NULL)
 { printf("%d\t",curr->data);
   curr=curr->link;
 }
}


struct node *unionsl(struct node *f1,struct node *f2)
{ struct node *f3=NULL;

  c1=f1; c2=f2;
  while(c1!=NULL && c2!=NULL)
  {
   c3=NEWNODE;
   if(c1->data<c2->data)
   {
     c3->data=c1->data;
     c1=c1->link;
   }
   else
   if(c1->data>c2->data)
   {
     c3->data=c2->data;
     c2=c2->link;
   }
   else
   {
    c3->data=c1->data;
    c1=c1->link; c2=c2->link;
   }

   f3=attach(f3,c3);

  }//while

  while(c1!=NULL) //copy remaining nodes of first list
  { c3=NEWNODE;
    c3->data=c1->data;
    c1=c1->link;
    f3=attach(f3,c3);
  }

  while(c2!=NULL) //copy remaining nodes of second list
  { c3=NEWNODE;
    c3->data=c2->data;
    c2=c2->link;
    f3=attach(f3,c3);
  }

  c3->link=NULL;
  return(f3);
}


struct node *intersl(struct node *f1, struct node *f2)
{
  struct node *f3=NULL;

  c1=f1; c2=f2;
  while(c1!=NULL && c2!=NULL)
  {
    if(c1->data<c2->data)
      c1=c1->link;
    else
    if(c1->data>c2->data)
      c2=c2->link;
    else
    { c3=NEWNODE;
      c3->data=c1->data;
      c1=c1->link;
      c2=c2->link;
      f3=attach(f3,c3);
    }
   }//while
   c3->link=NULL;
   return(f3);
 }//intersl


struct node *attach(struct node *f3, struct node *c3)
{
   if(f3==NULL)
   { f3=c3; p3=c3;
   }
   else
   { p3->link=c3; p3=c3;
   }
   return(f3);
}


Program to reverse a string using stack


#include "stack.h"

void main()
{
  char str[30],rstr[30];
  int i;

  clrscr();
  printf("Enter a string:");
  gets(str);

  //Push one by one char of string into stack
  top=-1;
  for(i=0; str[i]!='\0'; i++)
    push(str[i]);

  //Pop characters from stack and store into rstr
  //String get in reverse

  i=0;
  while(top!=-1)
   { rstr[i]=pop();
     i++;
   }
  rstr[i]='\0';
  printf("Reverse String:%s",rstr);
  getch();
}


void push(char ch)
{
  stk[++top]=ch;
  /*
    top++;
    stk[top]=ch;
  */
}

char pop()
{
 return(stk[top--]);

/* char ch;
 ch=stk[top];
 top--;
 return(ch);
*/
}


Circular Queue using Array



#define MAX 5

int cque[MAX], front=0, rear=0;

void inscque(int);
int delcque();
void main()
{
  int choice,x;
  clrscr();
  while(1)
  {
  printf("\n\n1.Insert\n2.Delete\n0.Exit");
  printf("\nEnter choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1: printf("Enter data to insert:");
	   scanf("%d",&x);
	   inscque(x);
	   break;
   case 2: x=delcque();
	   printf("Data deleted:%d",x);
	   break;
   case 0:  exit(0);
   default: printf("Wrong choice");
  }//switch
  }
}//main

void inscque(int x)
{
  if((rear+1)%MAX==front)
    printf("Cir Queue is full..");
  else
  {
   rear=(rear+1)%MAX;

   /*OR
    rear=rear+1;
    if(rear==MAX)
      rear=0;*/

   cque[rear]=x;
  }
}

int delcque()
{
  if(front==rear)
  {  printf("Cir Queue is empty..");
     return(-1);
  }
  else
  {
    front=(front+1)%MAX;
    return(cque[front]);
  }
}


Implement queue using array - static queue



#define MAX 5

int que[MAX], front=-1,rear=-1;

void insque(int);
int delque();
void main()
{
  int choice,x;
  clrscr();
  while(1)
  {
  printf("\n\n1.Insert\n2.Delete\n0.Exit");
  printf("\nEnter choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1: printf("Enter data to insert:");
	   scanf("%d",&x);
	   insque(x);
	   break;
   case 2: x=delque();
	   printf("Data deleted:%d",x);
	   break;
   case 0:  exit(0);
   default: printf("Wrong choice");
  }//switch
  }
}//main


void insque(int x)
{
  if(rear==MAX-1)
    printf("Queue is full..");
  else
    que[++rear]=x;
}


int delque()
{
  int x;
  if(front==rear)
  {  printf("Queue is empty..");
     return(-1);
  }
  else
  {
     front++;
     x=que[front];
     return(x);
  }
}

Implementation of Ascending Priority Queue



//Insert: Element is inserted in ascending order
//Delete: Delete front element which higherst priority(smallest value)

#include <alloc.h>

struct node
{
 int data;
 struct node *link;
} *front=NULL,*rear=NULL;

void inspq(int);
int delpq();

void main()
{

  int choice,x;
 clrscr();
 while(1)
 {
  printf("\n\n1.Insert\n2.Delete\n0.Exit");
  printf("\nEnter choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:printf("\nEnter data to insert:");
	  scanf("%d",&x);
	  inspq(x);
	  break;
   case 2:x=delpq();
	  printf("\nDeleted data:%d",x);
	  break;
   case 0: exit(0);
   default: printf("Invalid choice");
  }
 }
}//main


void inspq(int x)
{
  struct node *nnode,*curr,*prev;

  nnode=(struct node *)malloc(sizeof(struct node));
  nnode->data=x; nnode->link=NULL;

  curr=front;
  while(curr->data<=nnode->data)
  {
    prev=curr;
    curr=curr->link;
    if(curr==NULL) break;
  }
  prev->link=nnode;
  nnode->link=curr;

  if(curr==front)
    front=nnode;
}

int delpq()
{ int x;
  struct node *curr;

  if(front==NULL)
  {
    printf("PQ is empty..");
    return(-1);
  }
  curr=front;
  front=front->link;
  x=curr->data;
  free(curr);
  return(x);
}


Implementation of Descending Priority Queue




//Insert: Element is inserted in descending order
//Delete: Delete front element of higherst priority(largest value)

#include <alloc.h>

struct node
{
 int data;
 struct node *link;
} *front=NULL,*rear=NULL;

void inspq(int);
int delpq();

void main()
{

  int choice,x;
 clrscr();
 while(1)
 {
  printf("\n\n1.Insert\n2.Delete\n0.Exit");
  printf("\nEnter choice:");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1:printf("\nEnter data to insert:");
	  scanf("%d",&x);
	  inspq(x);
	  break;
   case 2:x=delpq();
	  printf("\nDeleted data:%d",x);
	  break;
   case 0: exit(0);
   default: printf("Invalid choice");
  }
 }
}//main


void inspq(int x)
{
  struct node *nnode,*curr,*prev;

  nnode=(struct node *)malloc(sizeof(struct node));
  nnode->data=x; nnode->link=NULL;

  curr=front;
  while(curr->data>=nnode->data)
  {
    prev=curr;
    curr=curr->link;
    if(curr==NULL) break;
  }
  prev->link=nnode;
  nnode->link=curr;

  if(curr==front)
    front=nnode;
}

int delpq()
{ int x;
  struct node *curr;

  if(front==NULL)
  {
    printf("PQ is empty..");
    return(-1);
  }
  curr=front;
  front=front->link;
  x=curr->data;
  free(curr);
  return(x);
}








Post a Comment

0 Comments

•Give The opportunity to your child with GreatToCode Kid's • Online Coding Classes for Your Kid • Introduce Your kid To the world's of coding
•Fuel You Career with our 100+ Hiring Partners, Advance Your Career in Tech with GreatToCode. •Join The Largest Tech and coding Community and Fast Forward Your career with GreatToCode. •10000+ Learner's+ 90 % placement Guarantee. • Learning Coding is Better with the GreatToCode community .
•Greattocode Kid's •GreatToCode Career •GreatToCode Interview •GreatToCode Professional •GreatToCode for schools •GreatToCode For colleges •GreatToCods For Businesses.
Are you ready to join the millions of people learning to code? GreatToCode Pass is your one-stop-shop to get access to 1000+ courses, top-notch support, and successful job placement. What are you waiting for? Sign up now and get your future in motion with GreatToCode Pass.