Thread: Singly linked list c

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    3

    Singly linked list c

    Here's my code I'm trying to figure out how to add stuff to inner list but cant figure out what exactly I'm doing wrong here as I had nothing said about them on my lectures thuz I'm kind of lost about what to do. The function dodajprzedmiot is my attempt at doing so could somebody tell me exactly what I'm doing wrong and correct me or provide example how should it be done?Also would love if somebody could provide link to good lectures about singly linked lists and text/binary files ,Thanks.

    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <stdio.h>
    
    
    typedef struct lista lista;
    typedef struct item item;
    struct item
    {
        char item_name[30];
        item *next;
    };
    struct lista
    {
        int liczba;
        item *data;
        char napis[10];
        lista *next;
    };
    lista* find(lista *l,char name[61])
    {
        lista *current = l;
            int i;
            for(i=1;current!=NULL;i++)
            {
                if(strcmp(current->napis,name) == 0)
                {
                    printf("[%d] %d %s\n",i ,current->liczba,current->napis);
                    return current;
                }
                current=current->next;
        }
        return (NULL);
    }
    lista *dodajprzedmiot(lista *temp, char *item_named)
    {
          if(temp->data == NULL)
        {
            temp = (lista*)malloc(sizeof(lista));
            strcpy(temp->data->item_name, item_named);
            temp->data->next = NULL;
        }
        else
        {
            lista *pom;
            pom = temp;
    
    
            while(pom->data->next) pom->data=pom->data->next;
    
    
            pom->next=(lista*)malloc(sizeof(lista));
            strcpy(pom->data->next->item_name, item_named);
            pom->data->next->next=NULL;
        }
        return (NULL);
    }
    lista* kasuj(lista *list)
    {
       lista *pom=NULL;
    
    
        while(list)
        {
            pom=list->next;
            free(list);
            list=pom;
        }
        return NULL;
    }
    
    
    void dodaj(lista **l, int n, char tab[100])
    {
        if((*l)==NULL)
        {
            *l = (lista*)malloc(sizeof(lista));
            (*l)->liczba=n;
            strcpy((*l)->napis, tab);
            (*l)->next = NULL;
        }
        else
        {
            lista *pom;
            pom=(*l);
    
    
            while(pom->next) pom=pom->next;
    
    
            pom->next=(lista*)malloc(sizeof(lista));
            pom->next->liczba=n;
            strcpy(pom->next->napis, tab);
            pom->next->next=NULL;
        }
    }
    
    
    void dodajmiejsce(lista **l, int n, char tab[100])
    {
        if((*l)==NULL)
        {
            *l = (lista*)malloc(sizeof(lista));
            (*l)->liczba=n;
            strcpy((*l)->napis, tab);
            (*l)->next =NULL;
            return;
        }
    
    
        if( n<(*l)->liczba)
        {
            lista *pom=NULL;
            lista *pom2=NULL;
    
    
            pom=(lista*)malloc(sizeof(lista));
            pom->liczba=n;
            strcpy(pom->napis, tab);
            pom2=pom;
            pom->next=(*l);
            (*l)=pom2;
            return;
        }
        else
        {
            lista *pom;
            lista *pom2;
            pom=(*l);
    
    
            while((pom->next!= NULL) && (n>pom->next->liczba)) pom=pom->next;
    
    
            pom2=pom->next;
    
    
            pom->next=(lista*)malloc(sizeof(lista));
            pom->next->liczba=n;
            strcpy(pom->next->napis, tab);
            pom->next->next=pom2;
        }
    }
    
    
    void wypiszliste(lista *lis)
    {
      for( ; lis; lis=lis->next)
      {
        printf("%d %s \n", lis->liczba, lis->napis);
      }
    }
    
    
    lista* usun(lista *l, int us)
    {
        if(l==NULL)
        return l;
    
    
        if(l->liczba == us)
        {
            lista *pom=l->next;
            free(l);
            return pom;
        }
        else
        {
            lista *zwrot=l;
    
    
             lista *pom=NULL;
    
    
            while(( l->next->liczba != us) && (l->next->next!=NULL))  l=l->next;
    
    
            if(l->next->liczba==us)
            {
            pom=l->next;
    
    
            l->next=l->next->next;
            free(pom);
            }
    
    
            return zwrot;
        }
    }
    
    
    
    
    int licz(lista* p)
    {
    int l=0;
    for(;p;p=p->next)
    l++;
    return l;
    }
    
    
    
    
    int main()
    {
        int wybur, l;
        char ss[100];
    
    
        srand(time(0));
        lista *pocz=NULL;
    
    
        dodaj(&pocz, 1, "jeden");
        dodaj(&pocz, 2, "dwa");
        dodaj(&pocz, 3, "trzy");
        dodaj(&pocz, 4, "cztery");
        dodaj(&pocz, 5, "piec");
        dodaj(&pocz, 6, "szesc");
        dodaj(&pocz, 7, "siedem");
        dodaj(&pocz, 8, "osiem");
        dodaj(&pocz, 9, "dziewiec");
        while(1)
        {
                wypiszliste(pocz);
                printf("\n\nElementow: %d, \nOPCJE:\n1 dodaj \n 2 usun\n 3 usun wszystkie\n", licz(pocz));
                scanf("%d", &wybur);
                if (wybur==1)
                {
                        printf(" podaj liczbe\n");
                        scanf("%d", &l);
                        printf(" podaj opis\n");
                        scanf("%s", ss);
                        dodajmiejsce(&pocz, l, ss);
                }
                if(wybur==2)
                {
                     printf(" podaj liczbe do usuniecia\n");
                     scanf("%d", &l);
                     pocz=usun(pocz, l);
                }
                if(wybur==3)
                {
                    pocz=kasuj(pocz);
                 }
                 if(wybur==4)
                {
                      char namec[30];
                     printf("podaj imie");
                     scanf("%s",namec);
                     find(pocz,namec);
                 }
                 if(wybur==5)
                    {
                        char nameo[30];
                        char namec[30];
                        lista *temporal = NULL;
                        printf("name of client to add");
                        scanf("%s",namec);
                        printf("name of item");
                        scanf("%s",nameo);
                        temporal = find(pocz,namec);
                        dodajprzedmiot(temporal,nameo);
                    }
        }
        return 0;
    }
    Last edited by Creotik; 01-15-2014 at 05:21 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well correct me if I am wrong but if the insert for lista works, then the linked list inside of lista is exactly the same, just that each node of lista contains an item list head. The way that you insert item is exactly the same in concept as what you do for lista.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    Well my understanding of linked lists is really bad, overall I'm pretty new to programming :x. I'm trying to figure it out past 2 days and couldnt manage to make my code for adding inner list work. Could you maybe provide example code or say what exactly im doing wrong in my?

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    To visualize a linked list, I imagine chains. Make a structure that holds the address of the next node of the chain link. To insert at curr, make curr's link be the address of the struct you want to insert and the one you want to insert must have a previous copy of the address curr originally contained.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    3
    Isnt that what i'm doing in my dodajprzedmiot function? Basicaly i find head of outer node and pass it to this function and try to add smth to data. At least I think so :x

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I find your program very difficult to read. It's almost as if it's in another language.

    However, in the function you're having trouble with you are mallocing a new list where it seems you should be mallocing a new item. I.e., something like:
    Code:
    void dodajprzedmiot(lista *temp, char *item_named) {
        if (temp->data == NULL) {
            temp->data = malloc(sizeof(item));
            strcpy(temp->data->item_name, item_named);
            temp->data->next = NULL;
        }else{
            item *pom = temp->data;
            while(pom->data->next)
                pom->data = pom->data->next;
            pom->next = malloc(sizeof(item));
            strcpy(pom->data->next->item_name, item_named);
            pom->data->next->next = NULL;
        }
    }
    I haven't tested this, but you get the idea.

    Also, you should test that malloc actually succeeds before proceeding (it returns NULL if it fails). It's best to make a separate function to malloc, test, and initialize new elements.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with singly linked list
    By saldar05 in forum C Programming
    Replies: 6
    Last Post: 03-08-2013, 12:30 AM
  2. Singly-linked list
    By Alcatar in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2013, 12:46 PM
  3. singly linked list
    By Annonymous in forum C Programming
    Replies: 22
    Last Post: 09-10-2011, 05:50 PM
  4. singly linked list
    By aextine in forum C Programming
    Replies: 2
    Last Post: 06-08-2010, 02:20 PM
  5. singly linked list
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 11-30-2002, 11:19 AM

Tags for this Thread