c - Creating a header for a doubly linked list -
so i'm taking course of study on operating systems, , we've started semester crash course of study in c programming. our first 2 assignments easy can't life of me figure 1 out.
so our professor had ta create next .c file:
#include <stdio.h> #include <stdlib.h> #include "dll.h" void init(dll *dll, int n){ node *prev = &(dll->head); prev->prev = null; node *node; for(int i=0;i<n;i++){ node = (node *)malloc(sizeof(node)); node->value = rand(); node->prev = prev; node->next = null; prev->next = node; prev = node; printf("%d. ", i+1); printf("%d\n", node->value); } } void print(dll *dll){ node *current; current = dll->head.next; while(current->next != null){ printf("%d\n", current->value); current = current->next; } printf("%d\n", current->value); } void sort(dll *dll){ int changed = 1; while(changed==1){ node *current; current = dll->head.next; changed = 0; while(current->next != null){ if(current->value > current->next->value){ int value; value = current->value; current->value = current->next->value; current->next->value = value; changed = 1; } current = current->next; } } } int main(){ dll dll; init(&dll, 10); print(&dll); sort(&dll); printf("\nsorted:\n"); print(&dll); homecoming 0; } and our assignment create header file should implement run .c file. i've spent day working on , best can come is:
#include <stdio.h> #include <stdlib.h> typedef struct dll { dll *prev; dll *next; int value; } dll; void *init(dll *dll, int n); void sort(dll *dll); void print(dll *dll); whenever run .h next errors in command line (we're using gcc on linux):
in file included dll.c:5:0: dll.h:5:5: error: unknown type name ‘dll’ dll.h:6:5: error: unknown type name ‘dll’ dll.h:10:1: error: unknown type name ‘dll’ dll.h:12:12: error: unknown type name ‘dll’ dll.h:13:11: error: unknown type name ‘dll’ dll.h:14:12: error: unknown type name ‘dll’ dll.c:7:11: error: unknown type name ‘dll’ dll.c:22:12: error: unknown type name ‘dll’ dll.c:31:11: error: unknown type name ‘dll’ dll.c: in function ‘main’: dll.c:51:2: error: unknown type name ‘dll’ i used other examples of these types of header files create mine, reason cannot figure out. appreciate help guys can give me.
*edit: * when alter name of "dll" gives me:
in file included dll.c:5:0: dll.h:5:5: error: unknown type name ‘dll’ dll.h:6:5: error: unknown type name ‘dll’ dll.h:10:1: error: unknown type name ‘type’ dll.h:12:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token dll.c:7:6: error: conflicting types ‘init’ dll.h:14:7: note: previous declaration of ‘init’ here dll.c: in function ‘init’: dll.c:8:2: error: unknown type name ‘node’ dll.c:8:20: error: ‘dll’ has no fellow member named ‘head’ dll.c:9:6: error: request fellow member ‘prev’ in not construction or union dll.c:10:2: error: unknown type name ‘node’ dll.c:11:2: error: ‘for’ loop initial declarations allowed in c99 mode dll.c:11:2: note: utilize alternative -std=c99 or -std=gnu99 compile code dll.c:12:11: error: ‘node’ undeclared (first utilize in function) dll.c:12:11: note: each undeclared identifier reported 1 time each function appears in dll.c:12:17: error: expected look before ‘)’ token dll.c:13:7: error: request fellow member ‘value’ in not construction or union dll.c:14:7: error: request fellow member ‘prev’ in not construction or union dll.c:15:7: error: request fellow member ‘next’ in not construction or union dll.c:16:7: error: request fellow member ‘next’ in not construction or union dll.c:19:22: error: request fellow member ‘value’ in not construction or union dll.c: in function ‘print’: dll.c:23:2: error: unknown type name ‘node’ dll.c:24:15: error: ‘dll’ has no fellow member named ‘head’ dll.c:25:15: error: request fellow member ‘next’ in not construction or union dll.c:26:25: error: request fellow member ‘value’ in not construction or union dll.c:27:20: error: request fellow member ‘next’ in not construction or union dll.c:29:24: error: request fellow member ‘value’ in not construction or union dll.c: in function ‘sort’: dll.c:34:3: error: unknown type name ‘node’ dll.c:35:16: error: ‘dll’ has no fellow member named ‘head’ dll.c:37:16: error: request fellow member ‘next’ in not construction or union dll.c:38:14: error: request fellow member ‘value’ in not construction or union dll.c:38:31: error: request fellow member ‘next’ in not construction or union dll.c:40:20: error: request fellow member ‘value’ in not construction or union dll.c:41:12: error: request fellow member ‘value’ in not construction or union dll.c:41:29: error: request fellow member ‘next’ in not construction or union dll.c:42:12: error: request fellow member ‘next’ in not construction or union dll.c:45:21: error: request fellow member ‘next’ in not construction or union
that should be:
typedef struct _node{ struct _node *next; struct _node *prev; int value; }node; typedef struct _dll{ node head; }; typo in typedef.. dll not same dll
also note, there no head in struct _dll
also, please allow me know node is.
c gcc header doubly-linked-list
No comments:
Post a Comment