Problem 1
Implementati conceptul de stiva folosind listele simplu inlantuite.
#pragma once
struct Node {
int data;
struct Node * next;
};
struct Node * a = NULL;
void push(int);
void pop();
int top();
int isEmpty();
void push(int data) {
struct Node * temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = a;
a = temp;
}
void pop() {
struct Node * temp = (struct Node *)malloc(sizeof(struct Node));
if (isEmpty()) {
return;
}
temp = a;
a = a->next;
free(temp);
}
int top() {
if (!isEmpty()) {
return a->data;
}
else {
return 0;
}
}
int isEmpty() {
if (a == NULL) {
return 1;
}
else {
return 0;
}
}
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "chapter10-problem1.h"
void main() {
printf("%d\n", top());
push(1);
printf("%d\n", top());
push(2);
printf("%d\n", top());
pop();
printf("%d\n", top());
push(3);
printf("%d\n", top());
_getch();
}
Problem 2
Scrieti un program in care se va implementa structura de mai jos si se va afisa valoarea totala pentru elementele din lista.
struct prod {
char codp[5];
int cant;
double pret;
prod * urm;
}
#pragma once
struct prod {
char codp[5];
int cant;
double pret;
struct prod * urm;
};
void adauga(char codp[], int, double);
void citeste(int *);
double valoare_totala();
void adauga(char codp[], int cantitate, double pret) {
struct prod * temp = (struct prod *) malloc(sizeof(struct prod));
strcpy(temp->codp, codp);
temp->cant = cantitate;
temp->pret = pret;
if (start == NULL) {
start = temp;
temp->urm = NULL;
}
else {
temp->urm = start;
start = temp;
}
}
void citeste(int * nr) {
free(start);
printf("Nr produse: ");
scanf("%d", nr);
for (int i = 0; i < *nr; i++) {
char cod_produs[5];
int cantitate = 0;
double pret = 0.0;
fgets(cod_produs, 5, stdin);
printf("\n\nCodul produsului: ");
fgets(cod_produs, 5, stdin);
printf("Cantitatea: ");
scanf("%d", &cantitate);
printf("Pret: ");
scanf("%lf", &pret);
adauga(cod_produs, cantitate, pret);
}
}
double valoare_totala() {
struct prod * trav = (struct prod *) malloc(sizeof(struct prod));
trav = start;
double total = 0;
while (trav->urm) {
total += (double)(trav->cant) * (trav->pret);
trav = trav->urm;
}
free(trav);
return total;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct prod * start;
#include "chapter10-problem2.h"
void main() {
int nr = 0;
double total = 0.0;
citeste(&nr);
total = valoare_totala();
printf("\nTotalul este: %f", total);
_getch();
}
Problem 3
Sa se creeze o lista simplu inlantuita cu 3 noduri, sa se initializeze si sa se afiseze elementele ei.
#pragma once
#define BUFF_SIZE 128
struct nod {
char nume[20];
int varsta;
struct nod * urmator;
};
void adauga(char nume[], int);
void citeste(int);
void afiseaza();
void adauga(char nume[], int varsta) {
if (start == NULL) {
start = (struct nod *)malloc(sizeof(struct nod));
strcpy(start->nume, nume);
start->varsta = varsta;
start->urmator = NULL;
}
else {
struct nod * temp = (struct nod *)malloc(sizeof(struct nod));
strcpy(temp->nume, nume);
temp->varsta = varsta;
temp->urmator = start->urmator;
start->urmator = temp;
}
}
void citeste(int nr_elemente) {
char nume[20];
int varsta;
char temp[BUFF_SIZE] = { '\0' };
for (int i = 1; i <= nr_elemente; i++) {
//// avoid multiple times call error
static int run = 0;
if (run++ != 0) {
fgets(temp, BUFF_SIZE, stdin);
}
//fgets(temp, 20, stdin);
printf("Nume: ");
fgets(nume, 20, stdin);
printf("Varsta: ");
scanf("%d", &varsta);
adauga(nume, varsta);
}
}
void afiseaza() {
while (start) {
printf("Nume: %s\n", start->nume);
printf("Varsta: %d\n\n", start->varsta);
start = start->urmator;
}
//free(start);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct nod * start;
#include "chapter10-problem3.h"
void main() {
int nr_elem = 3;
citeste(nr_elem);
afiseaza();
_getch();
}