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

#Include #Include : Typedef Struct Int Struct

This document contains C code that defines a struct called Nod to represent nodes in a linked list. It includes functions to insert a new node into the linked list based on its value, and to print out the values of all nodes. The main function creates a sample linked list, inserts some additional nodes using the insert function, and then prints out the final list using the afisare function.

Uploaded by

Ana Codreanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

#Include #Include : Typedef Struct Int Struct

This document contains C code that defines a struct called Nod to represent nodes in a linked list. It includes functions to insert a new node into the linked list based on its value, and to print out the values of all nodes. The main function creates a sample linked list, inserts some additional nodes using the insert function, and then prints out the final list using the afisare function.

Uploaded by

Ana Codreanu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1. #include <stdio.

h>
2. #include <stdlib.h>
3.
4.
5. typedef struct nod {
6. int valoare;
7. struct Nod *leg;
8. } Nod;
9.
10.

Nod *insert(Nod *ceva, int valoare) {

11.

if (ceva-> valoare > valoare) {

12.

Nod *nou;

13.

nou = malloc(sizeof(Nod));

14.

nou->valoare=valoare;

15.

nou->leg = ceva;

16.

return realloc(nou,sizeof(Nod));

17.

18.

Nod *prec = ceva,*misca = ceva->leg;

19.

while (misca-> leg != NULL) {

20.

if (misca->valoare >valoare) {

21.

Nod *nou;

22.

nou = malloc(sizeof(Nod));

23.

nou->valoare=valoare;

24.

nou->leg = misca;

25.

prec->leg= nou;

26.

return realloc(ceva,sizeof(Nod));

27.

28.

prec=misca;

29.

misca=misca->leg;

30.

31.

misca->valoare = valoare;

32.

return realloc(ceva,sizeof(Nod));

33.

34.
35.

void afisare(Nod *ceva) {

36.

printf("\n");

37.

while (ceva != NULL) {

38.

printf("%d ",ceva->valoare);

39.

ceva = ceva->leg;

40.

41.

42.

43.
44.

int main()

45.

46.

Nod *actual,*primul,*nou;

47.

nou = malloc(sizeof(Nod) );

48.

primul=nou;

49.

nou -> valoare = 2;

50.

nou->leg = NULL;

51.

int i;

52.

for(i = 1 ; i < 5; i++)

53.

54.

printf("Dati valoare :");

55.

actual=nou;

56.

nou=malloc(sizeof(Nod));

57.

scanf("%d",&(nou->valoare));

58.

nou->leg=NULL;

59.

actual->leg = nou;

60.

61.

primul=insert(primul,10);

62.

primul=insert(primul,1);

63.

primul=insert(primul,3);

64.
65.

afisare(primul);

66.

return 0;

67.

You might also like