C Program to implement STACK operations using Linked Lists

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 stack operations using 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 <stdlib.h>
typedef struct node {
int data;
struct node *link;
} NODE;

void Push(int);
int pop();
void Display();
NODE *top = NULL; /* Global Declarations */

main() {
/* Main Program */
int opn, elem;
do {
clrscr();
printf("n ### Linked List Implementation of 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 tobe pushed ?");
scanf("%d", &elem);
Push(elem);
break;
case 2:
elem = Pop();
if (elem != -1)
printf(" Deleted Node(From Top)with the Data: %dn", elem);
break;
case 3:
printf("Linked List Implementation of Stack: Status:n");
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);
}

void Push(int info) {
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL)
printf(" Out of Memory !! Overflow !!!");
else {
temp->data = info;
temp->link = top;
top = temp;
printf(" Node has been inserted at Top(Front) Successfully !!");
}
}

int Pop() {
int info;
NODE *t;
if (top == NULL) {
printf(" Underflow!!!");
return -1;
} else {
t = top;
info = top->data;
top = top->link;
t->link = NULL;
free(t);
return (info);
}
}

void Display() {
NODE *t;
if (top == NULL)
printf("Empty Stackn");
else {
t = top;
printf("Top->");
while (t) {
printf("[%d]->", t->data);
t = t->link;
}
printf("Nulln");
}
}
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

Linked List Operations

Data structures using C,
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 <stdlib.h>
#include <string.h>
typedef struct node {
int sid;
char sname[25];
int ssem;
struct node *link;
} NODE;

NODE *InsFront(NODE *, int, char *, int);
NODE *InsBack(NODE *, int, char *, int);
NODE *InsPos(NODE *, int, char *, int, int);
NODE *DelNode(NODE *, int);
NODE *SrchUpdate(NODE *, int);
void Display(NODE *);

main() {
NODE *start = NULL; /* Main Program */
int opn, id, sem, p, insopn;
char name[25];
do {
clrscr();
printf("n ### Linked List Operations ### nn");
printf("n Press 1-Insertion, 2-Deletion, 3-Search, 4-Display,5-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("Insertion at: Press 1->Front 2->Back 3->Pos ? ");
scanf("%d", &insopn);
printf("nnRead the Sid,Name, and Sem details ?");
scanf("%d%s%d", &id, name, &sem);
if (insopn == 1)
start = InsFront(start, id, name, sem);
else if (insopn == 2)
start = InsBack(start, id, name, sem);
else if (insopn == 3) {
printf(" At What Position ? ");
scanf("%d", &p);
start = InsPos(start, id, name, sem, p);
}
break;

case 2:
printf(" Read the Student Id of the Node to be deleted ? ");
scanf("%d", &id);
start = DelNode(start, id);
break;
case 3:
printf(" Read the Student Id of the Node to be Searched ? ");
scanf("%d", &id);
start = SrchUpdate(start, id);
break;
case 4:
printf(" Linked List is n");
Display(start);
break;
case 5:
printf("nn Terminating nn");
break;
default:
printf("nnInvalid Option !!! Try Again !! nn");
break;
}
printf("nnnn Press a Key to Continue . . . ");
getch();
} while (opn != 5);
}

NODE *InsFront(NODE *st, int id, char *name, int sem) {
NODE *temp;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (st);
} else {
temp->sid = id;
strcpy(temp->sname, name);
temp->ssem = sem;
temp->link = st;
printf(" Node has been inserted at Front Successfully !!");
return (temp);
}
}

NODE *InsBack(NODE *st, int id, char *name, int sem) {
NODE *temp, *t;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (st);
} else {
temp->sid = id;
strcpy(temp->sname, name);
temp->ssem = sem;
temp->link = NULL;
if (st == NULL)
return (temp);
else {
t = st;
while (t->link != NULL)
t = t->link;
t->link = temp;
printf(" Node has been inserted at Back Successfully !!");
return (st);
}
}
}

