4 Replies - 5381 Views - Last Post: 19 May 2010 - 10:36 AM Rate Topic: -----

#1 zigurdis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-November 09

sorted linked list in c++, in Sequential Access File

Posted 19 May 2010 - 08:53 AM

i neet to make c++ program,who realize sorted linked list, in linked list should be names, that i have, but i cant sort?
i made that program for numbers, then thats worked...but no for characters it doesnt...

/* //linked_list3.cpp
/*********************************************************

*********************************************************/
#include <iostream>
using namespace std;



class elem
{
public:
   char num[20];
   elem *next;
   elem (const char* n="") { strcpy(num, n); next = NULL; };
};


class List
{
protected:
    elem *first, *last;
public:
    List () { first = last = NULL; };
    elem *current;
    void add_element (char *n);
    void delete_element ();
    void BubbleSort();
    int is_empty () { return (first == NULL); };
    void start () { current = first; };
    int end () { return (current == NULL); };
    void next () { current = current -> next; };
};

int main()
{
  List l;
  char k[20];
  cout << "enter names:\n";
  cin >> k;
  while (!cin.eof())
  {
    l.add_element (k);
    cout<< "press ctrl+z to stop enter names"<<endl;
    cin >> k;
  };
  
  for (l.start(); !l.end(); l.next())
  {
    cout << l.current->num << endl;
  }
  
  l.BubbleSort();
  cout << "sakaartots" << endl;
   for (l.start(); !l.end(); l.next())
  {
    cout << l.current->num << endl;
  }
  while (!l.is_empty())
  {
    l.delete_element ();
  };
  system ("PAUSE");
};

void List::add_element (char *n)
{
    elem *p = new elem (n);
    if (first == NULL) first = last = p;
    else last = last -> next = p;
};

void List::delete_element ()
{
    elem *p = first;
    first = first -> next;
    delete p;
};

void List ::BubbleSort()
{
  int i,j,m,n=0;
  char hold[20];
  elem *q, *p, *t;
  for(elem*q = first ; q ; q=q->next)   //balad shodid;
    ++n;

  for(i=1 , t = first  ; i<=n-1 ;  t = t->next , ++i)
    for( j=0 , p = first  ; j<n-i ;  p = p->next , ++j)
       if(p->num > (p->next)->num)
       {

	 strcpy(hold, p->num);
      
        strcpy(p->num , (p->next)->num);
       
          strcpy((p->next)->num, hold);
       }
}
 */


This post has been edited by baavgai: 19 May 2010 - 09:48 AM
Reason for edit:: tagged


Is This A Good Question/Topic? 0
  • +

Replies To: sorted linked list in c++, in Sequential Access File

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: sorted linked list in c++, in Sequential Access File

Posted 19 May 2010 - 09:08 AM

because the values in the linked list are strings not numbers you should compare them with the strcmp function not the relational operators (<,<= and so on).

This post has been edited by muballitmitte: 19 May 2010 - 09:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 zigurdis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-November 09

Re: sorted linked list in c++, in Sequential Access File

Posted 19 May 2010 - 10:04 AM

but function strcmp compare aal name, but a should order all linked list, with different names
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: sorted linked list in c++, in Sequential Access File

Posted 19 May 2010 - 10:13 AM

When you compare
if(p->num > (p->next)->num)


you are comparing the address of p->num with the address of p->next->num.

Instead, you need
if( strcmp(p->num, p->next->num) > 0 )


Was This Post Helpful? 0
  • +
  • -

#5 zigurdis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 05-November 09

Re: sorted linked list in c++, in Sequential Access File

Posted 19 May 2010 - 10:36 AM

thanks:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1