C Program to search the linked list.

Data structures using C,
Write c program to search the linked list.
Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* 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>
#include<malloc.h>
struct l_list
{
int info;
struct link *next;
}
start, *node;

int search(int);
void main()
{
int no,i,item,pos;
clrscr();
start.next=NULL;
node=&start;
printf("How many nodes, you want in linked list? ");
scanf("%d",&no);
printf("");
for(i=0;i<no;i++)
{
node->next=(struct l_list *)malloc(sizeof(struct l_list));
printf("Enter element in node %d: ",i+1);
scanf("%d",&node->info);
node=node->next;
}
node->next=NULL;
printf("Linked list(only with info field) is:");

node=&start;
while(node->next!=NULL)
{
printf("%d ",node->info);
node=node->next;
}
printf("

Enter item to be searched : ");
scanf("%d",&item);
pos=search(item);
if(pos<=no)
printf("Your item is at node %d",pos);
else
printf("Sorry! item is no in linked list.a");
getch();
}

int search(int item)
{
int n=1;
node=&start;
while(node->next!=NULL)
{
if(node->info==item)
break;
else
n++;
node=node->next;
}
return 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!

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

C Program to reverse a Linked List.

Data structures using C,
C Program to reverse a 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 reverse the linked list, but without sorting 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>

#define MAX 100

struct leftnode {
int num;
struct leftnode *next;
};


void linklist_add(struct leftnode **n, int val);

void linklist_reverse(struct leftnode **n);

void linklist_display(struct leftnode *n);

int main(void) {
struct leftnode *new = NULL;
int i = 0;

for(i = 0; i <= MAX; i++)
linklist_add(&new, i);

printf("Linked list before reversal:n");
linklist_display(new);
linklist_reverse(&new);
printf("Linked list after reversal:n");
linklist_display(new);

return 0;
}


void linklist_add(struct leftnode **n, int val) {
struct leftnode *temp = NULL;
temp = malloc(sizeof(struct leftnode));
temp->num = val;
temp->next = *n;
*n = temp;
}


void linklist_reverse(struct leftnode **n) {
struct leftnode *a = NULL;
struct leftnode *b = NULL;
struct leftnode *c = NULL;
a = *n, b = NULL;

while(a != NULL) {
c = b, b = a, a = a->next;
b->next = c;
}

*n = b;
}

void linklist_display(struct leftnode *n) {
while(n != NULL)
printf(" %d", n->num), n = n->next;

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!

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

C Program to sort a linked list.

Data structures using C, Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. 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>
#define NULL 0

struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node;

main ()
{
int n;
node *head = NULL;
void print(node *p);
node *insert_Sort(node *p, int n);

printf("Input the list of numbers.n");
printf("At end, type -999.n");
scanf("%d",&n);

while(n != -999)
{
if(head == NULL) /* create 'base' node */
{
head = (node *)malloc(sizeof(node));
head ->number = n;
head->next = NULL;

}

else /* insert next item */
{
head = insert_Sort(head,n);
}
scanf("%d", &n);
}
printf("n");
print(head);
print("n");
}
node *insert_Sort(node *list, int x)
{
node *p1, *p2, *p;
p1 = NULL;
p2 = list; /* p2 points to first node */

for( ; p2->number < x ; p2 = p2->next)
{
p1 = p2;

if(p2->next == NULL)
{
p2 = p2->next; /* p2 set to NULL */
break; /* insert new node at end */
}
}

/* key node found */
p = (node *)malloc(sizeof(node)); /* space for new node */
p->number = x; /* place value in the new node */
p->next = p2; /* link new node to key node */
if (p1 == NULL)
list = p; /* new node becomes the first node */
else
p1->next = p; /* new node inserted after 1st node */
return (list);
}
void print(node *list)
{
if (list == NULL)
printf("NULL");
else
{
printf("%d-->",list->number);
print(list->next);
}
return;
}
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 implement Doubly Linked List Operations

Data structures using C, Linked list is a data structure in which the objects are arranged in a linear order. In Doubly Linked List, each node contains three fields, one is to store data and other two fields stores the reference(address) of the previous and next node. 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 dnode {
int data;
struct dnode *next, *prev;
} DNODE;

DNODE *InsFront(DNODE *, int);
DNODE *InsBefore(DNODE *, int, int);
DNODE *DelNode(DNODE *, int);
void Display(DNODE *);

main() {
DNODE *start = NULL; /* Main Program */
int opn, elem, info, n, i;
do {
clrscr();
printf("n ### Doubly Linked List Operations ### nn");
printf("n Press 1-Creation with front Insertion");
printf("n 2-Insert Before a Given Node");
printf("n 3-Delete a Given Node");
printf("n 4-Display");
printf("n 5-Exitn");
printf("n Your option ? ");
scanf("%d", &opn);
switch (opn) {
case 1:
printf("nnHow Many Nodes ?");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("nRead the Data for Node %d ?", i);
scanf("%d", &elem);
start = InsFront(start, elem);
}
printf("nDoubly Linked list with %d nodes is ready toUse!!n", n);
break;
case 3:
printf(" Read the Info of the Node to be deleted ? ");
scanf("%d", &info);
start = DelNode(start, info);
break;
case 2:
printf(" Read the Data for New noden");
scanf("%d", &elem);
printf(
" Read the Info of the Node(to the left of which new node tobe inserted ? ");
scanf("%d", &info);
start = InsBefore(start, elem, info);
break;
case 4:
printf(" Doubly 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);
}

DNODE *InsFront(DNODE *start, int elem) {
DNODE *temp;
temp = (DNODE *) malloc(sizeof(DNODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (start);
} else {
temp->data = elem;
temp->prev = NULL;
temp->next = start;
start->prev = temp;
printf(" New Node has been inserted at Front Successfully !!");
return (temp);
}
}

DNODE *InsBefore(DNODE *start, int elem, int info) {
DNODE *temp, *t;
temp = (DNODE *) malloc(sizeof(DNODE));
if (temp == NULL) {
printf(" Out of Memory !! Overflow !!!");
return (start);
} else {
temp->data = elem;
temp->next = NULL;
temp->prev = NULL;

if (start->data == info) /* Front Insertion */
{
temp->next = start;
start->prev = temp;
return (temp);
} else {
t = start;
while (t != NULL && t->data != info)
t = t->next;
if (t->data == info) /* Node found */
{
temp->next = t;
temp->prev = t->prev;
t->prev->next = temp;
t->prev = temp;
} else
printf(" Node not found,Invalid Info !!!");
return (start);
}
}
}

DNODE *DelNode(DNODE *start, int info) {
DNODE *t;
if (start == NULL) {
printf(" Underflow!!!");
return (start);
} else {
t = start;
if (start->data == info) /* Front Deletion */
{
start = start->next;
start->prev = NULL;
t->next = NULL;
free(t);
return (start);
} else {
while (t != NULL && t->data != info)
t = t->next;
if (t->data == info) /* node to be deleted found*/
{
t->prev->next = t->next;
t->next->prev = t->prev;
t->next = t->prev = NULL;
free(t);
} else
printf("Node not found, Invalid Info !!");
return (start);
}
}
}

void Display(DNODE *start) {
DNODE *t;
if (start == NULL)
printf("Empty Listn");
else {
t = start;
printf("Forward Traversal nn Start->");
while (t) {
printf("[%d]->", t->data);
t = t->next;
}
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

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 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 create a linked list and display the elements in the list.

Data structures using C,
C program to create a linked list and display the elements in the 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. 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()
{
struct node
{
int num;
struct node *ptr;
};

typedef struct node NODE;

NODE *head, *first, *temp=0;
int count = 0;
int choice = 1;
first = 0;

while(choice)
{
head =(NODE*) malloc(sizeof(NODE));
printf("Enter the data itemn");
scanf("%d", &head-> num);

if(first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
fflush(stdin);
printf("Do you want to continue(Type 0 or 1)?n");
scanf("%d", &choice);

} /* End of while */

temp->ptr = 0;
temp = first; /* reset temp to the beginning*/
printf("nstatus of the linked list isn");

while(temp!=0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULLn");
printf("No. of nodes in the list = %dn", count);
} /* End of 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