Hi,
I have some problems with this code.I must make a add person.delete person or search...
I make everything but if i add someone who is added before the program is crash..
Can someone to put new element on the exact place...
for example if i have 1 and 10...i add 5 and this 5 is betwen them..now put like last element..
also i don`t want this function 5 for sorting...
Thanks

#include <string>
#include <iostream>

using namespace std;
 struct student {
                        int fn;
                        string name;
                        student* next;
          } *group = NULL, *last = NULL; 

          void add()
          {
                        student* s=new student;
                        cout<<"Fakulteten nomer = ";
                        cin>>s->fn;
                        cout<<"Ime = ";
                        cin>>s->name;
                        s->next=NULL;
                        if(!group)
                                  group=last=s;
                        else
                                  last->next=s;
                        last=s;
          }  
          void list()

          {
                        student* s=group;
                        while(s)
                        {
                                  cout<<"#"<<s->fn<<" - "<<s->name<<"\n";
                                  s=s->next;
                        }
          } 
          void search() {
                        student* s=group;
                        int search;
                        cout<<"Fakulteten nomer za tyrsene = ";
                        cin>>search;
                        while(s)
                        {
                                  if(search==s->fn)
                                  {
                                                cout<<"Namereno # "<<s->fn<<" - "<<s->name<<"\n";
                                                break;
                                  }
                                  s=s->next;
                        }
          } 
                void sort() {
                   student *lst, *tmp = group, *prev, *potentialprev = group;
                   int idx, idx2, n = 0;
                   student* s=group;
                   while(s)
                        {
                                   n++;
                                  s=s->next;
                        }
                        for (idx=0; idx<n-1; idx++) 
                          {
                                for (idx2=0,lst=group; 
                                         lst && lst->next && (idx2<=n-1-idx);
                                         idx2++)
                                {
                                  if (!idx2)
                                  {
                                        prev = lst;
                                  }
                         
                                  if (lst->next->fn < lst->fn) 
                                  {  
                                        tmp = (lst->next?lst->next->next:0);
                         
                                        if (!idx2 && (prev == group))
                                        {
                                          group = lst->next;
                                        }
                                        potentialprev = lst->next;
                                        prev->next = lst->next;
                                        lst->next->next = lst;
                                        lst->next = tmp;
                                        prev = potentialprev;
                                  }
                                  else
                                  {
                                        lst = lst->next;
                                        if(idx2)
                                        {
                                          prev = prev->next;
                                        }
                                  }      
                                } 
                          }
                          list();
          }
          void del() {
                        student* s=group, *prev=group;
                        int search;
                        cout<<"Vyvedete fakulteten nomer za iztrivane = ";
                        cin>>search;
                        while(s) {
                                  if(search==s->fn) {
                                                prev->next=s->next;
                                                if(s==last) last=prev;
                                                if(s==group) group=s->next;
                                                delete s;
                                                
                                                break; 
                                                }
                           prev=s;
                           s=s->next; 
                        }

          } 
          int main(int argc, char* argv[])
          {
                        int choice;
                        do {
                                  cout<<"\n =============================================";
                                  cout<<"\n1.Dobavi\n2.Pokaji\n3.Tyrsi\n";
                                  cout<<"4.Iztrivane\n5.Sortirane\n6.Izhod\n";
                                  cout<<"\n =============================================";
                                  cout<<"\nIzberete:";
                                  cin>>choice;
                                  switch(choice) {
                                          case 1: add(); break;
                                          case 2: list(); break;
                                          case 3: search(); break;
                                          case 4: del(); break;
                                          case 5: sort(); break;
                                  }
          } while(choice!=6);

          return 0;

        }

Edited 7 Years Ago by vinsbg: n/a

Please clarify. If list starts with 1->10 and you want to add 5 to the list do you want to end up with 1->5->10 or 1->10->5? If 5 already exists in the list do want to allow a second 5 in the list or ignore it if it comes up as an input again?

I wont 1->5->10...
i have 1 next data is 21 for example...if i add 15 i wont this 15 to go betwen...i wont automatically arranged in ascending order...here i have to input number and name...
if i have one time element with 5 i want to allow second 5 but with different names...if the number and name is matching not allowed..

This article has been dead for over six months. Start a new discussion instead.