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

main functions in a stack data struct #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions lists.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>

//This prog teachs how to use structs of list in c

//Main struct of all structs

typedef struct no
{
struct no * next;
int n;
}NODE;

NODE * cria_no(int n);
void print_no(NODE * l);


//list progs
NODE * insert_lista (NODE * l,int n);
NODE * delete_lista(NODE * l,int n);

int main (){
int n,i;

NODE * lista = NULL;

while(scanf("%d",&n)!=EOF){
lista = insert_lista (lista,n);
getchar();
}

printf("Numeros inseridos\n");
print_no(lista);


while(scanf("%d",&n)!=EOF){
lista = delete_lista (lista,n);
print_no(lista);
}
return 0;
}


NODE * cria_no(int n){
NODE * new = malloc(sizeof(NODE));
if (new!= NULL){
new->next = NULL;
new->n = n;
return new;
}
exit(1);
}

void print_no(NODE * l){
if (l !=NULL){
printf("%d ",l->n );
print_no(l->next);
}else {
printf("\n");
}
}

///***LISTA*****

NODE * insert_lista (NODE * l,int n){
if (l == NULL) return cria_no(n);
else{
l->next = insert_lista(l->next,n);
}
return l;
}

NODE * delete_lista(NODE * l,int n){
if (l == NULL) return NULL;
else if (l->n == n){
NODE * tmp = l;
l = l->next;
free(tmp);
}else{
l->next = delete_lista(l->next,n);
}
return l;
}
107 changes: 107 additions & 0 deletions queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include <stdio.h>
#include <stdlib.h>

//This prog teachs how to use structs of Queue in c

//Main struct of all structs

typedef struct no
{
struct no * next;
int n;
}NODE;


//expecific struct used in queue programs
typedef struct fila
{
NODE * begin,*final;
}FILA;

//auxiliar functions
NODE * cria_no(int n);
void print_no(NODE * l);

//Queue progs
FILA * cria_fila(void);
FILA * insert_fila(FILA * f,int n);
FILA * delete_fila (FILA * f);


int main (){
int n,i;
FILA * fila = cria_fila();
while(scanf("%d",&n)!=EOF){
fila = insert_fila(fila,n);
}

printf("OK\n");
while(scanf("%d",&n)!=EOF){
delete_fila(fila);
print_no(fila->begin);
}

return 0;
}


NODE * cria_no(int n){
NODE * new = malloc(sizeof(NODE));
if (new!= NULL){
new->next = NULL;
new->n = n;
return new;
}
exit(1);
}

void print_no(NODE * l){
if (l !=NULL){
printf("%d ",l->n );
print_no(l->next);
}else {
printf("\n");
}
}
///***FILAS*****

FILA * cria_fila(void){
FILA * f = malloc(sizeof(FILA));
if (f!=NULL){
f->begin = f->final = NULL;
return f;
}
exit(1);
}

FILA * insert_fila(FILA * f,int n){
if (f->begin == NULL){
f->begin = f->final = cria_no(n);
}else{
NODE * aux = cria_no(n);
aux->next = f->begin;
f->begin = aux;
}
return f;
}

FILA * delete_fila (FILA * f) {
if (f->begin == NULL) return f;
else{
if (f->begin == f->final){
NODE * aux = f->begin;
f->begin = f->final = NULL;
free(aux);
}else{
NODE * aux = f->begin;
while(aux->next!=f->final){
aux = aux->next;
}
f->final = aux;
aux = aux->next;
free(aux);
f->final->next = NULL;
}
}
return f;
}
80 changes: 80 additions & 0 deletions stacks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>

//This prog teachs how to use structs of Stacks in c

//Main struct of all structs

typedef struct no
{
struct no * next;
int n;
}NODE;

//auxiliar functions
NODE * cria_no(int n);
void print_no(NODE * l);

//stacks progs
NODE * insert_pilha (NODE * p,int n);
NODE * delete_pilha (NODE * p);

int main (){
int n,i;

NODE * pilha = NULL;

while(scanf("%d",&n)!=EOF){
pilha = insert_lista (pilha,n);
getchar();
}

printf("Numeros inseridos\n");
print_no(pilha);


while(scanf("%d",&n)!=EOF){
lista = delete_pilha (lista,n);
print_no(pilha);
}
return 0;
}


NODE * cria_no(int n){
NODE * new = malloc(sizeof(NODE));
if (new!= NULL){
new->next = NULL;
new->n = n;
return new;
}
exit(1);
}

void print_no(NODE * l){
if (l !=NULL){
printf("%d ",l->n );
print_no(l->next);
}else {
printf("\n");
}
}


///***PILHA*****

NODE * insert_pilha (NODE * p,int n){
if (p == NULL) return cria_no(n);
else{
NODE * aux = cria_no(n);
aux->next = p;
return aux;
}
}
NODE * delete_pilha (NODE * p){
if (p!=NULL){
NODE * aux = p;
p = p->next;
free(aux);
}
}