Advertisement
erfanul007

Project - phone book (Doubly)

Dec 6th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. /*Phone Book*/
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6.  
  7. struct node{
  8. char name[100];
  9. char number[20];
  10. char about[1000];
  11. struct node *next;
  12. struct node *prev;
  13. }*start=NULL;
  14.  
  15.  
  16. void sort(struct node *newnode)
  17. {
  18. struct node *current,*currentprev,*temp;
  19.  
  20. current = start;
  21.  
  22. if(start == NULL)
  23. start = newnode;
  24.  
  25. else if(start->next == NULL){
  26.  
  27. if(strcmp(newnode,current)>0){
  28. current->next = newnode;
  29. newnode -> prev = current;
  30. }
  31.  
  32. else{
  33. newnode->next = current;
  34. current->prev = newnode;
  35. start = newnode;
  36. }
  37. }
  38.  
  39. else{
  40. while(current!=NULL){
  41. if(strcmp(newnode,current)<0)
  42. break;
  43. if(current->next==NULL)
  44. temp=current;
  45. current = current->next;
  46. }
  47.  
  48. if(current == start){
  49. newnode->next = current;
  50. current->prev = newnode;
  51. start = newnode;
  52. }
  53.  
  54. else if(current == NULL){
  55. temp->next = newnode;
  56. newnode->prev = temp;
  57. }
  58.  
  59. else{
  60. currentprev = current->prev;
  61. currentprev->next = newnode;
  62. newnode->prev = currentprev;
  63. newnode->next = current;
  64. current->prev = newnode;
  65. }
  66. }
  67.  
  68. }
  69.  
  70.  
  71. int checkernum(struct node *newnode)
  72. {
  73. int i=0;
  74. struct node *current;
  75. current=start;
  76. while(current!=NULL){
  77. if(strcmp(current->number,newnode->number)==0){
  78. i++;
  79. break;
  80. }
  81. current=current->next;
  82. }
  83. return i;
  84. }
  85.  
  86.  
  87. int checkername(struct node *newnode)
  88. {
  89. int i=0;
  90. struct node *current;
  91. current=start;
  92. while(current!=NULL){
  93. if(strcmp(current->name,newnode->name)==0){
  94. i++;
  95. break;
  96. }
  97. current=current->next;
  98. }
  99. return i;
  100. }
  101.  
  102. void insert()
  103. {
  104. struct node *newnode,*current;
  105. newnode=(struct node*)malloc(sizeof(struct node));
  106. newnode->next=NULL;
  107. newnode->prev=NULL;
  108.  
  109. printf("name : ");
  110. getchar();
  111. scanf("%[^\n]",newnode->name);
  112.  
  113. printf("number: ");
  114. getchar();
  115. scanf("%s",newnode->number);
  116.  
  117. printf("about : ");
  118. getchar();
  119. scanf("%[^\n]",newnode->about);
  120.  
  121. int i=checkername(newnode);
  122. int j=checkernum(newnode);
  123. if(i==0 && j==0){
  124. sort(newnode);
  125. printf("\n*save successful*\n");
  126. }
  127. else{
  128. printf("\nContact Exist, can't save it\n");
  129. }
  130. }
  131.  
  132.  
  133. void del(struct node *current)
  134. {
  135. if(current == start)
  136. start = current->next;
  137.  
  138. else if(current->next == NULL)
  139. (current->prev)->next = NULL;
  140.  
  141. else{
  142. struct node *currentprev, *currentnext;
  143. currentprev = current->prev;
  144. currentnext = current->next;
  145. currentprev->next = currentnext;
  146. currentnext->prev = currentprev;
  147. }
  148. }
  149.  
  150.  
  151. void edit(struct node *current)
  152. {
  153. del(current);
  154.  
  155. char n;
  156.  
  157. printf("1. Change name\n");
  158. printf("2. Skip\n");
  159.  
  160. getchar();
  161. scanf("%c",&n);
  162.  
  163. if(n == '1'){
  164. printf("name : ");
  165. getchar();
  166. scanf("%[^\n]",current->name);
  167. }
  168.  
  169. printf("1. Change number\n");
  170. printf("2. Skip\n");
  171.  
  172. getchar();
  173. scanf("%c",&n);
  174.  
  175. if(n == '1'){
  176. printf("number: ");
  177. getchar();
  178. scanf("%s",current->number);
  179. }
  180.  
  181. printf("1. Change about\n");
  182. printf("2. Skip\n");
  183.  
  184. getchar();
  185. scanf("%c",&n);
  186.  
  187. if(n == '1'){
  188. printf("about : ");
  189. getchar();
  190. scanf("%[^\n]",current->about);
  191. }
  192.  
  193. current->next = NULL;
  194. current->prev = NULL;
  195.  
  196. sort(current);
  197.  
  198. }
  199.  
  200.  
  201. void byname()
  202. {
  203. struct node *current;
  204.  
  205. if(start==NULL){
  206. printf("\nNo Contact saved for search\n");
  207. return;
  208. }
  209.  
  210. char str[100];
  211. printf("Enter name for search : ");
  212. getchar();
  213. scanf("%[^\n]",str);
  214. current = start;
  215.  
  216. int i=0;
  217. char n;
  218.  
  219. while(current!=NULL){
  220. if(strcmp(current->name,str)==0){
  221. printf("\nName found\n");
  222. printf("name : %s\n",current->name);
  223. printf("Number : %s\n",current->number);
  224. printf("about : %s\n",current->about);
  225. printf("\n");
  226. i++;
  227. break;
  228. }
  229.  
  230. current = current->next;
  231. }
  232.  
  233. if(i==0)
  234. printf("\nNot Found\n");
  235.  
  236. else{
  237. printf("1. Edit\n");
  238. printf("2. Delete\n");
  239. printf("0. Home\n");
  240. printf("Chose an option : ");
  241.  
  242. getchar();
  243. scanf("%c",&n);
  244.  
  245. if(n == '1'){
  246. edit(current);
  247. printf("\n*edit successful*\n");
  248. }
  249. else if(n == '2'){
  250. del(current);
  251. printf("\n*Delete successful*\n");
  252. }
  253. else if(n == '0')
  254. return;
  255. }
  256.  
  257. }
  258.  
  259.  
  260. void bynumber()
  261. {
  262. struct node *current;
  263.  
  264. if(start==NULL){
  265. printf("\nNo Contact saved for search\n");
  266. return;
  267. }
  268.  
  269. char str[100];
  270.  
  271. printf("Enter number for search : ");
  272. getchar();
  273. scanf("%[^\n]",str);
  274.  
  275. current = start;
  276.  
  277. int i=0;
  278. char n;
  279.  
  280. while(current!=NULL){
  281. if(strcmp(current->number,str)==0){
  282. printf("\nNumber found\n");
  283. printf("name : %s\n",current->name);
  284. printf("Number : %s\n",current->number);
  285. printf("about : %s\n",current->about);
  286. printf("\n");
  287. i++;
  288. break;
  289. }
  290. current = current->next;
  291. }
  292.  
  293. if(i==0)
  294. printf("\nNot Found\n");
  295.  
  296. else{
  297. printf("1. Edit\n");
  298. printf("2. Delete\n");
  299. printf("0. Home\n");
  300. printf("Chose an option : ");
  301.  
  302. getchar();
  303. scanf("%c",&n);
  304.  
  305. if(n == '1')
  306. edit(current);
  307. else if(n == '2')
  308. del(current);
  309. else if(n == '0')
  310. return;
  311. }
  312.  
  313. }
  314.  
  315.  
  316. void count()
  317. {
  318. struct node *current;
  319. int count = 0;
  320. current = start;
  321.  
  322. while(current!=NULL){
  323. count++;
  324. current = current->next;
  325. }
  326. printf("\nTotal contacts %d\n",count);
  327. }
  328.  
  329.  
  330. void displayall()
  331. {
  332.  
  333. if(start == NULL)
  334. printf("\nNo contact for display\n");
  335.  
  336. else{
  337. count();
  338. struct node *current;
  339. current = start;
  340. int i=0;
  341. while(current!=NULL){
  342. i++;
  343.  
  344. printf("\nContact no %d :-\n",i);
  345. printf("name : %s\n",current->name);
  346. printf("Number : %s\n",current->number);
  347. printf("about : %s\n",current->about);
  348. printf("\n");
  349.  
  350. current = current->next;
  351. }
  352. }
  353. printf("\n");
  354. }
  355.  
  356.  
  357. void home()
  358. {
  359. char n;
  360. printf("\nChose an option : ");
  361. getchar();
  362. scanf("%c",&n);
  363.  
  364. if(n=='0')
  365. exit(0);
  366.  
  367. else if(n=='1')
  368. insert();
  369.  
  370. else if(n=='2')
  371. displayall();
  372.  
  373. else if(n=='3')
  374. byname();
  375.  
  376. else if(n=='4')
  377. bynumber();
  378. else
  379. return;
  380. }
  381.  
  382.  
  383. int main()
  384. {
  385. char n[100],x[100];
  386. strcpy(x,"12345");
  387. printf("\n***PHONE BOOK***\n\n");
  388. while(1){
  389. printf("\nEnter PIN: ");
  390. scanf("%s",n);
  391.  
  392. if(strcmp(n,x)!=0){
  393. printf("Wrong password\n");
  394. continue;
  395. }
  396. printf("Password matched - WELCOME\n");
  397. while(1){
  398. printf("\n***HOME***\n\n");
  399. printf("1. Save a new contact\n");
  400. printf("2. Show all contact\n");
  401. printf("3. Search by name\n");
  402. printf("4. Search by number\n");
  403. printf("0. exit\n");
  404. home();
  405. }
  406. }
  407. return 0;
  408. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement