C Program for Infix to Postfix Conversion.

C Program for Infix to Postfix Conversion. Here we covert the infix expression to postfix expression by using stack. for example a*b-c/d is the infix expression, and equivalent postfix expression is: ab*cd/-. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */

push(char elem) { /* Function for PUSH operation */
s[++top] = elem;
}

char pop() { /* Function for POP operation */
return (s[top--]);
}

int pr(char elem) { /* Function for precedence */
switch (elem) {
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}

main() { /* Main Program */
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("nnRead the Infix Expression ? ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '') {
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')') {
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
} else { /* Operator */
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = ''; /* Make pofx as valid string */
printf("nnGiven Infix Expn: %s Postfix Expn: %sn", infx, pofx);
}

Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C Program to do the operations of Sequential file with records.

C Program to do the operations of Sequential file with records. Sequential file, A file which consists of the same record types that is stored on a secondary storage device. The physical sequence of records may be based upon sorting values of one or more data items or fields. Here we are creating, Reading, searching the sequential file, using c file i/o operations. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/


#include <stdio.h>
typedef struct
{
int usn;
char name[25];
int m1,m2,m3;
}STD;

STD s;

void display(FILE *);
int search(FILE *,int);

void main()
{
int i,n,usn_key,opn;
FILE *fp;
printf(" How many Records ? ");
scanf("%d",&n);
fp=fopen("stud.dat","w");
for(i=0;i<n;i++)
{
printf("Read the Info for Student: %d (usn,name,m1,m2,m3) n",i+1);
scanf("%d%s%d%d%d",&s.usn,s.name,&s.m1,&s.m2,&s.m3);
fwrite(&s,sizeof(s),1,fp);
}
fclose(fp);
fp=fopen("stud.dat","r");
do
{
printf("Press 1- Displayt 2- Searcht 3- Exitt Your Option?");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("n Student Records in the File n");
display(fp);
break;
case 2: printf(" Read the USN of the student to be searched ?");
scanf("%d",&usn_key);
if(search(fp,usn_key))
{
printf("Success ! Record found in the filen");
printf("%dt%st%dt%dt%dn",s.usn,s.name,s.m1,s.m2,s.m3);
}
else
printf(" Failure!! Record with USN %d not foundn",usn_key);
break;
case 3: printf(" Exit!! Press a key . . .");
getch();
break;
default: printf(" Invalid Option!!! Try again !!!n");
break;
}
}while(opn != 3);
fclose(fp);
} /* End of main() */

void display(FILE *fp)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
printf("%dt%st%dt%dt%dn",s.usn,s.name,s.m1,s.m2,s.m3);
}
int search(FILE *fp, int usn_key)
{
rewind(fp);
while(fread(&s,sizeof(s),1,fp))
if( s.usn == usn_key) return 1;
return 0;
}





Read more Similar C Programs
C Basic

C File i/o

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to implement stack.

Data structures using C,
Write a C program to implement stack. Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are added or deleted from only one end, i.e. top of the stack. Stack is a LIFO data structure. LIFO – Last in First Out Perform PUSH(insert operation), POP(Delete operation) and Display stack . Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/



#include <stdio.h>
#include <conio.h>
#define MAXSIZE 5

struct stack /* Structure definition for stack */
{
int stk[MAXSIZE];
int top;
};

typedef struct stack STACK;
STACK s;

/* Function declaration/Prototype*/

void push (void);
int pop(void);
void display (void);

void main ()
{
int choice;
int option = 1;

clrscr ();

s.top = -1;

printf ("STACK OPERATIONn");
while (option)
{
printf ("------------------------------------------n");
printf (" 1 --> PUSH n");
printf (" 2 --> POP n");
printf (" 3 --> DISPLAY n");
printf (" 4 --> EXIT n");
printf ("------------------------------------------n");

printf ("Enter your choicen");
scanf ("%d", &choice);

switch (choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return;
}

fflush (stdin);
printf ("Do you want to continue(Type 0 or 1)?n");
scanf ("%d", &option);
}
}

