Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

#include <stdio.

h>

#include <stdlib.h>

#include <string.h>

#define MAX 300

struct emp_data

int empno;

char empName[MAX];

char depart[MAX];

struct emp_data *next;

};

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

/* Function to insert a node at the front of the linked list. */

/* front: front pointer, id: employee ID, name: employee name */

/* depart: Employee department */

/* Returns the new front pointer. */

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

struct emp_data *insert(struct emp_data *front, int id, char name[],char depart[])

struct emp_data *temp;

temp = (struct emp_data*)malloc(sizeof(struct emp_data));

if (temp == NULL)

printf("\n Allocation failed \n");

exit(2);
}

p->empno = id;

strcpy(p->empName, name);

strcpy(p->department, depart);

p->next = front;

front = p ;

return(front);

/* End of insert() */

/* Function to display a node in a linked list */

void printNode(struct emp_data *p)

printf("\n Employee Details...\n");

printf("\n Emp No : %d", p->empno);

printf("\n Name : %s", p->empName);

printf("\n department : %s\n", p->department);

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_data* deleteNode(struct emp_data *front, int id)

struct emp_data *ptr;

struct emp_data *bptr;


if (front->empno == id)

ptr = front;

printf("\n Node 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("\n Node deleted:");

printNode(ptr);

bptr->next = ptr->next;

free(ptr);

return(front);

printf("\n Employee 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_data *front, int key)

struct emp_data *ptr;

for (ptr = front; ptr != NULL; ptr = ptr -> next)

if (ptr->empno == key)

printf("\n Key found:");

printNode(ptr);

return;

printf("\n Employee Number %d not found ", key);

/* End of search() */

/* Function to display the linked list */

void display(struct emp_data *front)

struct emp_data *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()

{
int ch;
printf("---------------------------------------------\n");

printf("Press 1 to INSERT a new record into the list \n");

printf("Press 2 to DELETE a record 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");
scanf("%d",&ch);
return ch;

/* End of menu() */

/* Function to select the option */

void main()

struct emp_data *linkList;

char name[21], desig[51];

char choice;

int eno;

linkList = NULL;

menu();

do

/* choose oeration to be performed */


switch(ch)

case '1':

printf("\n Enter the Employee Number : ");

scanf("%d", &eno);

printf("Enter the Employee name : ");

fflush(stdin);

gets(name);

printf("Enter the Employee department : ");

gets(desig);

linkList = insert(linkList, eno, name, desig);

break;

case '2':

printf("\n\n Enter the employee number to be deleted: ");

scanf("%d", &eno);

linkList = deleteNode(linkList, eno);

break;

case '3':

if (linkList == NULL)

printf("\n List empty.");

break;

display(linkList);

break;

case '4':

printf("\n\n Enter the employee number to be searched: ");

scanf("%d", &eno);
search(linkList, eno);

break;

case '5': break;

} while (choice != '5');

You might also like