Cześć!
Siedzę nad programem, który wczytuje dane z pliku do stosu opartego o listę jednokierunkową, zrobiłam już wyspianie danych, wielkość stosu, ale chciałabym jeszcze móc wyszukać osobę po nazwisku. Jak mam to zrobić - myślałam o funkcji, ale nie za bardzo wiem jak ją tutaj wrzucić (no bo to taki listo stos).
Poniżej podaję dotychczasowy kod - takie rozwiązanie doradził mi kolega, ale razem się głowimy co zrobić z tym wyszukiwaniem nazwiska. Bardzo proszę o pomoc.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check;
struct baza
{
char imie[10];
char nazwisko[15];
int numer;
}dane ;
struct node
{
struct baza dane;
struct node *next;
int index;
}el;
struct lista
{
struct node *head;
struct node * tail;
};
void init(struct lista *l)
{
l->head=NULL;
l->tail=NULL;
}
void push(struct lista *l, char imie[10], char nazwisko[15], int numer)
{
struct node *el=(struct node *)malloc(sizeof(struct node));
strcpy(el->dane.imie,imie);
strcpy(el->dane.nazwisko,nazwisko);
el->dane.numer=numer;
if((l->head==NULL) && (l->tail==NULL))
{
el->next=NULL;
l->head=el;
l->tail=el;
}
else
{
el->next=l->head;
l->head=el;
}
}
void druk (struct lista *l,int i)
{
int j;
struct node *el=(struct node *)malloc(sizeof(struct node));
for(j=1;j<=i;j++)
{
el=l->head;
while(el)
{
if(el->index==j)
{
printf("%s %s %d\n",el->dane.imie, el->dane.nazwisko, el->dane.numer);
break;
}
else
el=el->next;
}
}
}
void wczyt(struct lista *l,FILE *f,int i)
{
struct node *el=(struct node *)malloc(sizeof(struct node));
el=l->head;
if(f!=NULL)
{
fscanf(f,"%s %s %d",&el->dane.imie, &el->dane.nazwisko, &el->dane.numer);
el->index=i;
druk(l,i);
}
else
{
printf("Plik nie istnieje");
}
check=feof(f);
}
int size (struct lista *l)//zwraca aktualny rozmiar stosu(aklualna liczbe elementow)
{
int s=0;
struct node *el=l->head;
while(el)
{
s++;
el=el->next;
}
return s;
}
int main()
{
struct lista *l=(struct lista *)malloc(sizeof(struct lista));
FILE *f=fopen("c:\\Users\\Minami\\Desktop\\test.txt","r");
char imie[10];
char nazwisko[15];
int numer;
int i=1;
char wybor;
init(l);
printf("\n1 - dodaj element 2 - rozmiar 3 - zakoncz");
do
{
printf("\n\nWybierz opcje:\t");
scanf("%c",&wybor);
fflush(stdin);
switch(wybor)
{
case '1' :
{
if(check==0){
push(l,imie,nazwisko,numer);
wczyt(l,f,i);
i++;
}
else printf("koniec pliku");
break;
}
case '2' :
{
printf("Aktualny rozmiar: %d",size(l));
break;
}
case '3' :
{
return 0;
}
}
}while(wybor);
getchar();
return 0;
}