Code: Select all
typedef struct {
int x;
} example;
Code: Select all
struct example {
int x;
};
typedef struct example EXAMPLE;
Code: Select all
typedef struct {
int x;
} example;
Code: Select all
struct example {
int x;
};
typedef struct example EXAMPLE;
Code: Select all
typedef struct {
int x;
example *next;
} example; /* 'example' isn't declared when it is used */
Code: Select all
typedef struct example
{
int x;
struct example *next;
} EXAMPLE;
/* or */
typedef struct example EXAMPLE;
struct example
{
int x;
EXAMPLE *next;
};
Code: Select all
ll.c:65: error: dereferencing pointer to incomplete type
Code: Select all
struct->member = somevar;
Code: Select all
#include <stdio.h>
#include <stdlib.h>
struct rectangle {
???int x;
???int y;
???int area;
???
???struct rectangle *next;
???struct rectangle *prev;
};
typedef struct rectangle rectangle_t;
typedef struct rectangle_t *RECT;
RECT add(RECT head, int x, int y);
int main(void)
{
???RECT head = NULL;
???int i;
???int x;
???int y;
???int total;
???
???printf("How many rectangles to define?: ");
???scanf("%d", &total);
???
???for (i = 0; i < total; i++) {
??????printf("X value: ");
??????scanf("%d", &x);
??????printf("Y value: ");
??????scanf("%d", &y);
??????
??????head = add(head, x, y);
???}
??????
???return 0;
}
RECT add(RECT head, int x, int y)
{
???RECT new = NULL;
???
???if(!(new = (RECT) malloc(sizeof(rectangle_t)))) {
??????printf("Error allocating memory");
???}
???
???new->x = x;
???new->y = y;
???new->area = (x * y);
???
???if(head == NULL) {
??????head = new;
???} ?
/* Incomplete */
return head;?????
}
Code: Select all
struct rectangle {
int x;
int y;
int area;
struct rectangle *next;
struct rectangle *prev;
};
typedef struct rectangle rectangle_t;
typedef struct rectangle_t *RECT;
Code: Select all
typedef struct RECT{
int x;
int y;
int area;
struct RECT *next;
struct RECT *prev;
} RECT;
Code: Select all
RECT add(RECT head, int x, int y)
Code: Select all
RECT *add(RECT *head, int x, int y); // ?
Code: Select all
#include <stdio.h>
#include <stdlib.h>
struct rectangle {
???int x;??????/* Width */
???int y;??????/* Height */
???int area; /* Rectangle area (x * y) */
???
???struct rectangle *next;??????/* Double-linked */
???struct rectangle *prev;
} rectangle;
typedef struct rectangle *RECT;
RECT add(RECT head, int x, int y);
void display(RECT head);
int main(void)
{
???RECT head = NULL;
???int i;
???int x;
???int y;
???int total;
???
???printf("How many rectangles to define?: ");
???scanf("%d", &total);
???
???for (i = 0; i < total; i++) {
??????printf("Rectangle #%d\n", i);
??????printf("\tX value: ");
??????scanf("%d", &x);
??????printf("\tY value: ");
??????scanf("%d", &y);
??????
??????head = add(head, x, y);
???}
???
???display(head);
???printf("\n");
??????
???return 0;
}
RECT add(RECT head, int x, int y)
{
???RECT new = NULL;
???RECT tmp;?????????/* Used for looping */
???
???if (!(new = (RECT) malloc(sizeof(rectangle)))) {
??????printf("Error allocating memory");
??????exit(1);
???}
???
???new->x = x;
???new->y = y;
???new->area = (x * y);
???
???/* Don't forget we're working with a double-linked list */
??????
???if (head == NULL) {??????/* List empty; first node */
??????head = new;
??????new->next = NULL; /* Redundent */
??????new->prev = NULL;
???} else {
??????if (new->area < head->area) { /* Before first node */
?????????head->prev = new;
?????????new->next = head;
?????????head = new;
??????} else {??????????????????/* Needs to be added to the middle or end */
?????????tmp = head->next;
?????????
?????????if (tmp == NULL) {
????????????head->next = new;
????????????new->prev = head;
?????????} else {
????????????while (tmp->next != NULL) {
???????????????if (new->area < tmp->area) {
??????????????????new->next = tmp;
??????????????????new->prev = tmp->prev;
??????????????????tmp->prev->next = new;
??????????????????tmp->prev = new;
????????????
??????????????????break;
???????????????} else {
??????????????????tmp = tmp->next;
???????????????}
????????????}
????????????
????????????if (tmp->next == NULL) {?????????
???????????????if (new->area < tmp->area) {???/* Before last node */
??????????????????new->next = tmp;
??????????????????new->prev = tmp->prev;
??????????????????tmp->prev->next = new;
??????????????????tmp->prev = new;
???????????????} else {??????????????????/* Last node */
??????????????????tmp->next = new;
??????????????????new->prev = tmp;
??????????????????new->next = NULL;?????????/* Redundent */
???????????????}
????????????}
?????????}
??????}
???}
????????????
???return head;???
}
void display(RECT head)
{
???RECT current = head;
???int count = 0;
???
???if ( head == NULL ) { /* Something really when wrong in add() */
??????printf("List empty; nothing to display.");
??????exit(1);
???}
???while (current != NULL) {
??????printf("\nNode: %d\n", count++);
??????printf("\tX Value: %d\n", current->x);
??????printf("\tY Value: %d\n", current->y);
??????printf("\tArea: %d", current->area);
??????
??????current = current->next;
???}
}