Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- typedef struct Node
- {
- char Name[80], Phone[80], Email[80];
- struct Node* leftChild;
- struct Node* rightChild;
- struct Node* parent;
- }Node;
- Node* root;
- Node* makeNode(char *s1, char* s2, char* s3)
- {
- Node*p = (Node*)malloc(sizeof(Node));
- strcpy(p->Name,s1);
- strcpy(p->Phone,s2);
- strcpy(p->Email,s3);
- p->parent = NULL;
- p->leftChild = NULL;
- p->rightChild = NULL;
- return p;
- }
- void addNode(Node*p)
- {
- if(root == NULL)
- {
- root = p;
- return;
- }
- Node*q = root, *prev_q = NULL;
- int cmp;
- while(1)
- {
- if(q == NULL)
- {
- q = p;
- if(prev_q != NULL)
- {
- if(cmp<0)
- prev_q->rightChild = q;
- if(cmp>0)
- prev_q->leftChild = q;
- q->parent = prev_q;
- return;
- }
- cmp = strcmp(q->Name,p->Name);
- if(cmp == 0)
- return;
- if(cmp<0)
- {
- prev_q = q;
- q = q->rightChild;
- return;
- }
- if(cmp>0)
- {
- prev_q = q;
- q = q->leftChild;
- return;
- }
- }
- }
- }
- Node* findNode(char* Name, Node*p)
- {
- if(p == NULL)
- return NULL;
- int cmp;
- cmp = strcmp(p->Name,Name);
- Node* q;
- if(cmp == 0)
- return p;
- if(cmp > 0)
- {
- q = findNode(Name, p->leftChild);
- return q;
- }
- if(cmp < 0)
- {
- q = findNode(Name, p->rightChild);
- return q;
- }
- }
- void printbst(Node* p)
- {
- if(p == NULL)
- return;
- printf("%s %s %s", p->Name, p->Phone, p->Email);
- printbst(p->leftChild);
- printbst(p->rightChild);
- return;
- }
- int main()
- {
- FILE *f;
- f = fopen("phonebook.txt", "r");
- if (f == NULL)
- {
- printf("ERROR\n");
- }
- else
- {
- char s1[80], s2[80], s3[80];
- do{
- fscanf(f, "%s %s %s", s1, s2, s3);
- Node* newNode = makeNode(s1,s2,s3);
- addNode(newNode);
- }while(!feof(f));
- }
- printbst(root);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement