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 illustrate as to how the data stored on the disk is read.

C Program to illustrate as to how the data stored on the disk is read. In this program we use the C File i/o operations to read the file from disk. fopen function open the files for reading, and fclose() closes the file. 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>

void main()
{
FILE *fptr;
char filename[15];
char ch;

printf("Enter the filename to be openedn");
gets(filename);

fptr = fopen (filename, "r"); /*open for reading*/

if (fptr == NULL)
{
printf("Cannot open filen");
exit(0);
}

ch = fgetc(fptr);

while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(fptr);
}

fclose(fptr);
} /* End of main () */
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 find the size of a union.

C program to find the size of a union. Union is a collection of different data types like a structure but it gives the single piece of memory for all data types. In this program we use the sizeof() operator to find the size of union. sizeof() return the size of data types in bytes. 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>

void main()
{
union sample
{
int m;
float n;
char ch;
};

union sample u;

printf("The size of union =%dn", sizeof(u));

/*initialization */

u.m = 25;
printf("%d %f %cn", u.m, u.n,u.ch);

u.n = 0.2;
printf("%d %f %cn", u.m, u.n,u.ch);

u.ch = 'p';
printf("%d %f %cn", u.m, u.n,u.ch);
} /*End of main() */
Read more Similar C Programs
Learn C Programming

Simple C Programs

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 find the sum of two-dimensional arrays using Dynamic Memory Allocation.

Arrays in C
C Program to find the sum of two-dimensional arrays using Dynamic Memory Allocation. The malloc() function allocates the dynamic memory to variables of specified bytes.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()
{
int i,n;
int *a,*b,*c;

printf("How many Elements in each array...n");
scanf("%d", &n);

a = (int *) malloc(n*sizeof(int));
b = (int *) malloc(n*sizeof(int));
c =( int *) malloc(n*sizeof(int));

printf("Enter Elements of First Listn");
for(i=0;i<n;i++)
{
scanf("%d",a+i);
}

printf("Enter Elements of Second Listn");
for(i=0;i<n;i++)
{
scanf("%d",b+i);
}

for(i=0;i<n;i++)
{
*(c+i) = *(a+i) + *(b+i);
}

printf("Resultant List isn");
for(i=0;i<n;i++)
{
printf("%dn",*(c+i));
}

} /* End of main() */
Read more Similar C Programs
Array In C

Simple C Programs

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 sort the matrix rows and columns.

C Program to sort the matrix rows and columns. This C program accept a order MxN Matrix, and sort all rows of the matrix in ascending order and all columns in descending order . In this program, we use the for statement to read two dimension arrays. 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>

void main ()
{
static int ma[10][10],mb[10][10];
int i,j,k,a,m,n;

printf ("Enter the order of the matrix n");
scanf ("%d %d", &m,&n);

printf ("Enter co-efficients of the matrix n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
scanf ("%d",&ma[i][j]);
mb[i][j] = ma[i][j];
}
}
printf ("The given matrix is n");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("n");
}

printf ("After arranging rows in ascending ordern");
for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
for (k=(j+1);k<n;++k)
{
if (ma[i][j] > ma[i][k])
{
a = ma[i][j];
ma[i][j] = ma[i][k];
ma[i][k] = a;
}
}
}
} /* End of outer for loop*/

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",ma[i][j]);
}
printf ("n");
}

printf ("After arranging the columns in descending order n");
for (j=0;j<n;++j)
{
for (i=0;i<m;++i)
{
for (k=i+1;k<m;++k)
{
if (mb[i][j] < mb[k][j])
{
a = mb[i][j];
mb[i][j] = mb[k][j];
mb[k][j] = a;
}
}
}
} /* End of outer for loop*/

for (i=0;i<m;++i)
{
for (j=0;j<n;++j)
{
printf (" %d",mb[i][j]);
}
printf ("n");
}

} /*End of main() */
Read more Similar C Programs
C Basic.

C Matrix.

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 Compute X ^ N

C program to compute the value of X ^ N, given X and N as inputs. Program accepts two integers x, n and computes the power of x to the n by using recursion function power().  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 <math.h>

void main()
{
long int x,n,xpown;
long int power(int x, int n);

printf("Enter the values of X and Nn");
scanf("%ld %ld",&x,&n);

xpown = power (x,n);

printf(" %d to the power %d = %ldn",x,n,xpown);
}

/*Recursive function to computer the X to power N*/

long int power(int x, int n)
{
if (n==1)
return(x);
else if ( n%2 == 0)
return (pow(power(x,n/2),2)); /*if n is even*/
else
return (x*power(x, n-1)); /* if n is odd*/
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System

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 strlen function

Write a c program to find the length of a string without using the built-in function strlen(). Program will calculate the length of the string using for loop by going through each character of the string, till its end. 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>

void main()
{
char string[50];
int i, length = 0;

printf("Enter a stringn");
gets(string);

for (i=0; string[i] != ''; i++) /*keep going through each */
{ /*character of the string */
length++; /*till its end */
}
printf("The length of a string is the number of characters in itn");
printf("So, the length of %s =%dn", string, length);
}
Read more Similar C Programs
Searching in C

C Strings

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 convert days to years, weeks and days

Write a C Program to convert given number of days to a measure of time given in years, weeks and days. For example 375 days is equal to 1 year 1 week and 3 days (ignore leap year).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>
#define DAYSINWEEK 7

void main()
{
int ndays, year, week, days;

printf("Enter the number of daysn");
scanf("%d",&ndays);

year = ndays/365;
week = (ndays % 365)/DAYSINWEEK;
days = (ndays%365) % DAYSINWEEK;

printf ("%d is equivalent to %d years, %d weeks and %d daysn",
ndays, year, week, days);
}
Read more Similar C Programs
Searching in C

C Strings

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 compute the surface area and volume of a cube

C Program to compute the surface area and volume of a cube. Program will accept the length of the sides and calculates surface area and volume of the cube. 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 <math.h>

void main()
{
float side, surfArea, volume;

printf("Enter the length of a siden");
scanf("%f", &side);

surfArea = 6.0 * side * side;

volume = pow (side, 3);

printf("Surface area = %6.2f and Volume = %6.2fn", surfArea, volume);
}
Read more Similar C Programs
Learn C Programming

Simple C Programs

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