Showing posts with label Link List. Show all posts
Showing posts with label Link List. Show all posts

Friday, February 27, 2015

Swap Kth node from beginning with Kth node from end in a Linked List

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *next;
};

void push(struct node ** head_ref, int new_data)
{
    struct node* new_node = (struct node*) malloc(sizeof(struct node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)  = new_node;
}



void printList(struct node *head)
{
    struct node *temp = head;
    while (temp != NULL)
    {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}



void Fun(struct node **Node,int x,int y){
struct node *p=*Node;
struct node *q=*Node;
while(p->next->data!=x)
{
p=p->next;
}
while(q->next->data!=y)
{
q=q->next;
}
struct node *m=p->next;
struct node *r=p->next->next;
struct node *s=q->next->next;
p->next=q->next;
p->next->next=r;
q->next=m;
q->next->next=s;
 }



int main()
{
    int i;
     struct node *p = NULL;
     for (i = 8; i >= 1; i--){
       push(&p, i);
     }
     printf("Before Swapping 3 and 6   :");
     printList(p);
     Fun(&p,3,6);
     printf("\nAfter Swapping 3 and 6  :");
    printList(p);
     getchar();
     return 0;
}

Thursday, December 18, 2014

Implement Queue Using Link List in c

#include<stdio.h>
#include<stdlib.h>
#include "QUEUE.h"  //given before
int main()
{
struct node *s1=NULL,*s2=NULL;
int choice ,item;
while(1)

{
printf("\n1.Insert\n");
printf("2. Delete\n");
printf("3. Display the element at the front\n");
printf("4. Display all element of the queue\n");
printf("5. Quit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Input the element for adding in queue:");
scanf("%d",&item);
insert(&s1,&s2,item);
break;
case 2:
printf("deleted item is %d",del(&s2));
break;
case 3:
printf("Element at the front of Queue is %d\n",peek(s2));
break;
case 4:
display(s2);
break;
case 5:
exit(1);
}
}
return 0;
}

Sunday, December 14, 2014

remove duplicate elements from sorted link list in c

/* remove duplicate elements from sorted link list*/
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct node{

int data;
struct node* next;
};
/* Function to get the middle of the linked list*/
void RemoveDuplicates_from_sortedList(struct node **head)
{
struct node *temp=*head;
struct node *del;

while(((temp)!=NULL)&&((temp)->next!=NULL))
{
if((temp->data)==(temp->next->data))
{
del=temp->next;
temp->next=del->next;
free(del);
temp=*head;
}
else
{
temp=temp->next;
}
}

}
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;
}
// A utility function to print a given linked list
void printList(struct node *ptr)
{
while (ptr != NULL)
{
printf("%d->", ptr->data);
ptr = ptr->next;
}
printf("NULL\n");
}
int main()
{
/* Start
/* Start with the empty list */
struct node* head = NULL;
int i;
push(&head,3);
push(&head,3);
push(&head,2);
push(&head,2);
push(&head,2);
push(&head,1);
push(&head, 1);
push(&head,1);
push(&head,1);
push(&head,1);
printf("Sorted List is :->");
printList(head);
RemoveDuplicates_from_sortedList(&head);
printf("\n\nAfter removing duplicate from sorted List is: ->");
printList(head);
return 0;
}