GeeksforGeeks » Linked List specific questions

Given a Singly Linked List, swap elements in pair

(16 posts)

Tags:

  1. dd
    guest
    Posted 2 years ago #

    Given a singly linked list, swap every two elements (e.g. a->b->c->d->e->f->null should become b->a->d->c->f->e->null)

  2. Shekhu
    guest
    Posted 2 years ago #

    #include<stdio.h>
    #include<stdlib.h>
    
    struct node
    {
      int data;
      struct node *next;
    };
    
    /* Function to alternatively swap the elements */
    void rearrange(struct node *start)
    {
      struct node *p, * q;
      int temp;
      if ((!start) || !start->next)
          return;
      p = start;
      q = start->next;
      while(q)
      {
         temp = p->data;
         p->data = q->data;
         q->data = temp;
         p = q->next;
         q = p?p->next:0;
      }
    }
    
    /* TESTING rearrange() .............. */
    /* Function to push a node */
    void push(struct node** head_ref, int new_data)
    {
      /* allocate node */
      struct node* new_node =
                (struct node*) malloc(sizeof(struct node));
    
      /* put in the data  */
      new_node->data  = new_data;
    
      /* link the old list off the new node */
      new_node->next = (*head_ref);    
    
      /* move the head to point to the new node */
      (*head_ref)    = new_node;
    }
    
    /* Function to print nodes in a given linked list */
    void printList(struct node *node)
    {
      while(node != NULL)
      {
        printf(" %d ", node->data);
        node = node->next;
      }
    }
    
    /* Druver program to test above function */
    int main()
    {
      struct node *start = NULL;   
    
      /* The constructed linked list is:
       6->5->4->3->2->1 */
      push(&start, 1);
      push(&start, 2);
      push(&start, 3);
      push(&start, 4);
      push(&start, 5);
      push(&start, 6);
    
      printf("\n Original Linked list");
      printList(start);
    
      rearrange(start);
    
      printf("\n Linked list after alternative swaps ");
      printList(start);
    
      getchar();
    }
  3. aron
    guest
    Posted 1 year ago #

    u hav used three pointers,but dis can be done wid one pointer aloone.

  4. Rag
    guest
    Posted 1 year ago #

    tempnode=currentnode;
    currentnoce=currentnode->next;
    while(currentnode->next!=null)
    {
         tempnode->next=currentnode->next;
         currentnode->next=tempnode;
        currentnode=tempnode->next->next;
        tempnode=tempnode->next;
    
    }
  5. abrham
    guest
    Posted 10 months ago #

    Nice. Above only pointers are manipulated. no data is swapped.

  6. VRB
    guest
    Posted 10 months ago #

    struct node
    {
    int data;
    struct node* next;
    };

    node* first = NULL;

    struct node* makenode(int a)
    {
    node* tmp = new node();
    tmp->data = a;
    tmp->next = NULL;
    return tmp;
    }

    void display()
    {
    if(first == NULL)
    return;

    node* tmp = first;
    do
    {
    cout << tmp->data << endl;
    tmp = tmp->next;
    }
    while(tmp != NULL);
    }

    void swap()
    {
    int count = 0;
    node* tmp = first;

    if(tmp == NULL)
    return;
    do
    {
    count++;
    if(count % 2 == 1)
    {
    if(tmp->next == NULL)
    continue;
    int _tmp = tmp->data;
    tmp->data = tmp->next->data;
    tmp->next->data = _tmp;
    }
    tmp = tmp->next;
    }
    while(tmp != NULL);
    }

    int main()
    {
    first = makenode(1);
    first->next = makenode(2);
    first->next->next = makenode(3);
    first->next->next->next = makenode(4);
    first->next->next->next->next = makenode(5);
    first->next->next->next->next->next = makenode(6);

    cout << "Before swapping " << endl;
    display();
    swap();
    cout << "After swapping " << endl;
    display();
    getch();
    }

  7. alluri
    guest
    Posted 9 months ago #

    void swap(struct node* head){
    	    if(head == NULL ){
    			  return ;
    			}
    		struct node* p = head;
    		struct node* q = head->link;
    		int temp;
    		temp = p->data;
    		p->data = q->data;
    		q->data = temp;
    		swap(q->link);
    
    	}
  8. alluri
    guest
    Posted 9 months ago #

    void swap(struct node* head){
    if(head == NULL ){
    return ;
    }
    struct node* p = head;
    struct node* q = head->link;
    int temp;
    temp = p->data;
    p->data = q->data;
    q->data = temp;
    swap(q->link);

    } [center]

  9. Agniswar
    Member
    Posted 9 months ago #

    void swap_pair(node *h)
    {
        node *temp,*ptr1,*ptr2;
        temp=null; ptr1=h;ptr2=ptr1->next;
        while(ptr2)
       {
         ptr1->next=ptr2->next;
         ptr2->next=ptr1;
         if(temp==null)
           temp=ptr1;
        else
          temp->next=ptr2;
       ptr1=ptr1->next;
       ptr2=ptr1->next;
    }
    }

    Plz revert back if i am wrong..

  10. venakt
    guest
    Posted 8 months ago #

    No error ckecking

  11. swati baliyan
    guest
    Posted 7 months ago #

    //swap every two elements in an linked list
    /* a->b->c->d->e->f->null
    b->a->d->c->f->e->null */
    #include<stdio.h>
    #include<conio.h>
    #include<malloc.h>
    struct node
    {
    int data;
    struct node *next;
    };
    struct node *start=NULL;
    void create(int);
    void swap(int);
    void main()
    {
    int n;
    struct node *temp;
    printf("enter number of nodes");
    scanf("%d",&n);
    create(n);
    swap(n);
    printf("\nafter swapping\n");
    if(start!=NULL)
    {
    temp=start;
    while(temp!=NULL)
    {
    printf("%d ",temp->data);
    temp=temp->next;
    }
    }
    }
    void create(int n)
    {
    int i=0;
    for(i=0;i<n;i++)
    {
    struct node *newnode,*temp;
    newnode=(struct node *)malloc(sizeof(struct node));
    printf("\nenter number\n");
    scanf("%d",&newnode->data);
    newnode->next=NULL;
    if(start==NULL)
    start=newnode;
    else
    {
    temp=start;
    while(temp->next!=NULL)
    {
    temp=temp->next;
    }
    temp->next=newnode;
    }
    }
    }
    void swap(int n)
    {
    int i,c;
    struct node *temp,*curr,*mid;
    curr=start;
    for(i=1;i<=n;i++)
    {
    if(i%2==1 && curr->next!=NULL)
    {
    temp=curr;
    mid=temp->next;
    c=temp->data;
    temp->data=mid->data;
    mid->data=c;
    curr=mid->next;
    }
    }
    }

  12. devan
    Member
    Posted 6 months ago #

    just generalizing this to n. (every n element set will be reversed.) ie, 1->2->3->4->5->6, n=3 will be 3->2->1->6->5->4.

    node * sortinpartsofn(node *head,int n)
    {
    int k=n;
    if(head==NULL)return NULL;
    node *p,*q=NULL,*temp=head;
    while(k--)
    {
    p=q;
    q=temp;
    temp=temp->next;
    q->next=p;
    }
    head->next=sortinpartsofn(temp,n);
    return q;
    }

    NB: This works only if the number of nodes is a multiple of n.

  13. devan
    Member
    Posted 6 months ago #

    The name of the function above is a bit misleading. Don't bother. It does not sort.

  14. reehdus
    Member
    Posted 6 months ago #

    
    #include<stdio.h>
    #include<stdlib.h>
    
    struct LinkedListNode
    {
        int data;
        struct LinkedListNode *next;
    };
    
    struct LinkedListNode* createNode(int data)
    {
        struct LinkedListNode *newNode = (struct LinkedListNode *)malloc(sizeof(struct LinkedListNode));
        newNode->data = data;
        newNode->next = NULL;
        return newNode;
    }
    void printLinkedList(struct LinkedListNode *head)
    {
        while(head)
        {
            printf("%d ",head->data);
            head = head->next;
        }
        printf("\n");
    }
    void reverseNodeInPairs(struct LinkedListNode *head)
    {
        struct LinkedListNode *current = head;
        int temp;
        while(current && current->next!=NULL)
        {
            temp = current->data;
            current->data = current->next->data;
            current->next->data = temp;
            current = current->next->next;
        }
    
    }
    int main()
    {
        struct LinkedListNode *head = createNode(1);
        head->next = createNode(2);
        head->next->next = createNode(3);
        head->next->next->next = createNode(4);
        head->next->next->next->next = createNode(5);
        head->next->next->next->next->next = createNode(6);
        head->next->next->next->next->next->next = createNode(7);
        printLinkedList(head);
        reverseNodeInPairs(head);
        printLinkedList(head);
        getchar();
        return 1;
    }
    
  15. reehdus
    Member
    Posted 6 months ago #

    Another solution for the problem is by interchanging the links of nodes

  16. reehdus
    Member
    Posted 6 months ago #

    
    #include<stdio.h>
    #include<stdlib.h>
    
    struct LinkedListNode
    {
        int data;
        struct LinkedListNode *next;
    };
    
    struct LinkedListNode* createNode(int data)
    {
        struct LinkedListNode *newNode = (struct LinkedListNode *)malloc(sizeof(struct LinkedListNode));
        newNode->data = data;
        newNode->next = NULL;
        return newNode;
    }
    void printLinkedList(struct LinkedListNode *head)
    {
        while(head)
        {
            printf("%d ",head->data);
            head = head->next;
        }
        printf("\n");
    }
    void reverseNodeInPairs(struct LinkedListNode **head)
    {
        struct LinkedListNode *current = *head;
        struct LinkedListNode *previous = NULL;
        struct LinkedListNode *temp;
        int count=0;
        while(current && current->next!=NULL)
        {
            temp = current->next;
            if(current==*head)
            {
                *head=temp;
                printf("head : %d\n" ,*head);
                printf("current : %d\n" ,current);
            }
            printf("temp: %d \n",temp->data);
            current->next = temp->next;
            printf("current->next: %d \n",current->next->data);
            temp->next = current;
            printf("temp ->next: %d \n",current->next->data);
            if(previous!=NULL)
               previous->next = temp;
            previous = current;
            current = current->next;
            printf("current: %d \n",current->data);
    
        }
    }
    int main()
    {
        struct LinkedListNode *head = createNode(1);
        head->next = createNode(2);
        head->next->next = createNode(3);
        head->next->next->next = createNode(4);
        head->next->next->next->next = createNode(5);
        head->next->next->next->next->next = createNode(6);
        head->next->next->next->next->next->next = createNode(7);
        printLinkedList(head);
        reverseNodeInPairs(&head);
        printLinkedList(head);
        getchar();
        return 0;
    }
    

Reply

You must log in to post.

RSS feed for this topic