Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*1. Create a DLL(DOUBLE LINKED LIST)
- 2. Enter 5 positive number in ascending order
- 3. Create a function which takes this DLL as argument and create another DLL which is reverse of the previous DLL.
- 4. Print the reverse DLL in main to show that function is working properly
- 5. Concatenate both link list in order where the 1st comes after 2nd.*/
- #include<stdio.h>
- typedef struct node{ // doubly linked node
- int info;
- struct Node* next; // points to next node
- struct Node* prev; // points to previous node
- }node;
- node * addtoempty(node * start, int n)
- {
- node * tmp;
- tmp = (node *)malloc(sizeof(node));
- tmp->info = n;
- tmp->prev = NULL;
- tmp->next = NULL;
- start = tmp;
- return start;
- }//end of addtoempty
- node * addtoend(node * start, int data)
- {
- node * tmp, *ptr;
- tmp = (node *)malloc(sizeof(node));
- tmp->info = data;
- ptr=start; //now we have to traverse to find the end location
- while(ptr->next != NULL)
- ptr = ptr->next;
- ptr->next=tmp;
- tmp->prev=ptr;
- tmp->next=NULL;
- return start;
- }//end of addtoend
- node * createlist(node * start, int data)
- {
- if (start == NULL)
- {
- start = addtoempty(start,data);
- return start;
- }
- else
- {
- start = addtoend(start,data);
- return start;
- }
- }//end of create_list
- node * rev_dll(node* start)
- {
- int i;
- node * p1,*p2=NULL;
- p1 = start;
- while(p1->next!=NULL)
- p1 = p1->next;
- //p2=p1;
- for(i=1;i<=5;i++)
- {
- p2 = createlist(p2,p1->info);
- p1 = p1->prev;
- }
- return p2;
- }//end of rev_dll
- node * concatenate_list(node *rev_start,node *start)
- { node * start1;
- if(rev_start == NULL)
- {
- rev_start = start;
- return rev_start;
- }
- if(start == NULL)
- return rev_start;
- start1 = rev_start;
- while(start1->next!= NULL)
- start1 = start1->next;
- start1->next = start;
- return rev_start;
- }//end of concatenate_list
- void display(node * start)
- {
- node * ptr;
- ptr = start;
- while(ptr!=NULL)
- {
- printf("%3d ",ptr->info);
- ptr = ptr->next;
- }
- }//end of display
- int main()
- { node * start=NULL,*rev_start,*con_start;
- int i,data;
- printf("Please enter 5 positive number in ascending order: \n");
- for ( i = 1; i <= 5; i++)
- {
- printf("Enter the nuber %d: \n",i);
- scanf("%d",&data);
- start = createlist(start,data);
- }
- printf("\nHere is your list: \n");
- display(start);
- printf("\n Here is the reversed list: \n");
- rev_start = rev_dll(start);
- display(rev_start);
- printf("\nHere is the merged list (first list after second): \n");
- con_start = concatenate_list(rev_start,start);
- display(con_start);
- return 0;
- }
Advertisement
Comments
-
- This all code have written on mobile using 'c coding' app.
Add Comment
Please, Sign In to add comment
Advertisement