NODE *InsPos(NODE *st, int id, char *name, int sem, int pos) {
NODE *temp, *t, *prev;
int cnt;
temp = (NODE *) malloc(sizeof(NODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (st);
} else {
temp->sid = id;
strcpy(temp->sname, name);
temp->ssem = sem;
temp->link = NULL;
if (pos == 1) /* Front Insertion */
{
temp->link = st;
return (temp);
} else {
t = st;
cnt = 1;
while (t != NULL && cnt != pos) {
prev = t;
t = t->link;
cnt++;
}
if (t) /* valid Position Insert new node*/
{
prev->link = temp;
temp->link = t;
} else
printf(" Invalid Position !!!");
printf(" Node has been inserted at given Position Successfully !!");
return (st);
}
}
}
NODE *DelNode(NODE *st, int id) {
NODE *t, *prev;
if (st == NULL) {
printf(" Underflow!!!");
return (st);
} else {
t = st;
if (st->sid == id) /* Front Deletion */
{
st = st->link;
t->link = NULL;
free(t);
return (st);
} else {
while (t != NULL && t->sid != id) {
prev = t;
t = t->link;
}
if (t) /* node to be deleted found*/
{
prev->link = t->link;
t->link = NULL;
free(t);
} else
printf(" Invalid Student Id !!!");
return (st);
}
}
}

NODE *SrchUpdate(NODE *st, int id) {
NODE *t;
if (st == NULL) {
printf(" Empty List !!");
return (st);
} else {
t = st;
while (t != NULL && t->sid != id) {
t = t->link;
}
if (t) /* node to be Updated found*/
{
printf(" Node with Student Id %d found inthe List !n", id);
printf(" Read the New Id,Name and Sem forthe Studentn");
scanf("%d%s%d", t->sid, t->sname, t->ssem);
} else
printf(" Invalid Student Id !!!");
return (st);
}
}

void Display(NODE *st) {
NODE *t;
if (st == NULL)
printf("Empty Listn");
else {
t = st;
printf("Start->");
while (t) {
printf("[%d,%s,%d]->", t->sid, t->sname, t->ssem);
t = t->link;
}
printf("Nulln");
}
}

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 for Circular QUEUE Operations

Data structures using C,
C Program to implement circular queue. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front end. In circular queue is connected to end to end, i,e rear and front end are connected. Compare to normal queue, Circular queue is more advantages. 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 5 /* Size of Circular Queue */
int CQ[SIZE], f = -1, r = -1; /* Global declarations */

CQinsert(int elem) { /* Function for Insert operation */
if (CQfull())
printf("nn Overflow!!!!nn");
else {
if (f == -1)
f = 0;
r = (r + 1) % SIZE;
CQ[r] = elem;
}
}

int CQdelete() { /* Function for Delete operation */
int elem;
if (CQempty()) {
printf("nnUnderflow!!!!nn");
return (-1);
} else {
elem = CQ[f];
if (f == r) {
f = -1;
r = -1;
} /* Q has only one element ? */
else
f = (f + 1) % SIZE;
return (elem);
}
}

int CQfull() { /* Function to Check Circular Queue Full */
if ((f == r + 1) || (f == 0 && r == SIZE - 1))
return 1;
return 0;
}

int CQempty() { /* Function to Check Circular Queue Empty */
if (f == -1)
return 1;
return 0;
}

display() { /* Function to display status of Circular Queue */
int i;
if (CQempty())
printf(" n Empty Queuen");
else {
printf("Front[%d]->", f);
for (i = f; i != r; i = (i + 1) % SIZE)
printf("%d ", CQ[i]);
printf("%d ", CQ[i]);
printf("<-[%d]Rear", r);
}
}

main() { /* Main Program */
int opn, elem;
do {
clrscr();
printf("n ### Circular Queue Operations ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnRead the element to be Inserted ?");
scanf("%d", &elem);
CQinsert(elem);
break;
case 2:
elem = CQdelete();
if (elem != -1)
printf("nnDeleted Element is %d n", elem);
break;
case 3:
printf("nnStatus of Circular Queuenn");
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 for Simple/Linear QUEUE Operations

Data structures using C,
C Program to implement circular queue. Queue is a abstract data type, In which entities are inserted into the rear end and deleted from the front 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
***********************************************************/

/***********************************************************
* 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 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */

Qinsert(int elem)
{ /* Function for Insert operation */
if( Qfull()) printf("nn Overflow!!!!nn");
else
{
++r;
Q[r]=elem;
}
}

int Qdelete()
{ /* Function for Delete operation */
int elem;
if(Qempty()){ printf("nnUnderflow!!!!nn");
return(-1); }
else
{
elem=Q[f];
f=f+1;
return(elem);
}
}

int Qfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}

int Qempty()
{ /* Function to Check Queue Empty */
if(f > r) return 1;
return 0;
}

display()
{ /* Function to display status of Queue */
int i;
if(Qempty()) printf(" n Empty Queuen");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",Q[i]);
printf("<-Rear");
}
}

main()
{ /* Main Program */
int opn,elem;
do
{
clrscr();
printf("n ### Queue Operations ### nn");
printf("n Press 1-Insert, 2-Delete,3-Display,4-Exitn");
printf("n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("nnRead the element to be Inserted ?");
scanf("%d",&elem);
Qinsert(elem); break;
case 2: elem=Qdelete();
if( elem != -1)
printf("nnDeleted Element is %d n",elem);
break;
case 3: printf("nnStatus of Queuenn");
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 for Evaluation of Postfix Expression

C Program for Evaluation of Postfix ExpressionIn this program we evaluate the Postfix Expression, using the stack. For example, 456*+7- is the postfix expression, from left one by one it is inserted into the stack, and after evaluation the answer is 27. 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>
int s[SIZE];
int top=-1; /* Global declarations */

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

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

main()
{ /* Main Program */
char pofx[50],ch;
int i=0,op1,op2;
printf("nnRead the Postfix Expression ? ");
scanf("%s",pofx);
while( (ch=pofx[i++]) != '')
{
if(isdigit(ch)) push(ch-'0'); /* Push the operand */
else
{ /* Operator,pop two operands */
op2=pop();
op1=pop();
switch(ch)
{
case '+':push(op1+op2);break;
case '-':push(op1-op2);break;
case '*':push(op1*op2);break;
case '/':push(op1/op2);break;
}
}
}
printf("n Given Postfix Expn: %sn",pofx);
printf("n Result after Evaluation: %dn",s[top]);
}
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 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 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

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