Single linked list general implementation, with header
node (employee records)
/*
* Single linked list general implementation, with header node (employee records)
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <strings.h>
enum BOOL {FALSE, TRUE};
typedef int BOOL; // the more obvious choice would be char, but int is more
// time efficient as it uses a basic memory unit optimal for the
// particular processor design, by default
struct NODE;
struct EMPLOYEE;
typedef struct EMPLOYEE employee;
typedef struct NODE Node;
typedef Node* PtrNode;
typedef PtrNode List;
typedef PtrNode Position;
typedef employee DataType; // this is a default data type
struct EMPLOYEE{
char SNo[12];
char name[50];
int age;
char designation[50];
float salary;
};
BOOL compareEquality(char* e1, char* e2)
{
return (strcmp(e1, e2) == 0);
}
struct NODE{ // single link list node
DataType data;
PtrNode next;
};
BOOL IsLast(List L, Position pos)
{
return (pos->next == NULL);
}
PtrNode NewList() // returns the List's header
{
PtrNode tempNode = malloc(sizeof(Node));
tempNode->next = NULL;
return tempNode;
}
Position NewNode()
{
return malloc(sizeof(Node)); // for old compilers,
// explicit type conversion like (Node) is needed to be
//used.
}
void Delete(PtrNode node)
{
free(node);
}
void Insert ( List L, DataType data, Position pos)
{
Position tempPos;
tempPos = NewNode(); // creates and allocates memory space
if(tempPos == NULL)
{
fprintf(stderr, "error: request for allocation of memory using function \"malloc\" rejected");
return;
}
tempPos->data = data;
tempPos->next = pos->next;
pos->next = tempPos;
}
Position FindPrevious(List L, char element[12]) // by default, this function does not check for the
{ // header node which never gets initialised
Position tempPos = L;
while(tempPos->next != NULL && !compareEquality(tempPos->next->data.SNo, element)) /// searches for the first occurrence of value 'element'
tempPos = tempPos->next;
return tempPos; // returns the node preceding the node containing the value 'element'
}
void DeleteElement(List L, char data[12])
{
Position tempPos = FindPrevious(L, data); // by all chances, this cannot delete the list
PtrNode tempNode;
if(IsLast(L, tempPos)) // element not found
return;
tempNode = tempPos->next;
tempPos->next = tempNode->next;
Delete(tempNode);
}
Position Find(List L, char element[12])
{
Position tempPos = L->next;
while(tempPos!=NULL && !compareEquality(tempPos->data.SNo,element))
tempPos = tempPos->next;
return tempPos; // this return NULL by default when it fails to find the
// element
}
void DisplayElement(DataType element)
{
printf("\n\nserial no:%s", element.SNo);
printf("\nname:%s", element.name);
printf("\nsalary:%f", element.salary);
printf("\ndesignation:%s", element.designation);
printf("\nage:%d", element.age);
}
void Display (List L)
{
Position pos = L->next;
if(pos == NULL)
return;
while (pos != NULL)
{
DisplayElement(pos->data);
pos = pos->next;
}
}
employee getRecord()
{
employee e;
printf("\n\nserial no:");
gets(e.SNo); fflush(stdin);
printf("name:");
gets(e.name); fflush(stdin);
printf("salary:");
scanf("%f", &e.salary); fflush(stdin);
printf("designation:");
gets(e.designation); fflush(stdin);
printf("age:");
scanf("%d", &e.age); fflush(stdin);
return e;
}
int main()
{
printf("employee record manipulation\n");
List L = NewList();
Position top = L;
char data[12];
char ch = '\0';
while(ch != '5')
{
printf("\n1. enter record");
printf("\n2. find record");
printf("\n3. delete record");
printf("\n4. display all records");
printf("\n5. exit");
ch = getch();
if(ch == '1')
{
Insert(L, getRecord(), top);
top = top->next;
}
else if (ch == '2')
{
printf("\nenter SNo.");
gets(data);
DisplayElement(Find(L, data)->data);
}
else if(ch=='3')
{
printf("\nenter SNo.");
gets(data);
DeleteElement(L, data);
Display(L);
}
else if (ch == '4')
Display(L);
else if(ch == '5')
break;
else
printf("\ninvalid command");
}
return 0;
}
copyright (c) 2015 K Sreram.
About my blog