Προς το περιεχόμενο

δομες δεδομενων c help...


dimitrispa

Προτεινόμενες αναρτήσεις

Δημοσ.

εχω την εξης ασκηση

 

>Υλοποιήστε τη δομή πίνακα κατακερματισμού με γραμμική δοκιμή (search, insert, delete) για φοιτητές (ΑΜ, όνομα, επμο, έτος_εγγραφής). Ο πίνακας κατακερματισμού πρέπει να είναι 20 θέσεων. Εισάγετε 12 φοιτητές με τους ακόλουθους ΑΜ: 17, 37, 57, 97, 1, 21, 4, 41, 61, 81, 117, 137. Εκτελέστε αναζήτηση για τους φοιτητές με ΑΜ 137, 81, 91, στη συνέχεια σβήστε το φοιτητή με ΑΜ 57 και εκτελέστε αναζήτηση για ΑΜ 57 και 137. 

 

και αυτος ο κωδικας μου:

>#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct foithths{
int am;
int etos;
char name[20];
char lname[20];
int status;
}*A[20];

int hash(int size,int am,int i){

int fesh;
return fesh=((am % size)+i)%size;

}

int searcharray(struct foithths *A[20],int am){

int firstPosition,thesh=hash(20,am,0),i=0;
firstPosition=thesh;
do{
	if(A[thesh]->status==-1){
		
		printf("The student with am %d was NOT found \n",A[thesh]->am);
		return -1;
	}

	else if(A[thesh]->am==am && A[thesh]->status==1){
		printf("The student with am %d was found \n",am);
		return thesh;
	}
	
	else{
	
		i++;
		thesh=hash(20,am,i);

	}

}while(i != firstPosition);

printf("Could not find the student");
return -1;

}

int insert(struct foithths *A[20],int am,int etos,char name[20],char lname[20]){

int firstPosition,fesh=hash(20,am,0),i=0;
firstPosition=fesh;

do{
	
		

	if(A[fesh]->status==-1 || A[fesh]->status==0){	//panta adeio
		
		A[fesh]->am=am;
		A[fesh]->etos=etos;
		strcpy(A[fesh]->name,name);
		strcpy(A[fesh]->lname,lname);
		A[fesh]->status=1;
		printf("Insert was succesfull \n");
		return 0;
	}

	else if(A[fesh]->status==1){//gemato
		
		i++;
		fesh=hash(20,am,i);
	}
	
			
}while(i != firstPosition );

printf("Failed to insert");
return -1;

}


int deletearray(struct foithths *A[20],int am){
int thesh;
	
thesh=searcharray(A,am);
	
if(thesh>=0 && thesh <= 19){
	A[thesh]->status=0;	
	printf("The delete of the student with am %d was complete \n",am);
	return 0;
}

return -1;	

}

void menu(void){
printf("1:Insert \n");
printf("2:Search \n");
printf("3:Delete \n");
printf("0:Exit \n");
}

int main(void){
int i,choice,am,etos,clue,removable;
char onoma[20],eponimo[20];

for(i=0;i<=19;i++){
	A[i]=(struct foithths *)malloc(sizeof(struct foithths));
	A[i]->status=-1;

}

menu();
scanf("%d", &choice);

while(choice != 0){

	if(choice==1){
		printf("Dose am,etos,onoma,eponimo\n");
		scanf("%d %d %s %s", &am, &etos, &onoma, &eponimo);
		insert(A, am, etos, onoma, eponimo);
	}
	else if(choice==2){
		printf("Give the am that youwish to find:\n");
		scanf("%d", &clue);
		searcharray(A,clue);
	}
	else if(choice==3){
		printf("Dose the am that you wish to delete:\n");
		scanf("%d",&removable);
		deletearray(A,removable);
	}
	else if(choice==0){
		break;
	}

	menu();
	scanf("%d", &choice);
}

return 1;

}

αλλα δεν λειτουργει σωστα.οποιος μπορει ας βοηθησει....

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...