Zdravim,
chcel by som sa spytat ze kde moze byt problem, totizto pisem do skoly ulohy ale stale mi vypisuje ze "dereferencing pointer to incomplete type"... hladal som pomoc na nete, ale nejaky vseobecny postup som nenasiel...
Ked bude dakto ochotny sa na to pozriet tak vdaka dopredu...
fatal.h
#include <stdio.h>
#include <stdlib.h>
#define Error( Str ) FatalError( Str )
#define FatalError( Str ) fprintf( stderr, "%s\n", Str ), system("PAUSE"), exit( 1 )
list.h
#ifndef _List_H
#define _List_H
struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
typedef int TElement;
List MakeEmpty ( List L );
int IsEmpty ( List L );
int IsLast ( Position P, List L );
Position Find ( TElement X, List L );
void Delete ( TElement X, List L );
Position FindPrevious( TElement X, List L );
void Insert ( TElement X, List L, Position P );
void DeleteList ( List L );
Position Header ( List L );
Position First ( List L );
Position Advance ( Position P );
TElement Retrieve ( Position P );
List Cat ( List L1, List L2 );
List Cut ( List L, Position P );
#endif /* _List_H */
test.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void
PrintList( const List L )
{
Position P = Header( L );
if( IsEmpty( L ) )
printf( "Empty list\n" );
else
{
do
{
P = Advance( P );
printf( "%d ", Retrieve( P ) );
} while( !IsLast( P, L ) );
printf( "\n" );
}
}
int main(void)
{
List L;
Position P;
int i;
L = MakeEmpty( NULL );
P = Header( L );
PrintList( L );
for( i = 0; i < 10; i++ )
{
Insert( i, L, P );
PrintList( L );
P = Advance( P );
}
for( i = 0; i < 10; i+= 2 )
Delete( i, L );
for( i = 0; i < 10; i++ )
if( ( i % 2 == 0 ) == ( Find( i, L ) != NULL ) )
printf( "Find fails\n" );
printf( "Finished deletions\n" );
PrintList( L );
DeleteList( L );
printf("\n\n== TESTING OPERATION CAT ==\n");
List list1 = NULL,
list2 = NULL;
Position p1,
p2;
list1 = MakeEmpty(list1);
list2 = MakeEmpty(list2);
p1 = Header(list1);
p2 = Header(list2);
int count;
for (count = 0; count < 5; count++) {
Insert(count, NULL, p1);
Insert(count + 5, NULL, p2);
}
printf("\n== Before operation CAT ==\n");
printf("Zoznam p1:\n");
while (p1 != NULL) {
printf(" %d", Retrieve(p1));
p1 = p1->Next;
}
printf("\n\nZoznam p2:\n");
while (p2 != NULL) {
printf(" %d", Retrieve(p2));
p2 = p2->Next;
}
Cat(list1, list2);
printf("\n\n== After operation CAT ==\n");
printf("Zoznam p1:\n");
while (p1 != NULL) {
printf(" %d", Retrieve(p1));
//p1 = p1->Next;
}
printf("\n\n== TESTING OPERATION CUT ==\n");
system("PAUSE");
return 0;
}
list.c
#include "list.h"
#include <stdlib.h>
#include "fatal.h"
/* Place in the interface file */
struct Node
{
TElement Element;
Position Next;
};
List
MakeEmpty( List L )
{
if( L != NULL )
DeleteList( L );
L = malloc( sizeof( struct Node ) );
if( L == NULL )
FatalError( "Out of memory!" );
L->Next = NULL;
return L;
}
/* Return true if L is empty */
int
IsEmpty( List L )
{
return L->Next == NULL;
}
/* Return true if P is the last position in list L */
/* Parameter L is unused in this implementation */
int IsLast( Position P, List L )
{
return P->Next == NULL;
}
/* Return Position of X in L; NULL if not found */
Position
Find( TElement X, List L )
{
Position P;
P = L->Next;
while( P != NULL && P->Element != X )
P = P->Next;
return P;
}
/* Delete from a list */
/* Cell pointed to by P->Next is wiped out */
/* Assume that the position is legal */
/* Assume use of a header node */
void
Delete( TElement X, List L )
{
Position P,
TmpCell;
P = FindPrevious( X, L );
if( !IsLast( P, L ) ) /* Assumption of header use */
{ /* X is found; delete it */
TmpCell = P->Next;
P->Next = TmpCell->Next; /* Bypass deleted cell */
free( TmpCell );
}
}
/* If X is not found, then Next field of returned value is NULL */
/* Assumes a header */
Position
FindPrevious( TElement X, List L )
{
Position P;
P = L;
while( P->Next != NULL && P->Next->Element != X )
P = P->Next;
return P;
}
/* Insert (after legal position P) */
/* Header implementation assumed */
/* Parameter L is unused in this implementation */
void
Insert( TElement X, List L, Position P )
{
Position TmpCell;
TmpCell = malloc( sizeof( struct Node ) );
if( TmpCell == NULL )
FatalError( "Out of space!!!" );
TmpCell->Element = X;
TmpCell->Next = P->Next;
P->Next = TmpCell;
}
/* Correct DeleteList algorithm */
void
DeleteList( List L )
{
Position P,
Tmp;
P = L->Next; /* Header assumed */
L->Next = NULL;
while( P != NULL ) {
Tmp = P->Next;
free( P );
P = Tmp;
}
}
Position
Header( List L )
{
return L;
}
Position
First( List L )
{
return L->Next;
}
Position
Advance( Position P )
{
return P->Next;
}
TElement
Retrieve( Position P )
{
return P->Element;
}
/* IMPLEMENTACIA OPERACIE CAT */
List
Cat( List L1, List L2 )
{
List P = L1;
while (P->Next != NULL)
P = P->Next;
P->Next = L2;
return L1;
}
/* IMPLEMENTACIA OPERACIE CUT */
List
Cut( List L, Position P )
{
while (L->Next != P)
L = L->Next;
L->Next = NULL;
return P;
}