superben
2nd Mar 2008 Sun, 14:31
:help:linked list implementation:struct _node_t {
char cData[64];
struct _node_t *pNext;
};
/* alias of struct _node_t*/
typedef struct _node_t node_t;
ngayon, kelangan ko ng sorting algo para mai-arrange yung linked list' cData ko alphabetically. salamat sa makakatulong, i owe you my life! :yipee:
Sahdow_Strik3
3rd Mar 2008 Mon, 04:37
wohooo!!! C language
i google mo nalang yan...
hints lang... mahirap mag sort ng may laman na yung list...
para mas madali... i sort mo na siya habang humihingi palang ng input...
hmx_ryan
3rd Mar 2008 Mon, 08:58
Alphabetizing Theory
To sort a list of words into alphabetically ascending order, we need to be able to compare them. Since C provides a library known as string.h, we should use this. In string.h there is a simple comparison function called strcmp. The strcmp function takes two strings and returns:
* -1 if string A is less than string B
* 0 if the strings are equal
* 1 if string B is less than string A
In other words, if string A were to come before string B in a dictionary, then strcmp would return -1. Thus, the majority of the hard work in alphabetizing is done for us. There is, however, a caveat - if the strings are not both in the same case, then the strcmpi (the i standing for insensitive) should be used.
Alphabetizing in Practice : Bubble Sort
A bubble sort algorithm is one of the simplest forms of sorting known to programmers. It basically allows high values to 'bubble' up through the rest, by repeatedly comparing neighboring values, and swapping them. Consider the list:
* D
* B
* C
* A
If we wanted to sort this list to yield 'ABCD', we could use a bubble sort and compare neighbors, swapping the values if one was 'less' than the other. In the first pass, we would end up with the following:
* B
* C
* A
* D
This list is the result of swapping neighbors successively, starting at the first item in the list, and progressing through it until the last pair has been compared. The 'D' has bubbles up (or down) through the values, being swapped each time because each other value was 'less' than 'D':
* D B C A : Swap D & B
* B D C A : Swap D & C
* B C D A : Swap D & A
* B C A D : End
This, however, is not the end of the story, since the list is now better sorted than it was before, but not quite correctly sorted. In fact, we need to apply the bubble sort algorithm repeatedly, until no more swaps take place.
In C, this might look akin to:
do
{
Swap = 0;
for (i = 1, i < nStrings; i++)
{
if (swap(szStrings[i-1], szStrings[i], -1) == 1)
{
nSwap++;
}
}
} while (nSwap == 0);
We assume that the swap function performs a compare and swap based on two strings and a direction. This function would be different depending on the elements of the list that we wish to sort.
The Linked List Implementation
The above is fine, but it only provides a way of sorting a two dimensional list : a list of strings. What we need is a linked list, and so we need to redefine the data structures and algorithms used to manipulate the list.
To do this, we create a node structure in C, containing:
* The string
* A pointer to the next node
Each node is then initialized with the string that it needs to contain, and a NULL pointer. When we need to add a node, all that has to be done is to create a new node, and point the 'next' member to the head of the existing list.
Swapping then becomes a case of redirecting these pointers such that the nodes are reversed in their order within the list. This will require two steps:
* Point the next member of Node A to the node after Node B
* Point the next member of Node B to Node A
Note that, unlike processing an array, no copy of the data to be swapped is taken, as only a reference to the data elemnt is retained. This makes the bubble sort process marginally more efficient, but still far less efficient than other, more specialized, sorting algorithms.
Source:Alphabetizing a linked list in C (http://computerprogramming.suite101.com/article.cfm/alphabetizing_a_linked_list_in_c)
This might help..This is a simple example that could help you a lot... This code also helps me in my sorting problems.. please do study it... :)
PINOY_RADICAL
14th Mar 2008 Fri, 15:41
eto yung source ko ng linked list! i-run mo. magrarun yan pare.
#include <iostream.h>
#include <conio.h>
struct Node
{ int empNum;
char empFname[50];
char empLname[50];
float empSalary;
Node *next;
};
Node *head = NULL;
Node *current = NULL;
void addLast()
{ Node *newNode = new Node;
cout << "\nEmployee Number: ";
cin >> newNode->empNum;
cout << "Employee First Name: ";
cin >> newNode->empFname;
cout << "Employee Last Name: ";
cin >> newNode->empLname;
cout << "Employee Salary: ";
cin >> newNode->empSalary;
if(head == NULL)
{ head = current = newNode;
newNode->next = NULL;
}
else
{ Node *walker = head;
while (walker->next != NULL)
walker = walker->next;
walker->next = newNode;
newNode->next = NULL;
}
}
void addStart()
{ Node *newNode = new Node;
cout << "\nEmployee Number: ";
cin >> newNode->empNum;
cout << "Employee First Name: ";
cin >> newNode->empFname;
cout << "Employee Last Name: ";
cin >> newNode->empLname;
cout << "Employee Salary: ";
cin >> newNode->empSalary;
current = newNode;
if(!head)
{ head = newNode;
newNode->next = NULL;
}
else
{ newNode->next = head;
head = newNode;
}
}
void deleteStart()
{ if (head == NULL)
return;
else
{ if(head == current)
current = head->next;
Node *oldHead = head;
head = head->next;
delete oldHead;
}
}
void deleteLast()
{ if(head == NULL)
return;
else
{ Node *walker = head;
while(walker->next != NULL)
walker = walker->next;
if(head == walker)
{ head = current = NULL;
delete walker;
}
else
{ Node *walker2 = head;
while(walker2->next != walker)
walker2 = walker2->next;
if(current == walker)
current = walker2;
walker2->next = NULL;
delete walker;
}
}
}
void displayList()
{ Node *walker = head;
if(head == NULL)
cout << "\n\nThe list is empty!\n\n";
else
{ cout << "\n\n";
while (walker != NULL)
{ cout << "Emp. No: " << walker->empNum << " "
<< "Emp. Name: " << walker->empFname << " "<< walker->empLname <<
" "
<< "Salary : " << walker->empSalary;
if(walker == current)
cout << " <-- Current";
cout << endl;
walker = walker->next;
}
cout << "--- End of list! ---\n\n";
}
}
void moveForward()
{ if (head == NULL || current->next == NULL)
cout << "\nYou are at the end of the list.";
else
current = current->next;
}
void moveBackward()
{ if (current == head)
cout << "\nYou are at the start of the list";
else
{ Node *walker = head;
while (walker->next != current)
walker = walker->next;
current = walker;
}
}
void showMenu()
{ cout << "Please select an option: "
<< "\n1. Add a Node at the beginning of the list."
<< "\n2. Add a Node at the end of the list."
<< "\n3. Delete the first Node on the list."
<< "\n4. Delete the last Node on the list."
<< "\n5. Move the current pointer on one Node."
<< "\n6. Move the current pointer back one Node."
<< "\n7. Exit the program."
<< "\n\n--->>> ";
}
int main()
{ clrscr();
for(;;)
{ showMenu();
switch(getche()-48)
{ case 1: addStart();
displayList();
break;
case 2: addLast();
displayList();
break;
case 3: deleteStart();
displayList();
break;
case 4: deleteLast();
displayList();
break;
case 5: moveForward();
displayList();
break;
case 6: moveBackward();
displayList();
break;
case 7: return 0;
default: cout << "\n\nInvalid Choice!\n\n";
}
}
}