Dodanie elementów struktury

0

Witam, stworzyłem taki oto program:

#include "stdafx.h"
#include <iostream>
#include "time.h"
#include "string.h"
#include "conio.h"
#include "stdio.h"
using namespace std;

struct SStudent
{
	float ocenaZPI;
	SStudent* next;
};

SStudent *add (SStudent *head, int New)
{
       SStudent *tmp, *nowy, *cien;
       int tmp2;
       nowy = (SStudent *)malloc(sizeof(SStudent));
       nowy->ocenaZPI = New;
       nowy->next=NULL;

       if (head==NULL){
          head=nowy;
          }
          else if (head->ocenaZPI >= New){
               nowy->next=head;
               head=nowy;
               }
       else{
            tmp=head;
            while(tmp!=NULL && tmp->ocenaZPI<New){
            cien=tmp;
            tmp=tmp->next;
            }           
            cien->next=nowy;
            nowy->next=tmp;
            }
       return head;
}    

void show (SStudent *head)
{
     if(head != NULL){
     while (head != NULL){
		 printf("%d ", head->ocenaZPI);
		head=head->next;
		}
		printf("\n");
     }
     else printf("Brak elementow do wyswietlenia.n");
}

void search (SStudent *head, int Find)
{
     int i=1;
     SStudent *tmp=head;
	 while (tmp!= NULL && tmp->ocenaZPI!=Find)
		{
			tmp=tmp->next;
			i++;
		}
		if (tmp!=NULL)
		{
			printf("Element %d jest na %d miejscu", tmp->ocenaZPI, i);
			printf("\n");
		}
		else printf("Element nie istnieje\n");
}

SStudent *Delete (SStudent *head, int Del)
{
	SStudent *tmp, *tmp2, *pom;
	tmp = head;
	if(head==NULL)
	{
		printf("rak elementu do usuniecia\n");
		return head;
	}
	else if(head->next == NULL && head->ocenaZPI == Del)
	{
		free(head);
		head = NULL;
		return head;
	}
	else
	{
		if (tmp->ocenaZPI== Del)
		{
			pom=tmp->next;
			free(head);
			return pom;
		}
		else
		{
			tmp2=tmp->next;
			while (tmp2->next!= NULL && tmp2->ocenaZPI != Del)
			{
				tmp=tmp->next;
				tmp2=tmp->next;
			}
			if (tmp2->next== NULL)
			{
				printf("Brak elementu.\n");
			}
			else if(tmp2->next==NULL)
			{
				free(tmp2);
				tmp->next = NULL;
			}
			else
			{
				tmp->next = tmp2->next;
				free(tmp2);
				tmp2 = NULL;
			}

           }
	}
         return head;
}


int main ()
{
	
	int option=0, New, Del, Find;
    SStudent *my_list;
    my_list = (SStudent *)malloc(sizeof(SStudent));
    my_list = NULL;

    while(option!=5)
    {

		printf("\n1.Dodaj\n2.Wyswietl\n3.Szukaj\n4.Usun\n5.Wyjscie\n");
		scanf("%d",&option);

		switch(option)
		{
		case 1: 
		{
			printf("Podaj ocene z PI\n"); scanf("%d",&New);
			my_list=add(my_list,New); 
			
			break;
		}
		case 2: 
		{
		show(my_list);
		
		break;
		}
		case 3:
		{
			printf("Podaj ocene\n"); 
			scanf("%d",&Find);
			search(my_list,Find); 
			
			break;
		}
		case 4:
		{
			printf("Podajocene, z ktora studenci maja byc usunieci\n"); 
			scanf("%d",&Del);
			my_list=Delete(my_list,Del); 

			break;
		}
		case 5:
		{
			option = 5;
			
			break;
		}
		default: 
		{  
			break;
		}
		}

    }
    return 0;
}

Teraz dodatkowo muszę jeszcze do struktury dodać imię, nazwisko i numer indeksu. Niestety nie mam pojęcia, jak to zrobić. Znaczy, umiem dodać do struktury, ale jak potem dodać to w dodawaniu i wyświetlaniu? usuwanie i szukanie chyba bez zmian, bo ma dotyczyć oceny.

0

przeciąż funkcje add tak by był parametr oceny, oceny z indeksem, oceny z nazwiskiem itd. Wtedy będziesz po prostu wywoływał odpowiednie add (jeżeli chcesz dodać studenta z ocenami wraz) lub zrób funkcje addRating w której prześlesz początek "listy" studentów oraz drugi parametr który będzie rozpoznawał czy "to ten student" (może to być adres obiektu, nazwisko, indeks czy co tam chcesz)

i jeżeli to C to odpowiednio taguj. Bo jeżeli c++ to w sumie cały program jest do przepisania.

0

Nie bardzo wiem jak to zrobić, mógłbyś mi napisać jakiś przykładowy kod albo coś w tym stylu?

1 użytkowników online, w tym zalogowanych: 0, gości: 1