473,883 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sorting linked list in alphabetical order HELP!

41 New Member
I'm confused as to why this isnt working.. am i doing this right?? I have to create a function that does the following:

- Insert a word to the linked list so that the list
// remains sorted alphabetically.
// PRE: The list is sorted alphabetically or is NULL
// POST: The resulting list is sorted alphabetically. If the word is
// in the list already, do nothing.

struct WordNode {
string word;
WordNode *link;
};

here is what i have........

{
while(!list)
{
WordNode *tempNode = new WordNode;
tempNode->word=word;
tempNode->link=list;
list=tempNode;
}
// do a deep copy of list to current

WordNode *current, *lastNode;
current = new(WordNode);
current->word = list->word;
current ->link = NULL;
lastNode = current;
list = list -> link;
while (list!=NULL)
{
WordNode *p = new(WordNode);
p -> word = list -> word;
p->link = NULL;
lastNode -> link = p;
lastNode = p;
list = list -> link;
}

// now sort in alphabetical order

WordNode *last;
while(current!= NULL && current->word<word)
{
last=current;
current=current->link;
}
WordNode*tempNo de = new WordNode;
tempNode->word=word;
//cout << "temp-> word " << tempNode->word << endl;
last->link=tempNod e;
tempNode->link=current ;}

Am I way off track??? it keeps coming up with an error when I try to compile. I can see the problem lies somewhere within

last->link=tempNode. .......

????
May 14 '09 #1
5 12424
scruggsy
147 New Member
I don't see a syntax error on that line. To be honest, assuming list and word are defined, the code ought to compile. Can you post the text of the error message you are receiving and confirm the line referenced by the error message?

Your code seems rather complicated for such a simple task. I think you can reduce it to a single, short loop.

I am assuming you intend to add code to delete the WordNodes you've allocated with new.
May 14 '09 #2
kylie991
41 New Member
yes I will have to do that too.. I'm only new to this :) will get the errors...
May 14 '09 #3
kylie991
41 New Member
it just says filename.exe has stopped working. Windows can check online for a solution to the problem.

so there is no syntax error being shown. could i possibly be going out of bounds???

originally I tried to use this code to sort it but it didnt seem to work!

while(!list)
{
WordNode *tempNode = new WordNode;
tempNode->word=word;
tempNode->link=list;
list=tempNode;
}

node *current=head,* last;
while(current!= NULL && current->word< word)
{
last=current;
current=current->link;
}
node *temp;
temp->word=word;
last->link=temp;
temp->link=current ;

if I left this code as node*temp.. then it crashes. So I created temp new Wordnode to stop this from happening...
May 14 '09 #4
scruggsy
147 New Member
@kylie991
Okay. You are getting a run-time error then, not a compile-time error as you indicated in your first post.
In this bit:
Expand|Select|Wrap|Line Numbers
  1. WordNode *last;
  2. while(current!=NULL && current->word<word)
  3. {
  4. last=current;
  5. current=current->link;
  6. }
...it is possible for last to never become initialized with the address of a WordNode if execution never enters the body of your while loop, which would cause a crash later if you try to use last.

I'm unclear about what you're trying to do with this bit:
Expand|Select|Wrap|Line Numbers
  1. while(!list)
  2. {
  3. WordNode *tempNode = new WordNode;
  4. tempNode->word=word;
  5. tempNode->link=list;
  6. list=tempNode;
  7. }
This loop can only run at most once, because you assign to list within the body, making !list false - is that intentional (if so, why not just use an if?)

I suggest writing out the logical steps you need to take, in English (or whatever natural language you're most comfortable with), and making sure you've covered all possible cases before moving on to code. That way you won't get bogged down in all the pointer stuff before you've got a handle on the actual problem.
May 14 '09 #5
kylie991
41 New Member
oh sorry i get confused with the terminology.. Ive worked it out anyway :) but thanks for trying to help!
May 14 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2537
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys() sorted_feature_vector.sort() #feature_vector.keys()=sorted_feature_vector
5
2343
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set up the class link and tel_list. I set up a class person with strings for name, town, and number. I just don't know how to set up the classes w/ their methods (constructors and operations). I'm stuck on the assignment operator and the add and...
2
1861
by: Neil Hindry | last post by:
I wonder if you can help me. I have been using the very basic functions of Access XP for a while now but I have just started delving into forms etc. I have created a form to enter my data and I have a couple of pull down menus (I think they are called Combo boxes) so that I can only select from a list of data for that field. What I cannot seem to do is make this list appear in alphabetical order.
4
2078
by: Peter Schmitz | last post by:
Hi, in my application, I defined a linked list just as follows: typedef struct _MYLIST{ int myval; void *next; }MYLIST; MYLIST head;
6
2269
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ---------------------------(end of broadcast)--------------------------- TIP 5: Have you checked our extensive FAQ? http://www.postgresql.org/docs/faqs/FAQ.html
2
7000
by: Susan.Adkins | last post by:
Alright, I must say that the problem my teacher has given us to do has royally annoyed me. We are to take a letter *txt file* and then read in the words. Make 2 seperate lists *even and odd* for the number of occurances... so the first time a word is entered its going to have an occurance of one and be in the odd list then it is going to go to the next word see if its in the odd list. If its not its going to check the even list. if...
20
20926
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement sorting for LinkedList<myself? Thanks in advance Martin
3
2401
by: Don | last post by:
I have a "Report" that is created from a "Form". It prints a list of items, you may consider it a shopping list. In any event I use to run this in alphabetical order but have since decided to run it as it comes from the form (random order). My problem is I don't know how to make this happen. The report is sorted in ascending order and I don't know how to change that. I see only two options, ascending or descending. Can you help?...
9
4919
by: Sheldon | last post by:
Hi, I am trying to understand linked lists and the different ways to write a linked list and double linked list. I have been trying to get this function called insert_word to work but to no avail. The function receives a string from main and then inserts in the list the new word in alphabetical order. Here is my function: struct word { // double linked list. Here the list is global and is first built
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7129
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3233
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.