fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. struct node
  5. {
  6. int iSSN;
  7. char cName[30],cDept[10],cDesignation[30],cPhNo[11];
  8. int iSalary;
  9. struct node *plink;
  10. struct node *nlink;
  11. };
  12. typedef struct node *NODEPTR;
  13. NODEPTR fnGetNode(void);
  14. void fnFreeNode(NODEPTR);
  15. NODEPTR fnInsRear(NODEPTR);
  16. NODEPTR fnDelFront(NODEPTR);
  17. NODEPTR fnInsFront(NODEPTR);
  18. NODEPTR fnDelRear(NODEPTR);
  19. void fnDisplay(NODEPTR);
  20. int main()
  21. {
  22. NODEPTR first=NULL;
  23. int iChoice,iNum,i;
  24. printf("\n Enter the number of Employees N:");
  25. scanf("%d",&iNum);
  26. for(i=0;i<iNum;i++)
  27. {
  28. printf("\n Enter Data for node %d:\n",i+1);
  29. first=fnInsRear(first);
  30. }
  31. for(;;)
  32. {
  33. printf("\n DLL OPERATIONS\n");
  34. printf("=========");
  35. printf("\n 1. Insert Rear \n 2. Delete Front\n 3. Insert Front \n 4. Delete Rear \n 5. Display \n 6. Exit \n");
  36. printf("\n Enter your choice\n");
  37. printf("\n Enter the number of Employees N:");
  38. scanf("%d",&iNum);
  39. for(i=0;i<iNum;i++)
  40. {
  41. printf("\n Enter Data for node %d:scanf("%d",&iChoice);
  42. switch(iChoice)
  43. {
  44. case 1:
  45. first=fnInsRear(first);
  46. break;
  47. case 2:
  48. first=fnDelFront(first);
  49. break;
  50. case 3:
  51. first=fnInsFront(first);
  52. break;
  53. case 4:
  54. first=fnDelRear(first);
  55. break;
  56. case 5:
  57. fnDisplay(first);
  58. break;
  59. case 6:
  60. exit(0);
  61. }
  62. }
  63. return 0;
  64. }
  65. NODEPTR fnGetNode()
  66. {
  67. NODEPTR newborn;
  68. newborn=(NODEPTR)malloc(sizeof(struct node));
  69. if(newborn==NULL)
  70. {
  71. printf("n Memory overflow");
  72. exit(0);
  73. }
  74. printf("\n Enter SSN:");
  75. scanf("%d",&newborn->iSSN);
  76. printf("\n Enter name:");
  77. scanf("%s",newborn->cName);
  78. printf("\n enter department:");
  79. scanf("%s",newborn->cDept);
  80. printf("\n Enter designation:");
  81. scanf("%s",newborn->cDesignation);
  82. printf("\n Enter salary:");
  83. scanf("%d",&newborn->iSalary);
  84. printf("\n Enter Phone No:");
  85. scanf("%s",newborn->cPhNo);
  86. return newborn;
  87. }
  88. void fnFreeNode(NODEPTR x)
  89. {
  90. free(x);
  91. }
  92. NODEPTR fnInsRear(NODEPTR firprintf("\n Enter SSN:");
  93. scanf("%d",&newborn->iSSN);
  94. printf("\n Enter name:");
  95. scanf("%s",newborn->cName);
  96. printf("\n enter department:");
  97. scanf("%s",newborn->cDept);
  98. printf("\n Enter designation:");
  99. scanf("%s",newborn->cDesignation);
  100. printf("\n Enter salary:");
  101. scanf("%d",&newborn->iSalary);
  102. printf("\n Enter Phone No:");
  103. scanf("%s",newborn->cPhNo);st)
  104. {
  105. NODEPTR temp,cur;
  106. temp=fnGetNode();
  107. temp->plink=temp->nlink=NULL;
  108. if(first==NULL)
  109. return temp;
  110. cur=first;
  111. while(cur->nlink!=NULL)
  112. {
  113. cur=cur->nlink;
  114. }
  115. cur->nlink=temp;
  116. temp->plink=cur;
  117. return first;
  118. }
  119. NODEPTR fnInsFront(NODEPTR first)
  120. {
  121. NODEPTR temp;
  122. temp=fnGetNode();
  123. temp->plink=temp->nlink=NULL;
  124. temp->nlink=first;
  125. first=temp;
  126. return first;
  127. }
  128. NODEPTR fnDelRear(NODEPTR first)
  129. {
  130. NODEPTR cur,prev;
  131. if(first==NULL)
  132. {
  133. printf("\nDLL is empty\n");
  134. return first;
  135. }
  136. cur=first;
  137. if(cur->nlink==NULL)
  138. {
  139. printf("\n Node deleted for %s\n",cur->cName);
  140. fnFreeNode(cur);
  141. return NULL;
  142. }
  143. while(cur->nlink!=NULL)
  144. {
  145. cur=cur->nlink;
  146. }
  147. prev=cur->plink;
  148. prev->nlink=NULL;
  149. printf("\n Node deleted for %s\n",cur->cName);
  150. fnFreeNode(cur);
  151. return first;
  152. }
  153. NODEPTR fnDelFront(NODEPTR first)
  154. {
  155. NODEPTR temp;
  156. if(first==NULL)
  157. {
  158. printf("\n DLL is empty\n");
  159. return first;
  160. }
  161. if(first->nlink==NULL)
  162. {
  163. printf("\n Node deleted for %s\n", first->cName);
  164. fnFreeNode(first);
  165. return NULL;
  166. }
  167. temp=first;
  168. first=first->nlink;
  169. first->plink=NULL;
  170. printf("\n Node deleted for %s\n",temp->cName);
  171. fnFreeNode(temp);
  172. return first;
  173. }
  174. void fnDisplay(NODEPTR first)
  175. {
  176. NODEPTR curr;
  177. int count=0;
  178. if(first==NULL)
  179. {
  180. printf("\n DLL is empty\n");
  181. return;
  182. }
  183. printf("\n The contents of DLL are:\n");
  184. currprintf("\n Enter the number of Employees N:");
  185. scanf("%d",&iNum);
  186. for(i=0;i<iNum;i++)
  187. {
  188. printf("\n Enter Data for node %d:=first;
  189. printf("\n");
  190. printf("\n SSN \t Name\tDept\tDesignation\tSalary\tPhone No");
  191. while(curr!=NULL)
  192. {
  193. printf("\n%-5d\t%s\t%s\t%s\t%-7d\t%-11s",curr->iSSN,curr->cName,curr->cDept,curr->cDesignation,curr->iSalary,curr->cPhNo);
  194. curr=curr->nlink;
  195. }
  196. printf("\n\nDLL has %d nodes\n",count);
  197. }
Success #stdin #stdout 0.02s 25896KB
stdin
Standard input is empty
stdout
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int iSSN;
char cName[30],cDept[10],cDesignation[30],cPhNo[11];
int iSalary;
struct node *plink;
struct node *nlink;
};
typedef struct node *NODEPTR;
NODEPTR fnGetNode(void);
void fnFreeNode(NODEPTR);
NODEPTR fnInsRear(NODEPTR);
NODEPTR fnDelFront(NODEPTR);
NODEPTR fnInsFront(NODEPTR);
NODEPTR fnDelRear(NODEPTR);
void fnDisplay(NODEPTR);
int main()
{
NODEPTR first=NULL;
int iChoice,iNum,i;
printf("\n Enter the number of Employees N:");
scanf("%d",&iNum);
for(i=0;i<iNum;i++)
{
printf("\n Enter Data for node %d:\n",i+1);
first=fnInsRear(first);
}
for(;;)
{
printf("\n DLL OPERATIONS\n");
printf("=========");
printf("\n 1. Insert Rear \n 2. Delete Front\n 3. Insert Front \n 4. Delete Rear \n 5. Display \n 6. Exit \n");
printf("\n Enter your choice\n");
printf("\n Enter the number of Employees N:");
scanf("%d",&iNum);
for(i=0;i<iNum;i++)
{
printf("\n Enter Data for node %d:scanf("%d",&iChoice);
switch(iChoice)
{
case 1:
first=fnInsRear(first);
break;
case 2:
first=fnDelFront(first);
break;
case 3:
first=fnInsFront(first);
break;
case 4:
first=fnDelRear(first);
break;
case 5:
fnDisplay(first);
break;
case 6:
exit(0);
}
}
return 0;
}
NODEPTR fnGetNode()
{
NODEPTR newborn;
newborn=(NODEPTR)malloc(sizeof(struct node));
if(newborn==NULL)
{
printf("n Memory overflow");
exit(0);
}
printf("\n Enter SSN:");
scanf("%d",&newborn->iSSN);
printf("\n Enter name:");
scanf("%s",newborn->cName);
printf("\n enter department:");
scanf("%s",newborn->cDept);
printf("\n Enter designation:");
scanf("%s",newborn->cDesignation);
printf("\n Enter salary:");
scanf("%d",&newborn->iSalary);
printf("\n Enter Phone No:");
scanf("%s",newborn->cPhNo);
return newborn;
}
void fnFreeNode(NODEPTR x)
{
free(x);
}
NODEPTR fnInsRear(NODEPTR firprintf("\n Enter SSN:");
scanf("%d",&newborn->iSSN);
printf("\n Enter name:");
scanf("%s",newborn->cName);
printf("\n enter department:");
scanf("%s",newborn->cDept);
printf("\n Enter designation:");
scanf("%s",newborn->cDesignation);
printf("\n Enter salary:");
scanf("%d",&newborn->iSalary);
printf("\n Enter Phone No:");
scanf("%s",newborn->cPhNo);st)
{
NODEPTR temp,cur;
temp=fnGetNode();
temp->plink=temp->nlink=NULL;
if(first==NULL)
return temp;
cur=first;
while(cur->nlink!=NULL)
{
cur=cur->nlink;
}
cur->nlink=temp;
temp->plink=cur;
return first;
}
NODEPTR fnInsFront(NODEPTR first)
{
NODEPTR temp;
temp=fnGetNode();
temp->plink=temp->nlink=NULL;
temp->nlink=first;
first=temp;
return first;
}
NODEPTR fnDelRear(NODEPTR first)
{
NODEPTR cur,prev;
if(first==NULL)
{
printf("\nDLL is empty\n");
return first;
}
cur=first;
if(cur->nlink==NULL)
{
printf("\n Node deleted for %s\n",cur->cName);
fnFreeNode(cur);
return NULL;
}
while(cur->nlink!=NULL)
{
cur=cur->nlink;
}
prev=cur->plink;
prev->nlink=NULL;
printf("\n Node deleted for %s\n",cur->cName);
fnFreeNode(cur);
return first;
}
NODEPTR fnDelFront(NODEPTR first)
{
NODEPTR temp;
if(first==NULL)
{
printf("\n DLL is empty\n");
return first;
}
if(first->nlink==NULL)
{
printf("\n Node deleted for %s\n", first->cName);
fnFreeNode(first);
return NULL;
}
temp=first;
first=first->nlink;
first->plink=NULL;
printf("\n Node deleted for %s\n",temp->cName);
fnFreeNode(temp);
return first;
}
void fnDisplay(NODEPTR first)
{
NODEPTR curr;
int count=0;
if(first==NULL)
{
printf("\n DLL is empty\n");
return;
}
printf("\n The contents of DLL are:\n");
currprintf("\n Enter the number of Employees N:");
scanf("%d",&iNum);
for(i=0;i<iNum;i++)
{
printf("\n Enter Data for node %d:=first;
printf("\n");
printf("\n SSN \t Name\tDept\tDesignation\tSalary\tPhone No");
while(curr!=NULL)
{
printf("\n%-5d\t%s\t%s\t%s\t%-7d\t%-11s",curr->iSSN,curr->cName,curr->cDept,curr->cDesignation,curr->iSalary,curr->cPhNo);
curr=curr->nlink;
count++;
}
printf("\n\nDLL has %d nodes\n",count);
}