I have this program, made of 3 files:
lista.h:
Code:
#ifndef _LISTA_H_
#define _LISTA_H_
typedef char LOGIC;
#define ADEVARAT 1
#define FALS 0
struct lista {
int info;
struct lista *urm;
};
typedef struct lista Lista,*LISTA;
LISTA newl();
void destroyl(LISTA);
LISTA ins_la_inceput(LISTA,int);
LISTA sterge_primul(LISTA);
void afiseaza_lista(LISTA);
LOGIC este_vida(LISTA l);
#endif
functii.cpp
Code:
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
#include "lista.h"
LISTA newl() {
return NULL;
}
LISTA ins_la_inceput(LISTA l,int info) {
LISTA p=(LISTA)malloc(sizeof(Lista));
if(p!=NULL) {
p->info=info;
p->urm=l;
return p;
}
return l;
}
LISTA sterge_primul(LISTA l) {
// daca lista nu este vida
if(l!=NULL) {
LISTA p=l->urm;
free(l);
return p;
}
return l;
}
void afiseaza_lista (LISTA l) {
if(l==NULL)
printf("\n Lista este vida.");
else {
while(l!=NULL) {
printf("%d ",l->info);
l=l->urm;
}
}
}
LOGIC este_vida(LISTA l) {
if(l==NULL)
return ADEVARAT;
else
return FALS;
}
void destroyl(LISTA l) {
if(l!=NULL) {
// se parcurg toate elementele si se elibereaza spatiul ocupat de ele
while(l!=NULL) {
LISTA p=l;
l=l->urm;
free(p);
}
}
}
main.cpp:
Code:
#include<stdio.h>
#include<stdlib.h>
#include "functii.cpp"
#include "lista.h"
#include<ctype.h>
main()
{
LISTA foaie;
int nr;
char j;
puts("A.Insert at begining");
puts("B.Del the first element");
puts("C.List the list");
puts("D.exit");
scanf("%c", &j);
switch(toupper(j))
{
case 'A':{
printf("Insert number:");
scanf("%d", &nr);
foaie=ins_la_inceput(foaie, nr);
break;
}
case 'B':{
sterge_primul(foaie);
break;
}
case 'C':{
puts("Afisare lista");
afiseaza_lista(foaie);
break;
}
case 'D':{
exit(0);
}
}
return 0;
}
at compilation
$gcc -o prog main.cpp
$/tmp/ccxkDtqH.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
same with
$gcc -o prog main.cpp lista.h functii.cpp
thx