Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Scrivere un programma che permetta di inserire un nuovo studente nella lista degli
- * studenti universitari, in modo che tale lista semplicemente concatenata sia ordinata
- * rispetto al numero di matricola. Stampare la lista.*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct studente {
- char nome[30];
- char cognome[30];
- int matricola;
- struct studente *next;
- } studente;
- studente *head=NULL;
- studente* inseriscistud( studente* head, studente* stud);
- void main() {
- studente *stud=NULL;
- stud=(studente*)malloc(sizeof(studente*));
- studente *s; //puntatore per la stampa
- printf("Inserisci nome: ");
- gets(stud->nome);
- printf("Inserisci cognome: ");
- gets(stud->cognome);
- printf("Inserisci numero di matricola: ");
- scanf("%d", stud->matricola);
- head= inseriscistud(head, stud);
- s=head;
- while(s!=NULL) {
- printf("Nome: %s\nCognome: %s\nMatricola: %d\n\n", s->nome, s->cognome, s->matricola);
- s=s->next;
- }
- }
- studente* inseriscistud(studente *head, studente *stud) {
- studente *r=NULL, *q=NULL;
- studente *p=NULL;
- if(head==NULL) {
- head=(studente*)malloc(sizeof(studente*));
- strcpy(head->nome, stud->nome);
- strcpy(head->cognome, stud->cognome);
- head->matricola= stud->matricola;
- head->next= NULL;
- }
- else {
- r=head; //precedente
- q=head; //successivo
- p=(studente*)malloc(sizeof(studente*));
- strcpy(p->nome, stud->nome);
- strcpy(p->cognome, stud->cognome);
- p->matricola= stud->matricola;
- while((q!=NULL) && (q->matricola < p->matricola)) {
- r=q;
- q=q->next;
- }
- r->next= p;
- p->next= q;
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement