my code stock.com

Code to manipulate list

Snippet options

Download: Download snippet as stacks-lists-and-queues.c.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

#include <stdio.h>
#include <stdlib.h>
/*
htp://twitter.com/gccgomes
This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses></http:>.

*/

typedef struct no {
  int i;
  struct no *p;
}tipoLista;



// is list void?
int listavazia(tipoLista *l){
  return l == NULL;
}
// insert new element on first position (Push)
void insereInicio(int x, tipoLista **l){
  tipoLista *ll;
  ll = (tipoLista*) malloc(sizeof(tipoLista));
  ll->i=x;
  ll->p= *l;
  *l=ll;
}

//Inserte new element in end of a list enqueue
void insereFim(int x, tipoLista **l) {
  tipoLista *ll,*aux;
   ll = (tipoLista*) malloc(sizeof(tipoLista));
   ll->i = x; 
   ll->p  = NULL;
   if (*l == NULL)
     *l = ll;
   else{
     aux = *l;
     while (aux->p != NULL) 
	  aux = aux->p; 
     aux->p = ll; 
   }
}
//Insert element X before a position  a position K
void insere(int x, int k, tipoLista **l) {
  tipoLista *ll,*aux;
  int i=0;
  ll = (tipoLista*) malloc(sizeof(tipoLista));
  ll->i = x; 
  ll->p  = NULL;
  if (*l == NULL)
    *l = ll;
  else{
    aux = *l;
    for (i=1;(i<k-1) && (aux->p !=NULL); ++i)
      aux = aux->p; 
    if(aux){
      ll->p=aux->p;
      aux->p=ll;
    }else{
      ll->p=*l;
      *l=ll;
      
    }
  }
}
// Remove the first element on a list (POP dequeue)
int removeInicio(tipoLista **l){
  tipoLista *ll;
  int x;
  if(*l==NULL)
    return (int) NULL;
  ll = *l;
  *l=ll->p;
  x = ll->i;
  free(ll);
  return x;
  
}

//Remove the last element on a list dequeue
int removeFim(tipoLista **l){
  tipoLista *ll,*aux;
  int x;
  ll = (tipoLista*) malloc(sizeof(tipoLista));
  if(*l == NULL)    return (int) NULL;
  if((*l)->p == NULL){
    ll = *l;
    *l=NULL;
  }else{
    aux=*l;
    while(aux->p->p != NULL)   aux = aux->p;
    ll=aux->p;
    aux->p=NULL;
  }
  x=ll->i;
  free (ll);
  return x;
  
}

// Remove a element X on a List
int remover(int x, tipoLista **l){
  tipoLista *ll, *aux;
  int t,i=1;
  if((*l)==NULL) return -1;
  if(x==1) return removeInicio(l);
  if((*l)->p==NULL)  return removeFim(l);
  
  ll = (*l);
  while (ll != NULL && (i < x-1)){
    // aux=ll;
    ll = ll->p;
    ++i;
  }
  --i;
  aux=ll->p;
  ll->p=ll->p->p;
  //  printf("pos[%d]=%d\n",i,aux->i);
  t=ll->i;
  removeInicio(&aux);
  return t;
}

// Print list
void imprime(tipoLista *lista){
  if(lista==NULL){
    printf("\n");
    return;
  }else {
    printf("%d ", lista->i); 
    lista = lista->p;
    imprime(lista);
  }
}
//is X in List ?
int estaNaLista(int x, tipoLista *lista){
  if(lista->p==NULL && lista->i!=x )
    return 0;
  if(lista->i==x)
    return 1;
  else{
    lista=lista->p;
    estaNaLista(x,lista);
  }
}
//Remove X
int removex(int x, tipoLista **l){
    tipoLista *ll, *aux;
    int i;
    if (*l == NULL) return -1;
    if (((*l)->p==NULL) && ((*l)->i=x)) return removeFim(l);
    if ((*l)->i==x) return removeInicio(l);
    ll = *l;
    while (ll->p->i != x)
        ll = ll->p;
    aux = ll->p;
    ll->p = ll->p->p;
    i=aux->i;
    free(aux);  
    return i;
}
//Insert X after k
void insereApos(int x, int k, tipoLista **l) {
  tipoLista *ll,*aux;
  int i=0;
  ll = (tipoLista*) malloc(sizeof(tipoLista));
  ll->i = x; 
  aux=*l;
  while (aux->i!=k)
    aux=aux->p;
  
  ll->p=aux->p;
  aux->p=ll;
}
//Insert X before K
void insereAntes(int x, int k, tipoLista **l) {
  tipoLista *ll,*aux,*aux2;
  int i=0;
  ll = (tipoLista*) malloc(sizeof(tipoLista));
  ll->i = x;
  aux=*l;

  if(aux->i==k) {
    insereInicio(x,l); 
    return;
  }else{
    aux2=*l;
    aux=aux2->p;
    while ((aux->i!=k) || (aux->p!=NULL)){
      aux2=aux;
      aux=aux->p;
    }
    ll->p=aux2->p;
    aux2->p=ll;
  }
}
//Remove Y before X
int removerAntes(int x, tipoLista **l){
  tipoLista *ll,*aux;
  int t,i=1;
  ll=aux=*l;
  if(aux==NULL) return -1;
  if(aux->p->i==x) return removeInicio(l);
  if(estaNaLista(x,*l)){
    while(ll->p->i !=x){
      aux=ll;
      ll=ll->p;
    }
    printf("aux->i=%i aux->p->i=%i\n",aux->i,aux->p->i);
    printf("ll->i=%i ll->p->i=%i\n",ll->i,ll->p->i);
    aux->p=ll->p;
    ll->p=aux->p->p;
  }
}

//Remove Y after X
int removerApos(int x, tipoLista **l){
  tipoLista *ll,*aux;
  int t,i=1;
  if((*l)==NULL) return -1;
  if(x==1) return removeInicio(l);
  if((*l)->p==NULL)  return removeFim(l);
  ll=*l;
  if(estaNaLista(x,*l)){
      while ((ll->i != x))
	ll = ll->p;
      aux=ll->p;
      t=aux->i;
      ll->p=ll->p->p;
      free(aux);
      return t;
  }
}


//Concat listone with listtwo

tipoLista* concatena( tipoLista *l1, tipoLista *l2){
  tipoLista *aux, *aux2; /* variável auxiliar para criar a nova lista */
  if (l1 == NULL) return l2;
  for(aux=l1;aux != NULL; aux=aux->p)
    aux2=aux;
  aux2->p=l2;
  return l1;
}


main(){
  tipoLista *l,*l2,*lc;
  int i;
  l=NULL;
  l2=NULL;

  insereInicio(1,&l);
  insereInicio(100,&l2);
  
  for (i=10;i<20;++i)
    insereInicio(i,&l2);
  imprime(l2);
  
  for (i=1;i<10;++i)
    insereInicio(i,&l);
  imprime(l);

  lc = concatena(l2,l);
  imprime(lc);
}

Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.