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 info@c-program-example.com
* 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 info@c-program-example.com
* 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 info@c-program-example.com
* 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 info@c-program-example.com
* 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 info@c-program-example.com
* 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++]) != '