-1

Thanks

#include <stdio.h>
#include <stdlib.h>

struct Node{
int data;
Node *link;
};

typedef Node* NodePtr;

void addToEnd(NodePtr* head, int val);
void printList(NodePtr head);

NodePtr mergeLists(NodePtr* list1, NodePtr* list2);

int main()
{
int nextInt;

NodePtr list1 = NULL;
NodePtr list2 = NULL;

printf( "Enter integers in sorted order for first list, -1 to end\n");
scanf("%d, &nextInt");
while (nextInt != -1)
{
addToEnd(list1, nextInt);
scanf("%d, &nextInt");
}

printf("Enter integers in sorted order for second list, -1 to end\n");
scanf("%d, &nextInt");
while (nextInt != -1)
{
addToEnd(list2, nextInt);
scanf("%d, &nextInt");
}

NodePtr mergedList = mergeLists(list1, list2);

printf("Merged list\n");
printList(mergedList);
printf("\nOriginal lists");
printf("\nlist1: ");
printList(list1);
printf("\nlist2: ");
printList(list2);
printf(endl);
}

void addToEnd(NodePtr* head, int val)
{
NodePtr newNode = new Node;
newNode->data = val;
newNode->link = NULL;

if (head == NULL)
head = newNode;
else
{
NodePtr temp = head;
while (temp->link != NULL)
temp = temp->link;
temp->link = newNode;
}
}

void printList(NodePtr head)
{
if (head != NULL)
{
printf( " ",head->data);
printList(head->link);
}
return 0;
}
Votes + Comments
ROMANES EUNT DOMUS - That's an order!
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Ancient Dragon
0

what are the mistakes? what are the error messages ? what operating system? what compiler ? In otherwords, don't just toss some code out there and expect everyone to correct it. You need to explain what you want or what the problems are.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.