// main.cpp // linklist #define _CRT_SECURE_NO_DEPRECATE #include #include "stdafx.h" #include struct Node { int empno; char name[10]; float salary; int married; Node *link; }; Node *first = 0; void Add(); Node* Find(int); void Delete(int); void Display(void); void Clear(void); int Menue(void); void Sort(void); int main() { int i; while (1) { switch (Menue()) { case 1: Add(); break; case 2: scanf("%d", &i); Find(i); break; case 3: scanf("%d", &i); Delete(i); break; case 4: Display(); break; case 5: Clear(); break; case 6: Sort(); break; case 7: return 0; } } } //******************** int Menue(void) { int i; printf("**********************************\n"); printf("1.Add Employee\n"); printf("2.Find Employee By EmpNo\n"); printf("3.Delete Employee By EmpNo\n"); printf("4.Display Employee\n"); printf("5.Clear List\n"); printf("6.Sort List\n"); printf("7.exit\n"); printf("Enter your choose:"); scanf("%d", &i); printf("**********************************\n"); return i; } //******************** void Add() { int n; printf("Enter count:"); scanf("%d", &n); for (int k = 0; k < n; k++) { Node *temp; temp = (Node *)malloc(sizeof Node); int d=0; printf("**********************************\n"); printf("Enter EmpNo:"); scanf("%d", &temp->empno); printf("Enter Name:"); scanf("%s", &temp->name); printf("Enter Salary:"); scanf("%f", &temp->salary); printf("Enter Marreid:"); scanf("%d", &temp->married); printf("**********************************\n"); temp->link = first; first = temp; } } //******************** Node* Find(int EmpNo) { Node *temp = first; while (temp) { if (EmpNo == temp->empno) return temp; temp = temp->link; } return 0; } //******************** void Delete(int EmpNo) { if (!first) return; Node *temp = first; if (first->empno == EmpNo) { first = first->link; return; } Node *cur = first->link; while (temp->link) { if (cur->empno == EmpNo) { temp->link = cur->link; return; } temp = cur; cur = cur->link; } } //******************** void Display(void) { Node *temp = first; while (temp) { printf("**********************************\n"); printf("%d-%s-%f-%d\n", temp->empno,temp->name,temp->salary,temp->married); printf("**********************************\n"); temp = temp->link; } printf("\n"); } //******************** void Clear(void) { Node *temp, *cur = first; first = 0; } //******************** void Sort(void) { int swapped, i; struct Node *ptr1; struct Node *lptr = NULL; /* Checking for empty list */ if (first == NULL) return; do { swapped = 0; ptr1 = first; while (ptr1->link != lptr && ptr1->link!=NULL) { if (ptr1->empno > ptr1->link->empno) { Node *temp = (Node *)malloc(sizeof Node); temp->empno = ptr1->empno; temp->married = ptr1->married; *temp->name = *ptr1->name; temp->salary = ptr1->salary; temp->link = ptr1->link; ptr1->empno = ptr1->link->empno; ptr1->married = ptr1->link->married; *ptr1->name = *ptr1->link->name; ptr1->salary = ptr1->link->salary; ptr1->link = ptr1->link->link; ptr1->link->empno = temp->empno; ptr1->link->married = temp->married; *ptr1->link->name = *temp->name; ptr1->link->salary = temp->salary; ptr1->link->link = temp->link; swapped = 1; } ptr1 = ptr1->link; } lptr = ptr1; } while (swapped); }