Strona główna > C++ > Lista w C (struktury danych)

Lista w C (struktury danych)

26 Czerwiec 2011 Dodaj komentarz Go to comments
//dodawanie, wyswietlanie, szukanie, usuwanie elementow
#include <stdio.h>
typedef struct L
{
        int key;
        struct L *next;
}L;

void show (L *head)
{
     if(head != NULL){
     while (head != NULL){
           printf("%d ", head->key);
           head=head->next;
           }
           printf("\n");
     }
     else printf("No element to show...\n");
}

void search (L *head, int Find)
{
     int i=1;
     L *tmp=head;
     while (tmp!= NULL && tmp->key!=Find){
           tmp=tmp->next;
           i++;
           }
           if (tmp!=NULL){
           printf("Element %d is on %d place ", tmp->key, i);
           printf("\n");}
     else printf("Element does not exist\n");
}

L *add (L *head, int New)
{
       L *tmp, *nowy, *cien;
       int tmp2;
       nowy = (L *)malloc(sizeof(L));
       nowy->key = New;
       nowy->next=NULL;

       if (head==NULL){
          head=nowy;
          }
          else if (head->key >= New){
               nowy->next=head;
               head=nowy;
               }
       else{
            tmp=head;
            while(tmp!=NULL && tmp->key<New){
            cien=tmp;
            tmp=tmp->next;
            }           
            cien->next=nowy;
            nowy->next=tmp;
            }
       return head;
}    

L *Delete (L *head, int Del)
{

      L *tmp, *tmp2, *pom;
      tmp = head;
      if(head==NULL){
          printf("No element to delete...\n");
          return head;
            }
      else if(head->next == NULL && head->key == Del){
           free(head);
           head = NULL;
           return head;
            }
      else{
           if (tmp->key== Del){
                   pom=tmp->next;
                   free(head);
                   return pom;
                   //
                   }
           else{
               tmp2=tmp->next;
               while (tmp2->next!= NULL && tmp2->key != Del){
                     tmp=tmp->next;
                     tmp2=tmp->next;
                     }
               if (tmp2->next== NULL){
                     printf("Element does not exist...\n");
                     }
               else if(tmp2->next==NULL){  //jesli ostatni
                    free(tmp2);
                    tmp->next = NULL;
                    //
                     }
               else{
                    tmp->next = tmp2->next;
                    free(tmp2);
                    tmp2 = NULL;
                    }
               }

           }
         return head;
}