/*Function to add an element to the stack*/
void push ()
{
int num;
if (s.top == (MAXSIZE - 1))
{
printf ("Stack is Fulln");
return;
}
else
{
printf ("Enter the element to be pushedn");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}


/*Function to delete an element from the stack*/
int pop ()
{
int num;
if (s.top == - 1)
{
printf ("Stack is Emptyn");
return (s.top);
}
else
{
num = s.stk[s.top];
printf ("poped element is = %dn", s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}

/*Function to display the status of the stack*/
void display ()
{
int i;
if (s.top == -1)
{
printf ("Stack is emptyn");
return;
}
else
{
printf ("nThe status of the stack isn");
for (i = s.top; i >= 0; i--)
{
printf ("%dn", s.stk[i]);
}
}
printf ("n");
}
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to illustrate the operations of singly linked list.

Data structures using C,
C program to illustrate the operations of singly linked list. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. In this program we insert, delete, search, and display the linked list. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MAX 30

struct EMP
{
int empno;
char empName[MAX];
char designation[MAX];
struct EMP *next;
};

/*********************************************************************/
/* Function to insert a node at the front of the linked list. */
/* front: front pointer, id: employee ID, name: employee name */
/* desg: Employee designation */
/* Returns the new front pointer. */
/*********************************************************************/

struct EMP* insert(struct EMP *front, int id, char name[], char desg[])
{
struct EMP *newnode;

newnode = (struct EMP*) malloc(sizeof(struct EMP));

if (newnode == NULL)
{
printf("nAllocation failedn");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
strcpy(newnode->designation, desg);
newnode->next = front;
front = newnode;
return(front);
} /*End of insert() */

/* Function to display a node in a linked list */
void printNode(struct EMP *p)
{
printf("nEmployee Details...n");
printf("nEmp No : %d", p->empno);
printf("nName : %s", p->empName);
printf("nDesignation : %sn", p->designation);
printf("-------------------------------------n");
} /*End of printNode() */

/* ********************************************************/
/* Function to deleteNode a node based on employee number */
/* front: front pointer, id: Key value */
/* Returns: the modified list. */
/* ********************************************************/

struct EMP* deleteNode(struct EMP *front, int id)
{
struct EMP *ptr;
struct EMP *bptr; /* bptr is pointing to the node behind ptr */


if (front->empno == id)
{
ptr = front;
printf("nNode deleted:");
printNode(front);
front = front->next;
free(ptr);
return(front);
}

for(ptr=front->next, bptr=front; ptr!=NULL; ptr=ptr->next, bptr=bptr->next)
{
if (ptr->empno == id)
{
printf("nNode deleted:");
printNode(ptr);
bptr->next = ptr->next;
free(ptr);
return(front);
}
}
printf("nEmployee Number %d not found ", id);
return(front);
} /*End of deleteNode() */

/*****************************************************************/
/* Function to search the nodes in a linear fashion based emp ID */
/* front: front pointer, key: key ID. */
/*****************************************************************/
void search(struct EMP *front, int key)
{
struct EMP *ptr;

for (ptr = front; ptr != NULL; ptr = ptr -> next)
{
if (ptr->empno == key)
{
printf("nKey found:");
printNode(ptr);
return;
}
}
printf("nEmployee Number %d not found ", key);
} /*End of search() */

/* Function to display the linked list */
void display(struct EMP *front)
{
struct EMP *ptr;

for (ptr = front; ptr != NULL; ptr = ptr->next)
{
printNode(ptr);
}
} /*End of display() */

/* Function to display the menu of operations on a linked list */
void menu()
{
printf("---------------------------------------------n");
printf("Press 1 to INSERT a node into the list n");
printf("Press 2 to DELETE a node from the list n");
printf("Press 3 to DISPLAY the list n");
printf("Press 4 to SEARCH the list n");
printf("Press 5 to EXIT n");
printf("---------------------------------------------n");
} /*End of menu() */

/* Function to select the option */
char option()
{
char choice;

printf("nn>> Enter your choice: ");
switch(choice=getche())
{
case '1':
case '2':
case '3':
case '4':
case '5': return(choice);
default : printf("nInvalid choice.");
}
return choice;
} /*End of option() */

/* The main() program begins */
void main()
{
struct EMP *linkList;
char name[21], desig[51];
char choice;
int eno;

linkList = NULL;

printf("nWelcome to demonstration of singly linked listn");

menu(); /*Function call */

do {
choice = option(); /*to choose oeration to be performed */

switch(choice)
{
case '1': printf("nEnter the Employee Number : ");
scanf("%d", &eno);

printf("Enter the Employee name : ");
fflush(stdin);
gets(name);

printf("Enter the Employee Designation : ");
gets(desig);

linkList = insert(linkList, eno, name, desig);
break;

case '2': printf("nnEnter the employee number to be deleted: ");
scanf("%d", &eno);

linkList = deleteNode(linkList, eno);
break;

case '3': if (linkList == NULL)
{
printf("nList empty.");
break;
}
display(linkList);
break;

case '4': printf("nnEnter the employee number to be searched: ");
scanf("%d", &eno);

search(linkList, eno);
break;

case '5': break;
}
} while (choice != '5');
} /*End fo main()*/
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com

C program to create a linked list and display the elements in the list.

Data structures using C,
C program to create a linked list and display the elements in the list. Linked list is a data structure in which the objects are arranged in a linear order. Linked List contains group of nodes, in which each node contains two fields, one is data field and another one is the reference field which contains the address of next node. Read more about C Programming Language .

/***********************************************************
* You can use all the programs on www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact [email protected]
* To find more C programs, do visit www.c-program-example.com
* and browse!
*
* Happy Coding
***********************************************************/

#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>

void main()
{
struct node
{
int num;
struct node *ptr;
};

typedef struct node NODE;

NODE *head, *first, *temp=0;
int count = 0;
int choice = 1;
first = 0;

while(choice)
{
head =(NODE*) malloc(sizeof(NODE));
printf("Enter the data itemn");
scanf("%d", &head-> num);

if(first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?n");
scanf("%d", &choice);

} /* End of while */

temp->ptr = 0;
temp = first; /* reset temp to the beginning*/
printf("nstatus of the linked list isn");

while(temp!=0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULLn");
printf("No. of nodes in the list = %dn", count);
} /* End of main*/
Read more Similar C Programs
Data Structures

Learn C Programming

You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can Follow @c_program

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it

(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

(c) www.c-program-example.com