C Program to implementing two Stacks on Single Array.

Data structures using C, Stack is a data structure in which the objects are arranged in a non linear order. In stack, elements are aded or deleted from only one end, i.e. top of the stack. In this program, we implement the two stacks using the single array. 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 10 /* Size of Stack */
int s[SIZE],top[3]={0,-1,SIZE};
/* Global declarations */
push(int elem,int stno)
{
int pos; /* Function for PUSH operation */
if( Sfull()) printf("nn Overflow!!!!nn");
else
{
if(stno==1) pos= ++top[stno];
else pos=--top[stno];
s[pos]=elem;
}
}


int pop(int stno)
{ /* Function for POP operation */
int elem,pos;
if(Sempty(stno)){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
pos=top[stno];
elem=s[pos];
if(stno == 1)top[stno]--;
else
top[stno]++;
return(elem);
}
}

int Sfull()
{ /* Function to Check Stack Full */
if(top[1] == top[2]-1) return 1;
return 0;
}

int Sempty(stno)
{
/* Function to Check Stack Empty */
switch(stno)
{
case 1: if(top[1] == -1) return 1; else return 0;
case 2: if(top[2] == SIZE) return 1;else return 0;
}
}

display(int stno)
{ /* Function to display status of Stack */
int i;
if(Sempty(stno)) printf(" n Empty Stackn");
else
{
if(stno == 1)
{
for(i=0;i<=top[stno];i++)
printf("%dn",s[i]);
printf("^Top");
}
else
{
for(i=SIZE-1;i>=top[stno];i--)
printf("%dn",s[i]);
printf("^Top");
}
}
}

main()
{ /* Main Program */
int opn,elem,stno;
do
{
clrscr();
printf("n ### Stack Operations ### nn");
printf("n Stack Number (1,2): ");
scanf("%d",&stno);
printf("n Press 1-Push, 2-Pop,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);

switch(opn)
{
case 1: printf("nnRead the element to be pushed ?");
scanf("%d",&elem);
push(elem,stno); break;
case 2: elem=pop(stno);
if( elem != -1)
printf("nnPopped Element is %d n",elem);
break;
case 3: printf("nnStatus of Stack %d nn",stno);
display(stno); break;
case 4: printf("nn Terminating nn"); break;
default: printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
}while(opn != 4);
}


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!

To browse more C Programs visit this link
(c) www.c-program-example.com

C Program for Stack Operations using arrays.

Data structures using C,
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. Here we implement the PUSH, POP, DISPLAY stack operations using the array. 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 SIZE 5 /* Size of Stack */
int s[SIZE], top = -1; /* Global declarations */

push(int elem) { /* Function for PUSH operation */
if (Sfull())
printf("nn Overflow!!!!nn");
else {
++top;
s[top] = elem;
}
}

int pop() { /* Function for POP operation */
int elem;
if (Sempty()) {
printf("nnUnderflow!!!!nn");
return (-1);
} else {
elem = s[top];
top--;
return (elem);
}
}

int Sfull() { /* Function to Check Stack Full */
if (top == SIZE - 1)
return 1;
return 0;
}

int Sempty() { /* Function to Check Stack Empty */
if (top == -1)
return 1;
return 0;
}

display() { /* Function to display status of Stack */
int i;
if (Sempty())
printf(" n Empty Stackn");
else {
for (i = 0; i <= top; i++)
printf("%dn", s[i]);
printf("^Top");
}
}

main() { /* Main Program */
int opn, elem;
do {
clrscr();
printf("n ### Stack Operations ### nn");
printf("n Press 1-Push, 2-Pop,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the element to be pushed ?");
scanf("%d", &elem);
push(elem);
break;
case 2:
elem = pop();
if (elem != -1)
printf("nnPopped Element is %d n", elem);
break;
case 3:
printf("nnStatus of Stacknn");
display();
break;
case 4:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 4);
}

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 convert IP address to 32-bit long int

C Program to convert IP address to 32-bit long int. Internet Protocol Address is the unique number assigned to the each device, which is connected to the computer network. Previously we are using IPv4 versions, now we are using IPv6. Here we read the ip address using unions, and converted that to the 32-bit long int. 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 <string.h>
#include <stdlib.h>
union iptolint
{
char ip[16];
unsigned long int n;
};
unsigned long int conv(char []);

main()
{
union iptolint ipl;
printf(" Read the IP Address to be convertedn");
scanf("%s",ipl.ip);
ipl.n=conv(ipl.ip);
printf(" Equivalent 32-bit long int is : %lun",ipl.n);
}

unsigned long int conv(char ipadr[])
{
unsigned long int num=0,val;
char *tok,*ptr;
tok=strtok(ipadr,".");
while( tok != NULL)
{
val=strtoul(tok,&ptr,0);
num=(num << 8) + val;
tok=strtok(NULL,".");
}
return(num);
}

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