Advertisement
erfanul007

phonebook project base

Nov 29th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4.  
  5. struct node{
  6. char name[100];
  7. char number[20];
  8. char about[1000];
  9. struct node *next;
  10. struct node *prev;
  11. }*start=NULL;
  12.  
  13.  
  14. void sort(struct node *newnode)
  15. {
  16. struct node *current;
  17.  
  18. current = start;
  19.  
  20. if(start == NULL)
  21. start = newnode;
  22.  
  23. else if(start->next == NULL){
  24.  
  25. if(current->name[0] <= newnode->name[0]){
  26. current->next = newnode;
  27. newnode -> prev = current;
  28. }
  29.  
  30. else{
  31. newnode->next = current;
  32. current->prev = newnode;
  33. start = newnode;
  34. }
  35. }
  36.  
  37. else{
  38. while(current->name[0] <= newnode->name[0]){
  39. if(current->next == NULL)
  40. break;
  41. current = current->next;
  42. }
  43.  
  44. if(current == start){
  45. newnode->next = current;
  46. current->prev = newnode;
  47. start = newnode;
  48. }
  49.  
  50. else if(current->next == NULL){
  51. current->next = newnode;
  52. newnode->prev = current;
  53. }
  54.  
  55. else{
  56. struct node *currentprev;
  57. currentprev = current->prev;
  58. currentprev->next = newnode;
  59. newnode->prev = currentprev;
  60. newnode->next = current;
  61. current->prev = newnode;
  62. }
  63. }
  64.  
  65. }
  66.  
  67.  
  68. void insert()
  69. {
  70. struct node *newnode,*current,*currentprev;
  71. newnode=(struct node*)malloc(sizeof(struct node));
  72. newnode->next=NULL;
  73. newnode->prev=NULL;
  74.  
  75. printf("name : ");
  76. getchar();
  77. scanf("%[^\n]",newnode->name);
  78.  
  79. printf("number: ");
  80. getchar();
  81. scanf("%s",newnode->number);
  82.  
  83. printf("about : ");
  84. getchar();
  85. scanf("%[^\n]",newnode->about);
  86.  
  87. sort(newnode);
  88.  
  89. printf("\n*save successful*\n");
  90. }
  91.  
  92.  
  93. void del(struct node *current)
  94. {
  95. if(current == start)
  96. start = current->next;
  97.  
  98. else if(current->next == NULL)
  99. (current->prev)->next = NULL;
  100.  
  101. else{
  102. struct node *currentprev, *currentnext;
  103. currentprev = current->prev;
  104. currentnext = current->next;
  105. currentprev->next = currentnext;
  106. currentnext->prev = currentprev;
  107. }
  108. }
  109.  
  110.  
  111. void edit(struct node *current)
  112. {
  113. del(current);
  114.  
  115. int n;
  116.  
  117. printf("1. Change name\n");
  118. printf("2. Skip\n");
  119. scanf("%d",&n);
  120. if(n == 1){
  121. printf("name : ");
  122. getchar();
  123. scanf("%[^\n]",current->name);
  124. }
  125.  
  126. printf("1. Change number\n");
  127. printf("2. Skip\n");
  128. scanf("%d",&n);
  129. if(n == 1){
  130. printf("number: ");
  131. getchar();
  132. scanf("%s",current->number);
  133. }
  134.  
  135. printf("1. Change about\n");
  136. printf("2. Skip\n");
  137. scanf("%d",&n);
  138. if(n == 1){
  139. printf("about : ");
  140. getchar();
  141. scanf("%[^\n]",current->about);
  142. }
  143.  
  144. current->next = NULL;
  145. current->prev = NULL;
  146.  
  147. sort(current);
  148.  
  149. }
  150.  
  151.  
  152. void byname()
  153. {
  154. struct node *current;
  155. char str[100];
  156. printf("Enter name for search : ");
  157. getchar();
  158. scanf("%[^\n]",str);
  159. current = start;
  160. int i=0, n;
  161.  
  162. while(1){
  163. if(strcmp(current->name,str)==0){
  164. printf("\nName found\n");
  165. printf("name : %s\n",current->name);
  166. printf("Number : %s\n",current->number);
  167. printf("about : %s\n",current->about);
  168. printf("\n");
  169. i++;
  170. break;
  171. }
  172.  
  173. if(current->next == NULL)
  174. break;
  175.  
  176. current = current->next;
  177. }
  178.  
  179. if(i==0)
  180. printf("\nNot Found\n");
  181.  
  182. else{
  183. printf("1. Edit\n");
  184. printf("2. Delete\n");
  185. printf("0. Home\n");
  186. printf("Chose an option : ");
  187. scanf("%d",&n);
  188.  
  189. if(n == 1){
  190. edit(current);
  191. printf("\n*edit successful*\n");
  192. }
  193. else if(n == 2){
  194. del(current);
  195. printf("\n*Delete successful*\n");
  196. }
  197. else if(n == 0)
  198. return;
  199. }
  200.  
  201. }
  202.  
  203.  
  204. void bynumber()
  205. {
  206. struct node *current;
  207. char str[100];
  208.  
  209. printf("Enter number for search : ");
  210. getchar();
  211. scanf("%[^\n]",str);
  212.  
  213. current = start;
  214. int i=0, n;
  215.  
  216. while(1){
  217. if(strcmp(current->number,str)==0){
  218. printf("\nNumber found\n");
  219. printf("name : %s\n",current->name);
  220. printf("Number : %s\n",current->number);
  221. printf("about : %s\n",current->about);
  222. printf("\n");
  223. i++;
  224. break;
  225. }
  226.  
  227. if(current->next == NULL)
  228. break;
  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. scanf("%d",&n);
  242.  
  243. if(n == 1)
  244. edit(current);
  245. else if(n == 2)
  246. del(current);
  247. else if(n == 0)
  248. return;
  249. }
  250.  
  251. }
  252.  
  253.  
  254. void count()
  255. {
  256. struct node *current;
  257. int count = 0;
  258. current = start;
  259.  
  260. while(1){
  261. count++;
  262. if(current->next == NULL)
  263. break;
  264. current = current->next;
  265. }
  266. printf("\nTotal contacts %d\n",count);
  267. }
  268.  
  269.  
  270. void displayall()
  271. {
  272.  
  273. if(start == NULL)
  274. printf("\nNo contact for display\n");
  275.  
  276. else{
  277. count();
  278. struct node *current,*currentprev;
  279. current = start;
  280. int i=0;
  281. while(1){
  282. i++;
  283. printf("\nContact no %d :-\n",i);
  284. printf("name : %s\n",current->name);
  285. printf("Number : %s\n",current->number);
  286. printf("about : %s\n",current->about);
  287. printf("\n");
  288.  
  289. if(current->next == NULL)
  290. break;
  291.  
  292. current = current->next;
  293. }
  294. }
  295. printf("\n");
  296. }
  297.  
  298.  
  299. void home()
  300. {
  301. int n;
  302. printf("\nChose an option : ");
  303. scanf("%d",&n);
  304.  
  305. if(n==0)
  306. exit(0);
  307.  
  308. else if(n==1)
  309. insert();
  310.  
  311. else if(n==2)
  312. displayall();
  313.  
  314. else if(n==3)
  315. byname();
  316.  
  317. else if(n==4)
  318. bynumber();
  319.  
  320. }
  321.  
  322.  
  323. int main()
  324. {
  325. int n;
  326.  
  327. while(1){
  328. printf("\n***HOME***\n\n");
  329. printf("1. Save a new contact\n");
  330. printf("2. Show all contact\n");
  331. printf("3. Search by name\n");
  332. printf("4. Search by number\n");
  333. printf("0. exit\n");
  334. home();
  335. }
  336. return 0;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement