Share
Lists – Example with students

Lists – Example with students

List with students:

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>

typedef struct elev{
	 char* nume;
	 char* prenume;
	 int nota;
	 struct elev*next;
	}ELEV;

ELEV* cap,*coada;

void citire(int i,ELEV* p){
 p->nume=(char*)malloc(sizeof(char)*20);
 printf("Nume %d: ",i);scanf("%s",p->nume);
 p->prenume=(char*)malloc(sizeof(char)*20);
 printf("Prenume %d: ",i);scanf("%s",p->prenume);
 printf("Nota %d: ",i);scanf("%d",&p->nota);
 p->next = NULL;
}

void adaugInceput(ELEV* p){
 p->next=cap;
 cap=p;
 if (!coada)
   coada=cap;
}

void adaugCoada(ELEV* p){
  if (cap==NULL)
   cap=p;
  else
   coada->next=p;
  coada = p;
}

void creare(void){
 ELEV *p;
 int i=0;
 char ch;

 cap=NULL;coada=NULL;
 while (1){
  p=(ELEV*)malloc(sizeof(ELEV));
  citire(i,p);
  //adaugInceput(p);
  adaugCoada(p);
  printf("Mai adaugati? (D/N):");ch=getche();
  printf("\n");
  i++;
  if (toupper(ch)=='N')
    break;
 }
}

void afisare(ELEV* cap){
  ELEV *p = cap;

  while (p != NULL){
    printf("%s %s %d \n",p->nume,p->prenume,p->nota);
    p = p->next;
  }
}

void inserare(ELEV* p){
 ELEV *q,*prev;

 if (p->nota<cap->nota) adaugInceput(p);
 else{
   prev=NULL;q=cap;
   while (q!=NULL && q->nota<=p->nota){
    prev=q;q=q->next;
   }
   if (!q) adaugCoada(p);
   else{
    prev->next=p;
    p->next=q;
   }
 }
}

void pune(void){
 ELEV *p;

 p=(ELEV*)malloc(sizeof(ELEV));
 citire(0,p);
 inserare(p);
}

void sterge(void){
  char nume[20];
  ELEV *p,*prev;

  printf("Nume: ");scanf("%s",nume);
  p=cap;prev=NULL;
  while (p != NULL && strcmp(nume,p->nume) != 0){
    prev=p;
    p=p->next;
  }
  if (!p) printf("Nu l-am gasit!\n");
  else{
    if (p==cap){
      cap=cap->next;
      if (!cap) coada=NULL;
    }
    else
      if (p==coada){
	coada=prev;
	coada->next=NULL;
      }
      else
	prev->next=p->next;
    free(p->nume);
    free(p->prenume);
    free(p);
  }
}

void main(void){
 creare();
 afisare(cap);
 sterge();
 afisare(cap);
}

 

8,638 total views, 22 views today