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 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 demonstrate time functions.

C Program to demonstrate time functions. time.h library function is used to get and manipulate date and time functions. 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> /* NULL */
#include <time.h> /* ctime, asctime */

main()
{
time_t now; /* define 'now'. time_t is probably
* a typedef */

/* Calender time is the number of
* seconds since 1/1/1970 */

now = time((time_t *)NULL); /* Get the system time and put it
* into 'now' as 'calender time' */

printf("%s", ctime(&now)); /* Format data in 'now'
* NOTE that 'ctime' inserts a
* 'n' */

/*********************************************************************/

/* Here is another way to extract the time/date information */

time(&now);

printf("%s", ctime(&now)); /* Format data in 'now' */

/*********************************************************************/

{
struct tm *l_time;

l_time = localtime(&now); /* Convert 'calender time' to
* 'local time' - return a pointer
* to the 'tm' structure. localtime
* reserves the storage for us. */
printf("%s", asctime(l_time));
}

/*********************************************************************/

time(&now);
printf("%s", asctime(localtime( &now )));

/*********************************************************************/

{
struct tm *l_time;
char string[20];

time(&now);
l_time = localtime(&now);
strftime(string, sizeof string, "%d-%b-%yn", l_time);
printf("%s", string);
}


}
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 demonstrate macros.

C Program to demonstrate macros. Macro is a piece of text that is expanded by the preprocessor part of the compiler. This is used in to expand text before compiling. Macro definitions (#define), and conditional inclusion (#if). In many C implementations, it is a separate program invoked by the compiler as the first part of translation.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 SQUARE(x) x*x

main()
{
int value=3;

printf("%d n", SQUARE(value));
}
Read more Similar C Programs
Learn C Programming

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 demonstrate goto statement

C Program to demonstrate goto statement . goto statement jumps the control from one point to another locally(within a function). In this program we perform a division checking for divide by zero by using the goto statement.

The Program

#include <stdio.h>
#include <stdlib.h>
main()
{
char data[100];
double num1, num2;
printf("\nPlease enter a number ==> " );
gets(data);
num1 = atof(data);
printf("\nPlease enter a number ==> " );
gets(data);
num2 = atof(data);
/* Stop a divide by zero with
* the goto statement.
*/
if ( num2 == 0.0 ) goto end_prog;
printf("\n%4.2f divided by %4.2f is %4.2f\n", num1, num2, num1/num2);
end_prog:
printf("\nProgram ended");
}
view raw goto.c hosted with ❤ by GitHub

Sample Output

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

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

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