Hackety Hacking Problem Compare and copy: Difference between revisions
(cat) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 14: | Line 14: | ||
== Curriculum Connections == |
== Curriculum Connections == |
||
To be done....... |
|||
1. Rudimentary knowledge of Data Structures |
|||
2. Creating, Traversing and Deletion of Nodes of Linked list |
|||
3. Using Recursion to solve problems |
|||
== Solution == |
== Solution == |
||
Commenting is left....... |
|||
<pre> |
<pre> |
||
/* CODE tested on Turbo C++ 3.0 Compiler |
|||
20th October 2007 |
|||
*/ |
|||
//Include necessary files (for defining prototypes of functions used) |
|||
#include<stdio.h> |
#include<stdio.h> |
||
#include<conio.h> |
#include<conio.h> |
||
#include<stdlib.h> |
#include<stdlib.h> |
||
typedef struct node // A structure representing a node of Linked List and re-naming it as node(Using typedef) |
|||
typedef struct node |
|||
{ |
{ |
||
int info; |
int info; |
||
Line 32: | Line 38: | ||
node; |
node; |
||
node *sptr1=NULL, *sptr2=NULL; |
node *sptr1=NULL, *sptr2=NULL; // Initialize the 2 Linked List pointer to header nodes with NULL(Empty Linked List) |
||
int menu() |
int menu() |
||
{ |
{ |
||
int ch; |
int ch; |
||
printf("\ |
printf("\nEnter your choice\n"); |
||
printf("1. |
printf("1.Add node\n2.Compare lists\n3.Traverse list\n4.Copy link list\n5.Exit\n"); |
||
scanf("%d",&ch); |
scanf("%d",&ch); // Menu Driven Program |
||
return ch; |
return ch; |
||
} |
} |
||
node *addnode(node *sptr) |
node *addnode(node *sptr) // When a new node is to be added in Linked List |
||
{ |
{ |
||
node *temp; |
node *temp; |
||
Line 49: | Line 55: | ||
temp->ptr=NULL; |
temp->ptr=NULL; |
||
temp->info=NULL; |
temp->info=NULL; |
||
printf("enter the no."); |
printf("enter the no."); // Asking user to enter data in node |
||
scanf("%d",&temp->info); |
scanf("%d",&temp->info); |
||
if(sptr==NULL) // Condition to check whether the node being added is 1st node |
|||
if(sptr==NULL) |
|||
sptr=temp; |
sptr=temp; |
||
else |
else |
||
Line 58: | Line 64: | ||
sptr=temp; |
sptr=temp; |
||
} |
} |
||
return sptr; |
return sptr; // returns the pointer to 1st node of List. |
||
} |
} |
||
Line 68: | Line 74: | ||
ptr2=ptr2->ptr; |
ptr2=ptr2->ptr; |
||
ptr1=ptr1->ptr; |
ptr1=ptr1->ptr; |
||
compare(ptr1,ptr2); |
compare(ptr1,ptr2); // Recursive function call. |
||
} |
} |
||
if(ptr1==NULL && ptr2==NULL) |
if(ptr1==NULL && ptr2==NULL) |
||
{ |
{ |
||
printf("the link lists |
printf("the link lists are equal"); |
||
return (0); |
return (0); |
||
} |
|||
else if(ptr1==NULL) |
else if(ptr1==NULL) |
||
{ |
{ |
||
printf("linklist 1 is smaller and not equal"); |
printf("linklist 1 is smaller and not equal"); |
||
return(0); |
return(0); |
||
} |
|||
else if(ptr2==NULL) |
else if(ptr2==NULL) |
||
{ |
{ |
||
printf("linklist 2 is smaller and not equal"); |
printf("linklist 2 is smaller and not equal"); |
||
return 0; |
return 0; |
||
} |
|||
else |
else |
||
printf("the lists are not equal"); |
printf("the lists are not equal"); |
||
Line 123: | Line 130: | ||
{ |
{ |
||
case 1: |
case 1: |
||
printf("enter the link list in which u |
printf("enter the link list in which u want to enter element\n"); |
||
printf("1.1st link lis\n2.2nd link list\n"); |
printf("1.1st link lis\n2.2nd link list\n"); |
||
scanf("%d",&i); |
scanf("%d",&i); |
||
Line 147: | Line 154: | ||
break; |
break; |
||
case 4: |
case 4: |
||
printf("enter the list no. u |
printf("enter the list no. u want to copy"); |
||
scanf("%d",&i); |
scanf("%d",&i); |
||
if(i==1) |
if(i==1) |
||
Line 172: | Line 179: | ||
</pre> |
</pre> |
||
[[Category: Hackety Lesson Plan]] |
Latest revision as of 20:05, 6 April 2008
Problem Statement
You are the coordinator of the class and you have been assigned the task of maintaining the test results of the students. You have to take marks alloted to the students as input and then compare it with the final list given to you by the teacher. Being intelligent, you try to speed up your task by making a program which can accept marks of the student and then compare it with the list given to by teacher.
You don't know the number of students in the class. Therefore you have a create a dynamic list.
HINT: Write a program to compare two linked list using recursion. And hence write a program to copy one linked list into another using recursion.
Sample Output
To be done.......
Curriculum Connections
1. Rudimentary knowledge of Data Structures 2. Creating, Traversing and Deletion of Nodes of Linked list 3. Using Recursion to solve problems
Solution
/* CODE tested on Turbo C++ 3.0 Compiler 20th October 2007 */ //Include necessary files (for defining prototypes of functions used) #include<stdio.h> #include<conio.h> #include<stdlib.h> typedef struct node // A structure representing a node of Linked List and re-naming it as node(Using typedef) { int info; struct node* ptr; } node; node *sptr1=NULL, *sptr2=NULL; // Initialize the 2 Linked List pointer to header nodes with NULL(Empty Linked List) int menu() { int ch; printf("\nEnter your choice\n"); printf("1.Add node\n2.Compare lists\n3.Traverse list\n4.Copy link list\n5.Exit\n"); scanf("%d",&ch); // Menu Driven Program return ch; } node *addnode(node *sptr) // When a new node is to be added in Linked List { node *temp; temp=(node*) malloc (sizeof(node)); temp->ptr=NULL; temp->info=NULL; printf("enter the no."); // Asking user to enter data in node scanf("%d",&temp->info); if(sptr==NULL) // Condition to check whether the node being added is 1st node sptr=temp; else { temp->ptr=sptr; sptr=temp; } return sptr; // returns the pointer to 1st node of List. } int compare(node *ptr1,node *ptr2) { if((ptr1->info==ptr2->info) && ptr1!=NULL && ptr2!=NULL) { ptr2=ptr2->ptr; ptr1=ptr1->ptr; compare(ptr1,ptr2); // Recursive function call. } if(ptr1==NULL && ptr2==NULL) { printf("the link lists are equal"); return (0); } else if(ptr1==NULL) { printf("linklist 1 is smaller and not equal"); return(0); } else if(ptr2==NULL) { printf("linklist 2 is smaller and not equal"); return 0; } else printf("the lists are not equal"); return 0; } int copy(node *ptr1,node *ptr2) { if(ptr1!=NULL && ptr2!=NULL) { ptr2->info=ptr1->info; ptr1=ptr1->ptr; ptr2=ptr2->ptr; copy(ptr1,ptr2); } return 0; } int traverse(node *sptr) { node *t; t=sptr; while(t->ptr) { printf("%d\t",t->info); t=t->ptr; } printf("%d",t->info); return 0; } int main() { int i; do { switch(menu()) { case 1: printf("enter the link list in which u want to enter element\n"); printf("1.1st link lis\n2.2nd link list\n"); scanf("%d",&i); if(i==1) sptr1=addnode(sptr1); else if(i==2) sptr2=addnode(sptr2); else printf("enter correctly"); break; case 2: compare(sptr1,sptr2); break; case 3: printf("enter the list no. to be traversed\n"); scanf("%d",&i); if(i==1) traverse(sptr1); else if(i==2) traverse(sptr2); else printf("enter correctly"); break; case 4: printf("enter the list no. u want to copy"); scanf("%d",&i); if(i==1) copy(sptr1,sptr2); else if(i==2) copy(sptr2,sptr1); else { printf("enter correct no."); break; } printf("the copied link list2 is\n"); traverse(sptr2); break; case 5: exit(0); } } while(1); getch(); return 0; }