0% found this document useful (0 votes)
33 views

Tema:: Raport

1. The document describes a laboratory report on solving problems using structs in C. 2. It includes code for implementing a stack using arrays and traversing/inserting nodes in a linked list. 3. The code shows functions for pushing/popping elements onto a stack, and inserting/deleting nodes from the beginning, end, or middle of a linked list.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Tema:: Raport

1. The document describes a laboratory report on solving problems using structs in C. 2. It includes code for implementing a stack using arrays and traversing/inserting nodes in a linked list. 3. The code shows functions for pushing/popping elements onto a stack, and inserting/deleting nodes from the beginning, end, or middle of a linked list.

Uploaded by

Alberto Bonnuci
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Ministerul Educaţiei

al Republicii Moldova

Universitatea Tehnică a Moldovei

RAPORT
despre lucrarea de laborator Nr. 6
la Structuri de Date si Algoritmi

Tema: Rezolvarea problemelor cu STRUCT în C


Varianta 5

A îndeplinit: Iordan Marin

Chişinău – 2020
Mersul lucrării:

Ex 1
Codul:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>

#define max 100

char a[max][max];
int top = -1;
char x[max];

int isempty() ;
int isfull() ;
void introducere() ;
int eliminare() ;
void afisare() ;

int main()
{
int ch;

do
{

printf("\n 1. introducere");
printf("\n 2. eliminare");
printf("\n 3. afisare");
printf("\n 4. Iesire\n");
scanf("%d",&ch);
switch (ch) {
case 1:
introducere();
break;
case 2:
eliminare();
break;
case 3:
afisare();
break;
case 4:

break;
default:
printf("Alegere invalida");
break;
}
}while(ch!=4);
return 0;
}

int isfull(){
if ( (top == max-1))
{
return 1;
}
else
{
return 0;
}
}

int isempty(){
if((top==-1))
{
return 1;
}
else
{
return 0;
}
}
void introducere(){
if (isfull())
{
printf("Stiva e plina");
}
else
{
printf("Introdu element: ");
scanf("%s",x);
top++;
strcpy(a[top],x);
}
}

int eliminare(){
if(isempty())
{
printf("Stiva e goala");
exit(0);
}
else{
strcpy(x,a[top]);
printf("%s",x);
top--;
}
}

void afisare()
{
if(isempty())
{
printf("nu este nimic de afisat");
exit(0);
}
else
{
int i;
for(i=0;i<top+1;i++)
{
printf("%s \n",a[i]);
}
}
}

Output:
Ex 2:

Codul:

#include <stdio.h>
#include <stdlib.h>
struct node {
char nume[30];
char prenume[30];
int anNastere;
char adresa[30];
int varsta;
char sex;
struct node *next;
};

void parcurgereSiAfisare(struct node *p) {


int nr = 1;
printf("\nLista: \n");
while (p != NULL) {
printf("Studentul nr: %d\n", nr);
printf("Numele: %s\n", p->nume);
printf("Prenumele: %s\n", p->prenume);
printf("An Nastere: %d\n", p->anNastere);
printf("Adresa: %s\n", p->adresa);
printf("Varsta: %d\n", p->varsta);
printf("Sex: %c\n", p->sex);
p = p->next; nr++;
}
}

void introducere(struct node *p) {


int nr = 1;
printf("Introducerea valorilor listei pentru 3 studenti:\n");
while (p != NULL) {
printf("Studentul %d: \n", nr);
printf("Introdu numele: "); fgets(p->nume, 30, stdin);
printf("Introdu prenumele: "); fgets(p->prenume, 30, stdin);
printf("Introdu anul nasterii: "); scanf("%d", &p->anNastere);
getchar();
printf("Introdu adresa: "); fgets(p->adresa, 30, stdin);
printf("Introdu varsta: "); scanf("%d", &p->varsta);
getchar();
printf("Introdu sexul: "); scanf("%c", &p->sex);
getchar();
p = p->next; nr++;
}
}