int main ()
{
    int option=0, New, Del, Find;
    L *my_list;
    my_list = ( L *)malloc(sizeof(L));
    my_list = NULL;

    while(option!=5)
    {

            printf("\n1.ADD\n2.Show\n3.Search\n4.Delete\n5.END\n");
            scanf("%d",&option);

            switch(option)
            {
               case 1: printf("Type value\n"); scanf("%d",&New);
               my_list=add(my_list,New); break;
               case 2: show(my_list); break;
               case 3: printf("Type value to find\n"); 
                       scanf("%d",&Find);
                       search(my_list,Find); break;
               case 4: printf("Enter the value to remove\n"); 
                       scanf("%d",&Del); 
                       my_list=Delete(my_list,Del); break;
               case 5: option = 5;break;
               default: break;
            }

    }
    return 0;
}
  1. 22 Czerwiec 2013 o 12:30 am

    Woah this website is wonderful i adore researching your content regularly. Maintain the great works of art! You realize, lots of individuals want all-around for this facts, you could make them greatly.

  2. 22 Czerwiec 2013 o 4:36 am

    I am actually thankful to the owner of this site who has
    shared this wonderful piece of writing at here.

  3. 13 Marzec 2014 o 6:29 am

    obviously like your web site but you have to test the spelling
    on several of your posts. Many of them are rife with spelling
    issues and I find it very bothersome to tell the reality then again I’ll certainly come back again.

  4. 19 Wrzesień 2014 o 5:50 am

    Write more, thats aall I have to say. Literally, it seems as though you
    relied on the video to make your point. You clearly know what youre talking about,
    why throw away you intelligence on just posting videos too your blog when you
    could be giving us something informative to read?

  5. 20 Wrzesień 2014 o 5:34 pm

    I’m gone to convey my little brother, that he should also pay a quick visit this webpage
    on regular basis to get updated from latest information.

  6. 26 Wrzesień 2014 o 1:21 pm

    Have you ever considered writing an e-book or guest authoring on other
    websites? I have a blog based upon on the same information you discuss and
    would really like to have you share some stories/information. I know
    my viewers would appreciate your work. If you’re even remotely interested, feel
    free to send me an email.

  7. 26 Wrzesień 2014 o 4:02 pm

    great points altogether, you simply received a emblem
    new reader. What may you recommend in regards to your publish that you simply made a few days in the past?
    Any positive?

  8. 30 Październik 2014 o 10:29 am

    Your backyard is one of the moost important places and you need to take care of it.
    Almost every Michigan home landscape plan includes
    shrubs but unfortunately whjen you look around a
    new subdivision almost all the homes wiill include the same typpes of shrubs.
    It iss important to chopse plants that you know will flower or give colour all year.

  9. 9 Grudzień 2014 o 9:55 pm

    Ahh very interesting. – learn about new
    things every day.

  10. 10 Kwiecień 2016 o 4:03 pm

    Thanks for the marvelous posting! I quite enjoyed reading
    it, you’re a great author.I will always bookmark your blog and may come back
    at some point. I want to encourage you to continue your great writing, have a nice weekend!

  11. 14 Maj 2016 o 3:04 am

    It’s very simple to find out any topic on net as compared to books,
    as I found this piece of writing at this web site.

  12. 14 Maj 2016 o 3:31 am

    I couldn’t resist commenting. Perfectly written!

  13. 27 Maj 2016 o 2:10 am

    Hi there would you mind letting me know which hosting company you’re using?

    I’ve loaded your blog in 3 completely different web browsers and I
    must say this blog loads a lot quicker then most. Can you recommend a good web hosting provider at a
    reasonable price? Thanks, I appreciate it!

  14. 4 Czerwiec 2016 o 1:39 pm

    What’s up friends, its enormous paragraph regarding
    tutoringand fully defined, keep it up all the
    time.

  15. 7 Czerwiec 2016 o 11:40 pm

    Magnificent site. Plenty of helpful information here. I am sending it
    to a few friends ans additionally sharing in delicious.
    And certainly, thanks for your sweat!

  16. 7 Czerwiec 2016 o 11:41 pm

    Hello mates, nice paragraph and good arguments
    commented here, I am truly enjoying by these.

  17. 10 Czerwiec 2016 o 1:40 am

    I delight in, result in I found just what I used to be taking a look for.
    You have ended my four day long hunt! God Bless you man. Have a nice day.

    Bye

  18. 10 Czerwiec 2016 o 2:13 am

    Hello, I think your site might be having browser compatibility issues.

    When I look at your blog site in Ie, it looks fine but when opening in Internet Explorer, it
    has some overlapping. I just wanted to give you a quick
    heads up! Other then that, awesome blog!

  19. 10 Czerwiec 2016 o 12:00 pm

    I think this is one of the such a lot important info for me.
    And i am glad reading your article. However want to commentary
    on some basic things, The web site style is perfect, the articles is actually great : D.
    Just right task, cheers

  20. 11 Czerwiec 2016 o 12:43 am

    Good blog post. I certainly love this site. Continue the good work!

  21. 12 Czerwiec 2016 o 1:24 pm

    Very good blog you have here but I was curious about if you knew of any discussion boards that
    cover the same topics talked about here?
    I’d really like to be a part of group where I can get feed-back from other knowledgeable individuals that share the same interest.
    If you have any suggestions, please let me know.
    Thank you!

  22. 17 Czerwiec 2016 o 11:47 pm

    Excellent pieces. Keep posting such kind of info on your site.
    Im really impressed by it.
    Hey there, You have performed a great job.

    I’ll definitely digg it and in my opinion recommend to
    my friends. I am sure they will be benefited from this website.

  23. 21 Czerwiec 2016 o 2:39 am

    whoah this blog is fantastic i love studying your articles.
    Keep up the good work! You realize, a lot of
    individuals are searching round for this information, you can aid them greatly.

  24. 26 Czerwiec 2016 o 1:22 am

    wonderful post, very informative. I wonder why
    the opposite experts of this sector do not understand this.
    You must proceed your writing. I am sure, you have a great readers’ base already!

  25. 27 Czerwiec 2016 o 1:35 am

    excellent points altogether, you simply won a new reader.
    What could you recommend in regards to your publish that
    you simply made some days in the past? Any certain?

  26. 27 Czerwiec 2016 o 11:53 pm

    Hi there everyone, it’s my first visit at this website,
    and post is truly fruitful in favor of me, keep up posting these types of articles or reviews.

  27. 28 Czerwiec 2016 o 3:14 pm

    Hi there to all, for the reason that I am in fact keen of reading this webpage’s post to
    be updated daily. It includes pleasant information.

  28. 11 Lipiec 2016 o 2:06 am

    Hello would you mind letting me know which hosting company you’re
    using? I’ve loaded your blog in 3 different internet browsers and I must say
    this blog loads a lot faster then most. Can you recommend a good web hosting provider at
    a reasonable price? Many thanks, I appreciate it!

  29. 24 Sierpień 2016 o 11:55 am

    certainly like your website however you need to test
    the spelling on several of your posts. A number of them are
    rife with spelling problems and I in finding it very troublesome to tell the reality then again I will
    certainly come again again.

  30. 24 Sierpień 2016 o 5:31 pm

    I don’t even know how I ended up right here, however I assumed
    this submit was good. I do not realize who you’re but definitely you’re going to
    a famous blogger if you aren’t already. Cheers!

  1. 12 Październik 2014 o 4:37 am
  2. 22 Maj 2016 o 12:57 pm
  3. 17 Lipiec 2016 o 1:13 pm
  4. 13 Sierpień 2016 o 4:02 am
  5. 13 Sierpień 2016 o 4:23 am
  6. 13 Sierpień 2016 o 5:27 am
  7. 13 Sierpień 2016 o 5:29 am
  8. 30 Wrzesień 2016 o 2:52 pm
  9. 16 Październik 2016 o 1:19 pm

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Log Out / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Log Out / Zmień )

Facebook photo

Komentujesz korzystając z konta Facebook. Log Out / Zmień )

Google+ photo

Komentujesz korzystając z konta Google+. Log Out / Zmień )

Connecting to %s

%d bloggers like this: