473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

About Ordered singly linked list..

I'm asking you to help me.
I'm a beginner of studying c++.
I'm trying to make the Singly Linked List(Ordered).
Anyway, I've been debugging all day.
I can't sort it out!!

As you see, I don't know what this is wrong..
When I run this pgm, It can't find the Current Variable.

//
Node *Current = Head;

if (!Head) {
Current = NewNode;
}
//

I think the point Current has Head's Data & Next now.
But It can't find Current variable..
I show you my coding.
If my coding is wrong, please correct it~*

----------------------------------------------------------
// InsertList.cpp
bool List::InsertList(Node *NewNode) {
Node *Previous = NULL;
Node *Current = Head;
if (!Head) // Empty
{
Head = NewNode;
return true;
}
if (NewNode->Data < Head->Data) // The first one.
{
NewNode->Next = Head;
Head = NewNode;
return true;
}
while (Current != NULL && (NewNode->Data > Current->Data)) // Go to Next
Item.
{
Previous = Current;
Current = Current->Next;
}
if (Current->Data == NewNode->Data) {
cout << "Data Redundancy" << endl;
return false;
}
else // In the middle of the list. // the last one.
{
Previous->Next = NewNode;
NewNode->Next = Current;
return true;
}
}
----------------------------------------------------------
//LinkedList.h
#include <iostream.h>
#include <stdlib.h>

class Node {
friend class List;
private:
Node *Next;
int Data;
public:
Node(int i) {
Data = i;
}
~Node() {}
};

class List {
private:
Node *Head;

public:
List() {
Head = NULL;
}
~List() {}
bool InsertList(Node *NewNode);
bool DeleteList(int Key);
void Display();
};
----------------------------------------------------------
Main.cpp
#include "linked_list.h"
void main() {
int MenuNumber, InsertNumber, DeleteNumber;

Node *DataItem;
List A;

cout << "Linked List" << endl;
cout << "Exit: 0 " << endl;
cout << "Insert: 1" << endl;
cout << "Delete: 2" << endl;
cout << "Display: 3" << endl;

cout << "Select Menu : ";
cin >> MenuNumber;

while (MenuNumber != 0) {
switch (MenuNumber) {
case 1:
cout << "Insert Number: ";
cin >> InsertNumber;

DataItem = new Node(InsertNumber);

if (DataItem == NULL) {
cout << "There is no memory" << endl;
exit(0);
}

if (A.InsertList(DataItem))
cout << "Good Insertion" << endl;
else
cout << "Bad Insertion" << endl;
break;

case 2:
cout << "Delete Number: ";
cin >> DeleteNumber;

if (A.DeleteList(DeleteNumber))
cout << "Good Delete" << endl;
else
cout << "Bad Delete" << endl;
break;

case 3:
A.Display();
break;

default:
cout << "Insert Again." << endl;
break;
}

cout << endl;
cout << "Select Menu Again: ";
cin >> MenuNumber;
}
}
---------------------------------------------------------
Display.cpp
#include "linked_list.h"

void List::Display() {
Node *Current = Head;
cout << "List: ";
while (Current != NULL) {
cout << Current->Data << " ";
Current = Current->Next;
}
cout << endl;
}
---------------------------------------------------------
That's it!!~*
Please Help you..
I desperately need your advice & help.. ^^&
Jul 22 '05 #1
4 4691

"HS-MOON" <mo******@paran.com> wrote in message
news:66******************************@localhost.ta lkaboutprogramming.com...
I'm asking you to help me.
I'm a beginner of studying c++.
I'm trying to make the Singly Linked List(Ordered).
Anyway, I've been debugging all day.
I can't sort it out!!
One error spotted see below.

As you see, I don't know what this is wrong..
When I run this pgm, It can't find the Current Variable.

//
Node *Current = Head;

if (!Head) {
Current = NewNode;
}
//

I think the point Current has Head's Data & Next now.
But It can't find Current variable..
I show you my coding.
If my coding is wrong, please correct it~*

----------------------------------------------------------
// InsertList.cpp
bool List::InsertList(Node *NewNode) {
Node *Previous = NULL;
Node *Current = Head;
if (!Head) // Empty
{
Head = NewNode;
Here you need to set NewNode->Next to NULL

NewNode->Next = NULL;
return true;
}


You might have other errors, I didn't check.

john
Jul 22 '05 #2
> ----------------------------------------------------------
// InsertList.cpp
bool List::InsertList(Node *NewNode) {
Node *Previous = NULL;
Node *Current = Head;
if (!Head) // Empty
{
Head = NewNode;
Previuos=NULL; Next=NULL;
return true;
}
if (NewNode->Data < Head->Data) // The first one.
{
NewNode->Next = Head;

NewNode->Previous = Head->Previous;
Head->Previous = NewNode;

....
Make a diagram where you draw all the pointers after the insertion of
the objects before and after the one that got inserted.
Jul 22 '05 #3
I referenced your tips..
Thanks a lot!!!!!

Anyway, at last, I made it!! ㅠ_ㅠ(So impressed)
Is this right?!
Even though the result is right,
sometimes our pgm is not logically right.

So,I'd like you to answer to me whether this code is logically right or
not.~
------------------------------------------------------
bool List::InsertList(Node *NewNode) {
Node *Current;
Node *Previous;

if (Head == NULL) { // Nothing
NewNode->Next = NULL;
Head = NewNode;
return true;
}

Previous = Head;
Current = Head->Next;

if (Head->Data > NewNode->Data) { NewNode->Next = Head;
Head = NewNode;
return true;
}

if (Head->Data == NewNode->Data) {
cout << "Data Redundancy: Insertion Again!" << endl;
return false;
}

while (Current != NULL) {
if (Current->Data > NewNode->Data) { NewNode->Next = Current;
Previous->Next = NewNode;
return true;
}
if (Current->Data == NewNode->Data) { cout << "Data Redundancy:
Insert Again!" << endl;
return false;
} else {
Previous = Current;
Current = Current->Next;
}
}
NewNode->Next = NULL;
Previous->Next = NewNode;
return true;
}

Jul 22 '05 #4

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2r*************@uni-berlin.de...
----------------------------------------------------------
// InsertList.cpp
bool List::InsertList(Node *NewNode) {
Node *Previous = NULL;
Node *Current = Head;
if (!Head) // Empty
{
Head = NewNode;


Previuos=NULL; Next=NULL;
return true;
}
if (NewNode->Data < Head->Data) // The first one.
{
NewNode->Next = Head;

NewNode->Previous = Head->Previous;
Head->Previous = NewNode;

...


No, there is no 'Previous' member. He is implementing a singly linked list.
Check his Node class.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
13
by: Raj | last post by:
Is there any way to delete a particular node from a singly linked list where the header of the list is unknown.Only pointer available is the one which points to the node to be deleted
59
by: Anando | last post by:
Hi, I have a linear singly linked list of 100 elements. I would like to prune it such that only user specified elements (say only the elements 1, 13, 78 and 100 of the original list) survive...
3
by: malik | last post by:
// Linked Lists in classes(excluding structures) without using tail pointer # include<iostream.h> # include<stdlib.h> void Swap(int num1, int num2) { int a = num1; num2 = num1; num2 = a;
6
by: Alien | last post by:
Hi, I am having problem with ordering a singly linked list. Since a singly linked list goes only one way, is it possible to order them? If my structs look like the one below. struct block...
3
by: jou00jou | last post by:
Hello, I am trying to sort a singly linked list for the following typedef typedef struct message { int messageId; char * messageText; struct message * next; } message; I have successfully...
23
by: Himanshu Chauhan | last post by:
Hi! I was wondering, In the first parse of a singly linked list of unknown length, is it possible to know when we are at middle of the linked list? Regards --Himanshu
4
by: saki | last post by:
How do we reverse a singly linked list without using extra memory.Extra pointers can however be used
7
by: davidson1 | last post by:
Hello friends, I want ur help regarding singly linked list in C,I tried in net,But it is confusing and difficult.I want singly linked list in a simple way of Understanding.Please Help Me I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...

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.