Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listas #37

Closed
Paola-Garciaa opened this issue Mar 20, 2024 · 0 comments
Closed

Listas #37

Paola-Garciaa opened this issue Mar 20, 2024 · 0 comments

Comments

@Paola-Garciaa
Copy link

Simple list

#include <stdio.h>
#include <stdlib.h>
#include "definitions.c"
#include "add ítems.c"
#include "show items.c"
#include "search item.c"
#include "del item.c"
#include "menú.c"

Int main(){
Menú ();
Return 0;

}

Definitions

/*
Node (data, id) ->

/*

Int cont = 1;

Struct node {
Int id;
Chat cad [20]
Float núm;
Struct node *next;

};

Enum booleano {false, true};
Enum booleano {add, delete, search, show, bye};

Menú

Void menú (){

 Struct node *cart = NULL;
 Int op;
 Struct node *tmp;
 Enum booleano res;
 While (1) {
       Print("choose...\n");
       Print ("1) add\n");
       Print ("2) del\n");
       Print ("3) search\n");
       Print ("4) show\n");
       Print ("5) bye\n");
       Scanf ("%d", &op);
  
 Switch (op) {
     Case 1: 
      Res = add item(&cart);
      If (res){
         Print("item added !\n");
      Break;
     Case 2: 

       Printf("which item...?\n);
        Scanf("%i", %id);
        Tmp = del item(&cart, id)
        If (tmp != NULL {
           Printf("we delete it!\n");
          Printf("%p\t%s\t%.2f\n", tmp, tmp->cad, tmp->num);

}
Break;

     Case 3:
        Printf("which item...?\n);
        Scanf("%i", %id);
        Tmp = search item(c, id)
        If (tmp != NULL {
           Printf("we found it!\n");
          Printf("%p\t%s\t%.2f\n", tmp, tmp->cad, tmp->num);

}
Break;
Case 4:
show items(cart);
Break;
Case 5:
/* exit */
Return;
Default;
Break;

}

}

add ítem

/*
(Struct node) cart -> | add ítem | -> boolean

/*
Enum booleano add item (struct node **c) {
Struct node new = (struct node)malloc(sizeof(struct node));
If(new != NULL){
New->id = cont++;
New->next = *c;
Scanf ("%s", new->cad);
Scanf ("%f", &new->num);
*c = new;
Return true;
}
Return false;
}

Show ítems

/*
Cart -> show items -> void
*/

Void show items(struct node *c){
While(c!= NULL){
Printf("%p\t%d\t%s\t%.2f\t->%p\n", c, c->id, c->cad, c->num, c->next);
c = c->next;
}
}

**Search item **

/*
Cart, id-> search item -> item or NULL
*/

Struct node *search item(struct node *c, int id) {
While (c != NULL) {
If (c->id == id){
Return c;
}
c = c->next;
}
Return NULL;
}

del item

/*
Cart, id -> del item -> item or NULL
*/

Struct node *del item(struct node **c, int id) {
Struct node *tmp = *c, *prev = NULL;
While (tmp != NULL) {
If(tmp->id == id){
If (prev == NULL){
*c = tmp->next;
Tmp->next = NULL;
} else {
Prev->next = tmp->next;
}
Tmp->next = NULL;
Return tmp;
}
Prev = tmp;
Tmp = tmp ->next;
}
Return NULL;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants