Guest User

Untitled

a guest
Nov 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node
  6. {
  7. char ssn[20], name[20], dept[10], desg[10];
  8. int sal;
  9. long int phno;
  10. struct node *rlink;
  11. struct node *llink;
  12. };
  13. typedef struct node* NODE;
  14. NODE first=NULL;
  15. int count=NULL;
  16.  
  17. NODE createEmployeeNode()
  18. {
  19.  
  20. NODE employee;
  21. employee=(NODE)malloc(sizeof(struct node));
  22.  
  23. if(employee==NULL)
  24. {
  25. printf("Out of Memory\n");
  26. exit(0);
  27. }
  28. employee->rlink=NULL;
  29. employee->llink=NULL;
  30.  
  31. char ssn[20], name[20], dept[10], desg[10];
  32. int sal;
  33. long int phno;
  34.  
  35. printf("Enter SSN:\t");
  36. scanf("%s", &ssn);
  37. printf("Enter Name:\t");
  38. scanf("%s", &name);
  39. printf("Enter Department:\t");
  40. scanf("%s", &dept);
  41. printf("Enter Designation:\t");
  42. scanf("%s",&desg);
  43. printf("Enter Salary:\t");
  44. scanf("%d", &sal);
  45. printf("Enter Phone Number:\t");
  46. scanf("%ld", &phno);
  47.  
  48. strcpy(employee->ssn, ssn);
  49. strcpy(employee->name, name);
  50. strcpy(employee->dept, dept);
  51. strcpy(employee->desg, desg);
  52.  
  53. employee->sal=sal;
  54. employee->phno=phno;
  55.  
  56. count++;
  57. return employee;
  58. }
  59.  
  60. NODE insert_front()
  61. {
  62. NODE temp;
  63. temp=createEmployeeNode();
  64.  
  65. if(first==NULL)
  66. {
  67. return temp;
  68. }
  69.  
  70. temp->rlink=first;
  71. first->llink=temp;
  72.  
  73. return temp;
  74. }
  75.  
  76. NODE insert_end()
  77. {
  78. NODE cur, temp;
  79. temp=createEmployeeNode();
  80.  
  81. if(first==NULL)
  82. {
  83. return temp;
  84. }
  85. cur=first;
  86. while(cur->rlink!=NULL)
  87. {
  88. cur=cur->rlink;
  89. }
  90. cur->rlink=temp;
  91. temp->llink=cur;
  92.  
  93. temp->rlink=NULL;
  94.  
  95. return first;
  96.  
  97.  
  98.  
  99. }
  100.  
  101. NODE del_front()
  102. {
  103. NODE temp;
  104.  
  105. if(first==NULL)
  106. {
  107. printf("List is empty!\n");
  108. return NULL;
  109. }
  110.  
  111. if(first->rlink==NULL)
  112. {
  113. printf("The employee record with ssn number %d has been deleted\n", first->ssn );
  114. free(first);
  115. count--;
  116. return NULL;
  117. }
  118.  
  119. temp=first;
  120. temp->rlink=NULL;
  121. first->llink=NULL;
  122. printf("The employee record with ssn number %d has been deleted\n", temp->ssn );
  123. free(temp);
  124. count--;
  125.  
  126. return first;
  127.  
  128. }
  129.  
  130. NODE del_end()
  131. {
  132. NODE prev, cur, temp;
  133.  
  134. if(first==NULL)
  135. {
  136. printf("List is empty\n");
  137. return NULL;
  138. }
  139.  
  140. if(first->rlink==NULL)
  141. {
  142. printf("The employee record with ssn number %d has been deleted\n", first->ssn );
  143. free(first);
  144. count--;
  145. return NULL;
  146. }
  147.  
  148. prev=NULL;
  149. cur=first;
  150.  
  151. while(cur->rlink!=NULL)
  152. {
  153. prev=cur;
  154. cur=cur->rlink;
  155. }
  156.  
  157. cur->llink=NULL;
  158. prev->rlink=NULL;
  159.  
  160. count--;
  161. return first;
  162.  
  163. }
  164.  
  165. void display()
  166. {
  167. NODE cur;
  168.  
  169. if(first==NULL)
  170. {
  171. printf("The list is empty\n");
  172. }
  173.  
  174. cur=first;
  175. printf("The contents of the list are");
  176. while(cur!=NULL)
  177. {
  178. printf("SSN: %s| Name: %s| Department: %s| Designation: %s| Salary: %d| Phone: %ld",cur->ssn, cur->name, cur->dept, cur->desg, cur->sal, cur->phno);
  179. cur=cur->rlink;
  180. }
  181. printf("The number of employees: %d",count);
  182.  
  183. }
  184.  
  185. void main()
  186. {
  187. int ch,i,n;
  188. while(1)
  189. {
  190. printf("\t---Menu---\n");
  191. printf("1. Create DLL of employee\n");
  192. printf("2. Display\n");
  193. printf("3. Insert at Front\n");
  194. printf("4. Insert at End\n");
  195. printf("5. Delete at front\n");
  196. printf("6. Delete at end\n");
  197. printf("7. Exit\n");
  198.  
  199. printf("Enter your choice:\t");
  200. scanf("%d",&ch);
  201.  
  202. switch(ch)
  203. {
  204. case 1: printf("Enter the number of employees!\n");
  205. scanf("%d",&n);
  206. for(i=0; i<n; i++)
  207. first=insert_end();
  208. break;
  209.  
  210. case 2: display();
  211. break;
  212. case 3: first=insert_front();
  213. break;
  214. case 4: first=insert_end();
  215. break;
  216. case 5: first=del_front();
  217. break;
  218. case 6: first=del_end();
  219. break;
  220. case 7: exit(0);
  221. break;
  222. default: printf("Invalid choice\n");
  223. break;
  224.  
  225. }
  226.  
  227.  
  228. }
  229. }
Add Comment
Please, Sign In to add comment