Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct listNode
- {
- int data;
- struct listNode *next;
- };
- int main()
- {
- int i; int size = 10;
- struct listNode *temp,*list1;
- list1 = malloc(sizeof(*list1));
- temp = malloc(sizeof(*temp));
- temp = list1;
- srand(time(NULL));
- for(i = 1;i<=size;i++) /* Generate list with 10 elements */
- {
- if (i==size){
- temp->data = rand() % 100;
- temp->next = NULL;
- }
- else{
- temp->data = rand() % 100;
- temp->next = malloc(sizeof(*temp));
- temp = temp->next;
- }
- }
- temp = list1;
- i = 1;
- printf("List 1:\n\n");
- while(temp!=NULL) /* Print the 10 elements of the list */
- {
- printf("Element %d of list: %d\n",i,temp->data);
- i++;
- temp = temp->next;
- }
- int max; /* Finding the max in the list */
- max = list1->data;
- temp = list1->next;
- for(i = 2;i <= size;i++)
- {
- if(temp->data>max){
- max = temp->data;
- }
- temp = temp->next;
- }
- printf("\n");
- printf("The max value of the list is %d\n",max);
- printf("\n");
- struct listNode *list2;
- list2 = malloc(sizeof(*list2));
- temp = list2;
- int size2 = 5;
- for(i = 1;i<=size2;i++) /* Generate second list with 5 elements */
- {
- if (i==size2){
- temp->data = rand() % 100;
- temp->next = NULL;
- }
- else{
- temp->data = rand() % 100;
- temp->next = malloc(sizeof(*temp));
- temp = temp->next;
- }
- }
- struct listNode *list3;
- list3 = list1;
- temp = list3;
- while(temp->next!=NULL) /* Merge list1 and list2 into list3*/
- {
- temp=temp->next;
- }
- temp->next = list2;
- temp = list3;
- i = 1;
- printf("List 3:\n\n");
- while(temp!=NULL) /* Print the combined lists */
- {
- printf("Element %d of list: %d\n",i,temp->data);
- i++;
- temp = temp->next;
- }
- printf("\n");
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement