Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This program take a DLL as input which has 10 positive and 10 negative number in random order and provide 2 DLL as output. One DLL for positive integers & other for negative integers.*/
- #include <stdio.h>
- #include <stdlib.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;
- }
- //return start;
- }//end of create list
- void bifurcatelist(node * start)
- {
- node * ptr, *p_start = NULL, *n_start = NULL;
- ptr = start;
- while(ptr!=NULL)
- {
- if(ptr->info < 0)
- {
- n_start = createlist(n_start,ptr->info);
- }
- else
- p_start = createlist(p_start,ptr->info);
- ptr= ptr->next;
- }
- printf("\n\nThe positive linked list is following: \n");
- display(p_start);
- printf("\n\nThe negative linked list is following: \n");
- display(n_start);
- }//end of bifurcatelist
- 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;
- int i,data;
- printf("Please enter 10 positive & 10 negative numbers in random order: \n");
- for ( i = 1; i <= 20; i++)
- {
- printf("Enter the nuber %d: \n",i);
- scanf("%d",&data);
- start = createlist(start,data);
- }
- printf("\n\n Here is your list: \n");
- display(start);
- printf("\n\nHere is your bifurcated lists: \n");
- bifurcatelist(start);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement