Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- unsigned n;
- struct node
- {
- char *key;
- struct node *next, *prev;
- };
- void addBack(struct node *root,struct node *end, char *newkey)
- {
- struct node *newnode;
- newnode = malloc(sizeof(struct node));
- end->next = newnode;
- newnode->key = newkey;
- newnode->prev = end;
- newnode->next = root;
- }
- struct node *createList(FILE *fp)
- {
- char *name = malloc(512 * sizeof(char));
- struct node *root = malloc(sizeof(struct node));
- fscanf(fp, "%s", name);
- root->key = name;
- root->next = root->prev = NULL;
- struct node *end = root;
- for(int i = 1 ; i < n; i++)
- {
- fscanf(fp , "%s", name);
- addBack(root, end, name);
- end = end->next;
- }
- return root;
- }
- void printList(struct node *root)
- {
- printf("%s ", root->key);
- printf("\n");
- }
- int main()
- {
- FILE *fp;
- fp = fopen("name.txt", "r");
- printf("Enter the number of students:\n");
- scanf("%u", &n);
- struct node *root = createList(fp);
- printList(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment