Code:
/*********** lap9 - linked list ***********/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/*********** Data ***********/
typedef struct {
int id;
char name[60];
int age;
float salary;
char pos[30];
}data;
/*********** linkd list ***********/
typedef struct node{
data emp;
struct node *next;
}*ptr,recnode;
/*********** The functions ***********/
void menu();
void Read(data*);
void insert_first(ptr*,data);
void insert_last(ptr*,data);
void insert_mid(ptr*,data);
int size(ptr);
void print(ptr);
void sorted(ptr*);
void search(ptr,int,ptr*,ptr*);
void search_By_name(ptr,char[],ptr*,ptr*);
void delet(ptr*,int,data *);
void del_first(ptr*,data *);
void del_last(ptr*,data *);
void max_salary(ptr,ptr*);
void rev(ptr*);
void del_freq(ptr*);
/*********** main ***********/
int main(){
int choice,t;
data R;
ptr head=NULL;
ptr cur,pre,p;
char a[60];
menu();
scanf("%d",&choice);
while(choice!=14){
switch(choice){
case 1:
Read(&R);
insert_first(&head,R);
break;
case 2:
Read(&R);
insert_last(&head,R);
break;
case 3:
Read(&R);
insert_mid(&head,R);
break;
case 4:
print(head);
break;
case 5:
sorted(&head);
break;
case 6:
del_first(&head,&R);
menu();
break;
case 7:
del_last(&head,&R);
menu();
break;
case 8:
printf("Eneter the id you want to delet :");
scanf("%d",&t);
delet(&head,t,&R);
menu();
break;
case 9:
printf("Enter the id to you finds :");
scanf("%d",&t);
search(head,t,&cur,&pre);
if(cur==NULL)
printf("Sorry not found\n");
else
printf("The Position in the list is %s\n",cur->emp.pos);
menu();
break;
case 10:
p=head;
max_salary(head,&p);
if(p!=NULL)
printf("\nThe Id is: %d , Name: %s , Age: %d , Salary: %.2f , Position: %s\n",p->emp.id,p->emp.name,p->emp.age,p->emp.salary,p->emp.pos);
menu();
break;
case 11:
rev(&head);
menu();
break;
case 12:
printf("Enter The name you want to change Id: ");
getchar();
gets(a);
search_By_name(head,a,&cur,&pre);
if(cur==NULL)
printf("Sorry not found the name\n");
else{
printf("\n");
printf("Enter the new id :");
scanf("%d",&cur->emp.id);}
menu();
break;
case 13:
del_freq(&head);
menu();
break;
default:
printf("Erorr choice\n");
menu();
break;
}
scanf("%d",&choice);
}
return 0;
}
/*********** menu ***********/
void menu(){
printf("+------------------------------------------------------+");
printf("\n| | The main menu | |");
printf("\n+------------------------------------------------------+");
printf("\n| 1- Inserts a new employee in the head of list |");
printf("\n| 2- Inserts a new employee in the last. |");
printf("\n| 3- Inserts a new employee in the middle of the list. |");
printf("\n| 4- Print all the employees and thir attributes. |");
printf("\n| 5- Sorts the employees according to thir salaries. |");
printf("\n| 6- Delet a first employee. |");
printf("\n| 7- Delet a last employee. |");
printf("\n| 8- Delet a given employee. |");
printf("\n| 9- Finds a given employee and returns his position. |");
printf("\n| 10- Prints the with maximum salary. |");
printf("\n| 11- Revers the linked list. |");
printf("\n| 12- chang Id. |");
printf("\n| 13- Delet Frequency By Id. |");
printf("\n| 14- End. |");
printf("\n+------------------------------------------------------+");
printf("\nEnter your choice :");
}
/*********** Read Record ***********/
void Read(data *R){
printf("\n+-------------------------------+");
printf("\n| Enter the info of employees |");
printf("\n+-------------------------------+");
printf("\nEnter the id number :");
scanf("%d",&R->id);
printf("\n");
printf("Enter the name (first - middle -last):");
scanf("%s",R->name);
printf("\n");
getchar();
printf("Enter the age :");
scanf("%d",&R->age);
printf("\n");
printf("Enter the salary :");
scanf("%f",&R->salary);
printf("\n");
printf("Enter the position :");
scanf("%s",R->pos);
printf("\n");
getchar();
menu();
}
/*********** insert_first ***********/
void insert_first(ptr *head,data R){
ptr p;
p=(struct node*)malloc(sizeof(recnode));
p->emp=R;
if(*head==NULL){
*head=p;
(*head)->next=NULL;
}
else{
p->next=*head;
*head=p;
}
}
/*********** Print linkedlist ***********/
void print(ptr head){
while(head!=NULL){
printf("\nThe Id is: %d , Name: %s , Age: %d , Salary: %.2f , Position: %s",head->emp.id,head->emp.name,head->emp.age,head->emp.salary,head->emp.pos);
head=head->next;
}
printf("\n");
menu();
}
/*********** insert_last ***********/
void insert_last(ptr *head,data R){
ptr p,q;
p=(struct node*)malloc(sizeof(recnode));
p->emp=R;
p->next=NULL;
if(*head==NULL)
*head=p;
else{
q=*head;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
}
/*********** size ***********/
int size(ptr head){
int n=0;
while(head!=NULL){
n++;
head=head->next;
}
return n;
}
/*********** insert_mid ***********/
void insert_mid(ptr*head,data R){
ptr p,q,r;
int n,k;
n=size(*head);
p=(struct node*)malloc(sizeof(recnode));
p->emp=R;
if(*head==NULL){
*head=p;
(*head)->next=NULL;
}
else if((*head)->next==NULL){
(*head)->next=p;
p->next=NULL;
}
else{
k=n/2;
for(q=*head;k>0;k--)
q=q->next;
k=(n/2)-1;
for(r=*head;k>0;k--)
r=r->next;
p->next=q;
r->next=p;
}
}
/*********** sort ***********/
void sorted(ptr *head){
ptr p,q;
data t;
int n,j,i;
if(*head==NULL)
printf("sorry no linked list to sort\n");
else{
p=*head;
q=(*head)->next;
n=size(*head);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if((p->emp.salary)<(q->emp.salary)){
t=p->emp;
p->emp=q->emp;
q->emp=t;
}
q=q->next;
p=p->next;
}
p=*head;
q=(*head)->next;
}}
menu();
}
/*********** search ***********/
void search(ptr head,int target,ptr *cur,ptr *pre){
*cur=*pre=NULL;
while((head!=NULL) && (*cur==NULL)){
if((head->emp.id)==target)
*cur=head;
else{
*pre=head;
head=head->next;
}
}
}
/*********** Delet ***********/
void delet(ptr *head,int t,data *Z){
ptr cur,pre;
search(*head,t,&cur,&pre);
if(cur==NULL)
printf("Sorry the id :%d not found \n",t);
else{
if(cur==*head)
del_first(head,Z);
else{
if(cur->next==NULL)
del_last(head,Z);
else{
pre->next=cur->next;
*Z=cur->emp;
free(cur);
}
}
}
}
/*********** Delet First ***********/
void del_first(ptr *head,data *Z){
ptr p;
if(*head==NULL)
printf("Sorry the linked list is empty\n");
else{
if((*head)->next==NULL){
*Z=(*head)->emp;
free(*head);
}
else{
p=*head;
*Z=p->emp;
*head=(*head)->next;
free(p);
}
}
}
/*********** Delet last ***********/
void del_last(ptr *head,data *Z){
ptr p,q;
if(*head==NULL)
printf("Sorry the linked list is empty\n");
else{
if((*head)->next==NULL){
*Z=(*head)->emp;
free(*head);
}
else{
p=*head;
q=(*head)->next;
while(q->next!=NULL){
q=q->next;
p=p->next;
}
*Z=q->emp;
p->next=NULL;
free(q);
}
}
}
/*********** Max salary ***********/
void max_salary(ptr head,ptr *cur){
ptr p;
float max;
if(head==NULL)
printf("Sorry Empty linked linst\n");
else{
max=head->emp.salary;
p=head->next;
while(p!=NULL){
if(max<p->emp.salary){
max=p->emp.salary;
*cur=p;
}
p=p->next;
}
}
}
/*********** Revers ***********/
void rev(ptr *head){
ptr p,q;
data t;
int i,n,j;
p=*head;
q=(*head)->next;
n=size(*head);
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
t=p->emp;
p->emp=q->emp;
q->emp=t;
q=q->next;
p=p->next;
}
q=(*head)->next;
p=*head;
}
}
/*********** search ***********/
void search_By_name(ptr head,char a[],ptr *cur,ptr *pre){
*cur=*pre=NULL;
while((head!=NULL) && (*cur==NULL)){
if(strcmp(head->emp.name,a)==0)
*cur=head;
else{
*pre=head;
head=head->next;
}
}
}
/*********** Delet Frequency By Id***********/
void del_freq(ptr *head){
ptr p,q;data Z;
int i,j,n=size(*head);
p=*head;
q=(*head)->next;
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(q->emp.id==p->emp.id){
del_first(&p,&Z);
n--;
}
q=q->next;
}
q=p->next;
}
}
/*********** End ***********/