void varstaMinMax(struct node *p){


int min = 1000, max = 0;
while (p != NULL){
if(p->varsta < min){
min = p->varsta;
}if(p->varsta > max){
max = p->varsta;
}
p = p->next;
}
printf("Varsta minima - %d\nVarsta maxima - %d\n", min, max);
}

int main() {
struct node *head;
struct node *one = NULL;
struct node *two = NULL;
struct node *three = NULL;
struct node *ch = NULL;
int n;

one = malloc(sizeof(struct node));


two = malloc(sizeof(struct node));
three = malloc(sizeof(struct node));
ch = malloc(sizeof(struct node));

one->next = two;
two->next = three;
three->next = NULL;

head = one;
while(1){
printf("Alege operatia: \n1.Introducerea listei\n2.Afisarea listei\n3.Introducere
Element Inceput\n4.Introducere Element Sfarsit\n5.Introducere element Intre 2
Noduri\n6.Stergere Element\n7.Minimul si Maximul Varstei\n8.EXIT\n");
scanf("%d", &n);
getchar();
switch(n){
case 1: introducere(head);
break;
case 2: parcurgereSiAfisare(head);
break;
case 3: front(head);
break;
case 4: end(head);
break;
case 5: printf("Introdu elementul dupa care sa inserezi nodul: ");
scanf("%d", &ch);
after(ch);
break;
case 6: printf("Introdu elementul dupa care sa stergi nodul: ");
scanf("%d", &ch);
del(ch);
break;
case 7: varstaMinMax(head);
case 8: return 0;
default: break;
}
}
return 0;
}

front(struct node *head)


{
struct node *p;
p=malloc(sizeof(struct node));
printf("Introdu numele: "); fgets(p->nume, 30, stdin);
printf("Introdu prenumele: "); fgets(p->prenume, 30, stdin);
printf("Introdu anul nasterii: "); scanf("%d", &p->anNastere);
getchar();
printf("Introdu adresa: "); fgets(p->adresa, 30, stdin);
printf("Introdu varsta: "); scanf("%d", &p->varsta);
getchar();
printf("Introdu sexul: "); scanf("%c", &p->sex);
getchar();
p->next=head;
return (p);
}

end(struct node *head,int value)


{
struct node *p,*q;
p=malloc(sizeof(struct node));
printf("Introdu numele: "); fgets(p->nume, 30, stdin);
printf("Introdu prenumele: "); fgets(p->prenume, 30, stdin);
printf("Introdu anul nasterii: "); scanf("%d", &p->anNastere);
getchar();
printf("Introdu adresa: "); fgets(p->adresa, 30, stdin);
printf("Introdu varsta: "); scanf("%d", &p->varsta);
getchar();
printf("Introdu sexul: "); scanf("%c", &p->sex);
getchar();
p->next=NULL;
q=head;
while(q->next!=NULL)
{
q = q->next;
}
q->next = p;
}

after(struct node *a)


{
if (a->next != NULL)
{
struct node *p;
p = malloc(sizeof(struct node));
printf("Introdu numele: "); fgets(p->nume, 30, stdin);
printf("Introdu prenumele: "); fgets(p->prenume, 30, stdin);
printf("Introdu anul nasterii: "); scanf("%d", &p->anNastere);
getchar();
printf("Introdu adresa: "); fgets(p->adresa, 30, stdin);
printf("Introdu varsta: "); scanf("%d", &p->varsta);
getchar();
printf("Introdu sexul: "); scanf("%c", &p->sex);
getchar();
p->next = a->next;
a->next = p;
}
else
{
printf("Use end function to insert at the end\n");
}
}

del (struct node *before_del)


{
struct node *temp;
temp = before_del->next;
before_del->next = temp->next;
free(temp);
}
Output(partea 1):
Output(partea 2):
Concluzie: În aceasta lucrare de laborator am lucrat cu stive si liste in
C

You might also like