Je to dobre napisane, az na jednu vec... nema vyznam v danom pripade vytvarat kolekciu...
Příspěvky odeslané z IP adresy 147.232.177.–
Payne
Payne
jednoduche, tym prvym mazem z batohu predmet, ale chcem ho aj vratit sucasne, taze tak...
no a toto druhe zase opacne, ked je dane miesto volne, tak tam dany predmet ulozim...
a inak uz viem de je chyba a veru v tom to nebolo, je to dobre napisane...
Payne
Zdravim, riesim akurat problem ze nefunguju mi metody pop a push na vkladanie a vyberanie poloziek do pola v batohu...
mohli by ste mi niekto pomoct???
public class BackPack {
private static final BackPack backPack = new BackPack();
private static final int NUMBER_OF_ITEMS = 4;
private PortableItem[] items;
private BackPack() {
items = new PortableItem [NUMBER_OF_ITEMS];
}
public static BackPack getBackPack() {
return backPack;
}
public PortableItem[] getItems() {
return items;
}
public PortableItem pop(String name) throws WrongItemExistenceException {
PortableItem variable;
for (PortableItem i : items) {
if (i != null) {
System.out.println("pop rozny od null");
if (i.getName().equals(name)) {
variable = i;
i = null;
return variable;
}
}
}
throw new WrongItemExistenceException("Predmet " + name + " sa v batohu nenachádza ...");
}
public void push(Item item) throws WrongItemUsageException {
if (item instanceof NonPortableItem)
throw new WrongItemUsageException("Predmet " + item.getName() + " sa nedá vložiť do batohu ...");
else {
for (PortableItem i : items) {
if (i == null) {
System.out.println("push rozny od null");
i = (PortableItem) item;
return;
}
}
throw new WrongItemUsageException("V batohu nieje miesto na uloženie dalšieho predmetu ...");
}
}
}
pricom PortableItem je trieda, ktora ma 2 atributy - name a description a nim prisluchajuce gettery...
Payne
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;
}