GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Apr-2007, 22:10
kracivi kracivi is offline
New Member
 
Join Date: Mar 2007
Posts: 7
kracivi is on a distinguished road

Linked Lists problem


I wrote this code which inserts a new node in a sorted linked list
CPP / C++ / C Code:
struct node* SortedInsert(struct node* head, int newData)
{
// Special case for the head end
if ( head == NULL || head ->data >= newData)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate node
newNode->data = newData; // put in the data
newNode->next = head;
head = newNode;
}
else
{
// Locate the node before the point of insertion
struct node* current = head;
while (current->next!=NULL && current->next->data<newData)
current = current->next;
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate node
newNode->data = newData; // put in the data
newNode->next = current->next;
current->next = newNode;
}
return head;
Also i have this code for merging two linked lists
CPP / C++ / C Code:
struct node* SortedMerge(struct node* a, struct node* b)
{
struct node dummy; // a dummy first node to hang the result on
struct node* tail = &dummy; // Points to the last result node so tail->next is the place to add new nodes
to the result.
dummy.next = NULL;
while (1)
{ // if either list runs out, use the other list
if (a == NULL)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate node
newNode->data = b->data; // put in the data
newNode->next = NULL;
tail->next = newNode;
b = b->next;
}
else if (b == NULL) {
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate node
newNode->data = a->data; // put in the data
newNode->next = NULL;
tail->next = newNode;
a = a->next;
}
else
if (a->data <= b->data)
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate
node
newNode->data = a->data; // put in the data
newNode->next = NULL;
tail->next = newNode;
a=a->next;
}
else
{
struct node* newNode = (struct node*) malloc(sizeof(struct node)); // allocate
node
newNode->data = b->data; // put in the data
newNode->next = NULL;
tail->next = newNode;
b=b->next;
}
tail = tail->next;
if (a == NULL && b == NULL)
break;
}
return(dummy.next);
}
Now i have to write a programm to check if these functions work properly...I have to initialize the two sorted linked lists and use the SortedInsert and SortedMerge to see if they work...
The printing of the list i think will be like this:
CPP / C++ / C Code:
void PrintList(struct node* head)
{
struct node* current = head;
while (current != NULL)
{
printf("%d ", current->data);
current = current->next;
}
printf("\n\n");
}
Some help in initializing the links and using the functions????????
Last edited by LuciWiz : 01-May-2007 at 11:56. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-May-2007, 08:40
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Linked Lists problem


Quote:
Originally Posted by kracivi
Some help in initializing the links and using the functions?

Maybe something like:

CPP / C++ / C Code:
#include <stdio.h>
struct node {
    int data;
    struct node *next;
};

int main()
{
    void PrintList(struct node* head);
    struct node* SortedInsert(struct node* head, int newData);

    int dataitem;

    struct node *list1 = NULL; /* Head number 1 == NULL for empty list */
    struct node *list2 = NULL; /* Head number 2 == NULL for empty list */

    /* get stuff into list number 1
     * by getting integer dataitems and repeatedly executing
     * the following:
     */
    /* some kind of loop {*/
         list1 = SortedInsert(list1, dataitem);
    /* } end of list1 stuff */

    /* get stuff into list number 2
     * by getting integer dataitems and repeatedly executing
     * the following:
     */
    /* some kind of loop {*/
        list2 = SortedInsert(list1, dataitem);
    /* } end of list2 stuff */

    printf("List number 1:\n");
    PrintList(list1);
    printf("\nList number 2:\n");
    PrintList(list2);

    /* Now call the merge/sort routine and print the results
     * printf("Merged list:\n");
     * PrintList(...
     */

    return 0;
}


Regards,

Dave
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
Doubly linked lists in C++ sweeeeeeetlipss C++ Forum 1 23-Oct-2006 23:27
still problem with polynom linked list if13121 C Programming Language 7 22-Nov-2004 23:02
help on linked lists any1????? nick4 C Programming Language 1 17-May-2004 09:32
linked lists, newbie needs help moltarim C Programming Language 4 06-May-2004 11:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:00.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.