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

Linked list opeartions at middle

//nexted list to insert middle
#include <iostream>
using namespace std;
#include<stdio.h>
struct node
{
int data;
node* next;
};
struct node* first=NULL;
struct node* prev=NULL;
struct node* ptr=NULL;
struct node* last=NULL;
void insert_middle();
void display();
void deleteKey();
int main()
{
display();
deleteKey();
display();
insert_middle();
display();
insert_middle();
display();
insert_middle();
display();
//insert_middle();
//display();
deleteKey();
display();
deleteKey();
display();
deleteKey();
display();
deleteKey();
display();
return 0;
}
void insert_middle()
{
cout<<"insert_middle"<<endl;
node *newnode = new node;
cout<<"Enter the data to insert:"<<endl;
cin>>newnode->data;
newnode->next=NULL;
int pos;
cout<<"Enter the position to enter:"<<endl;
cin>>pos;
int length=0;
node* ptr=first;
while(ptr)
{
ptr=ptr->next;
length++;
}
//cout<<"length="<<length<<endl;
if(pos==1)//111111111
{
    if(first==NULL)
    {
    first=last=newnode;
    first->next=NULL;
    last->next=NULL;
    }
    else
    {
    newnode->next=first;
    first=newnode;
    }
}
else if(pos >1 && pos <=length)////////////2-length
{
    ptr=first;
    for(int i =1; i<pos;i++)
    {
    prev=ptr;
    ptr=ptr->next;
    }
    prev->next=newnode;
    newnode->next=ptr;
}
else//out side of length
{
cout<<"Out of range....Inserting at last"<<endl;
    if(first==NULL)
    {
    first=last=newnode;
    first->next=NULL;
    last->next=NULL;
     }
     else
     {
     ptr=first;
      while(ptr->next!=NULL)//ptr-next is must as segflt
      {
      ptr=ptr->next;
      }
       ptr->next=newnode;
       newnode->next=NULL;
     }
 }
}
void display()
{
cout<<"display"<<endl;
node* current=first;
while(current!=NULL)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
}
void deleteKey()
{
cout<<"delteKey"<<endl;
int key;
    if(first == NULL)
    {
        printf("\nEmpty Linked List. Deletion not possible.\n");
    }
    else
    {
        printf("\nEnter the data of the node to be deleted: ");
        scanf("%d", &key);
        ptr = first;
        while((ptr->next != NULL) && (ptr->data != key))
        {
            cout<<"while"<<endl;
            prev = ptr;
            ptr = ptr->next;
        }
        if(ptr->data == key)
        {
            if(ptr==first)
            {
            cout<<"if"<<endl;
            node * temp=first;
            first=first->next;
            delete temp;   
            }
            else
            {
            cout<<"else"<<endl;
            prev->next = ptr->next;
            delete ptr;   
            }
            printf("\nNode with data %d deleted.\n", key);
        }
        else
        {
            printf("\nValue %d not found. Deletion not possible.\n", key);
        }       
    }
}
/*output
display
delteKey
Empty Linked List. Deletion not possible.
display
insert_middle
Enter the data to insert:
11
Enter the position to enter:
1
display
11
insert_middle
Enter the data to insert:
22
Enter the position to enter:
1
display
22 11
insert_middle
Enter the data to insert:
33
Enter the position to enter:
2
display
22 33 11
delteKey
Enter the data of the node to be deleted: 11
while
while
else
Node with data 11 deleted.
display
22 33
delteKey
Enter the data of the node to be deleted: 22
if
Node with data 22 deleted.
display
33
delteKey
Enter the data of the node to be deleted: 22
Value 22 not found. Deletion not possible.
display
33
delteKey
Enter the data of the node to be deleted: 33
if
Node with data 33 deleted.
display
*/

Find and remove loop in linked list code in cpp linux

//Remove Loop in a linked list1.
//Find the loop and pass the loop node to removeLoop function.
//assign pointer ptr1=start.
//start infinite loop.
//assign pointer ptr2=loopnode.
//in loop traverse till ptr2->next!=loopnode && ptr2->next!=ptr1
//loop is found when ptr2->next=ptr1 ; break the loop
//else increment ptr1//most important
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
//Inserting at start
void insert(struct node** ref,int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*ref);
*ref=newnode;//mind it
}
//displaying
void display(struct node *ref)
{
struct node *ptr=ref;
while(ptr!=NULL)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
//Detecting Loop
void loopDetect()
{
struct node* slow=start;
struct node* fast=start;
while(start->next !=NULL)
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
printf("LoopDetected\n");
removeLoop(slow);
break;//else infinite time LoopDetected gets printed
}
}
}
void removeLoop(struct node *loopnode)
{
printf("\nRemoveLoop Called.\n");
struct node *ptr1;
struct node *ptr2;
ptr1=start;
while(1)
{
ptr2=loopnode;
while(ptr2->next != loopnode && ptr2->next!=ptr1)
{
ptr2=ptr2->next;
}
if(ptr2->next==ptr1)//mind the next
break;
else
ptr1=ptr1->next;
}
ptr2->next=NULL;
}
int main()
{
insert(&start,10);
insert(&start,20);
insert(&start,30);
insert(&start,40);
insert(&start,50);
display(start);
//create a loop
start->next->next->next->next=start->next;
//display(start);//infinite loop means loop created.
loopDetect();
display(start);//No infinite loop means No loop now.

return 0;
}
/*
50 40 30 20 10
LoopDetected
50 40 30 20
*/
--------------------------
//Remove Loop in a linked list1.

C Linux code to insert in middle of Linked list

//Inserting After given node of Single Linked List
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
        int data;
        struct node* next;
};
struct node *start=NULL;
void push(struct node** ref,int d)
{
        struct node *newnode=(struct node *)malloc(sizeof(struct node));
        newnode->data=d;
        newnode->next=(*ref);
        (*ref)=newnode;
}
void display(struct node *start)
{
        struct node *ptr=start;
        while(ptr)
        {
                printf("%d\t",ptr->data);
                ptr=ptr->next;
        }
        printf("\n");
}
struct node* insertAfterNode(struct node *start,int d, int afternode)
{
int i;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
struct node* current=start;
while(current!=NULL)
{
if(current->data==afternode)
{
newnode->next=current->next;
current->next=newnode;
//return start;
}
current=current->next;
}
}
int main()

How to merge two sorted arrays in C Language

/*Merging two sorted arrays:
input: two sorted arrays a and b and 1 output array c
1.compare a and b, if a[i]< c[k] put that in c[k] else put b[j]in c[k]
2.when any of array is empty put the remaining elements of other array's
elements in C as it is.*/
#include<stdio.h>
#include<stdlib.h>
void mergeSorted(int a[], int b[], int c[], int asize, int bsize, int
csize);

int main()
{
int i;
int a[]={5,10,15,20,25};
int b[]={2,4,6,8,10};
int c[10];
mergeSorted(a,b,c,5,5,10);
printf("Merged Array is:\n");
for(i=0;i<10;i++)
{
printf("%d ",c[i]);/* 2 4 5 6 8 10 10 15 20 25 */
}

printf("\n\n");
return 0;
}

void mergeSorted(int *a, int *b, int *c, int asize, int bsize, int csize)
{
printf("\nin mergeSorted:\n");
int ai=0,bi=0,ci=0,i;
//csize=asize+bsize;
/*compare a[ai]<b[bi] till any of the array ends.*/
while((ai<asize) && (bi<bsize))
{
if(a[ai]<=b[bi])
{
c[ci]=a[ai];
ci++;
ai++;
}
else
{
c[ci]=b[bi];
ci++;
bi++;
}
}
if(ai>=asize)
for(i=ci;i<csize;i++,bi++)
c[i]=b[bi];

else if(bi>=bsize)
for(i=ci;i<csize;i++,ai++)
c[i]=a[ai];


}

How to detect/find Loop in a Linked List in C Code

/*C Linux code for detecting Loop/cycle in as a linked list.
Take two pointers advance one by one and the other by 2 nodes.
If these pointers meet then loop is present else not.*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void insert(struct node **,int d);
void display(struct node *);
int findLoop(struct node *);

int main()
{
int result;
insert(&head,10);
insert(&head,20);
insert(&head,30);
insert(&head,40);
insert(&head,50);
insert(&head,60);
display(head);
printf("\nCreate some loop.\n");
head->next->next->next->next->next=head;
result=findLoop(head);
if(result==1)
{

printf("\nYes,Loop Present in Linked List.\n ");
}
else
{

printf("\nNo,Loop Present in Linked List.\n ");
}
printf("\n");
return 0;
}
void insert(struct node **head,int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*head);
(*head)=newnode;
}
void display(struct node *head)
{
while(head)
{
printf("%d ",head->data);
head=head->next;
}
}
int findLoop(struct node *head)
{
struct node *slow=head;
struct node *fast=head;
while((fast->next!=NULL) && (fast->next->next!=NULL))
{
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
{
//printf("\nYes,Loop Present in Linked List.\n ");
return 1;
}
}
return 0;
}
/*
When run with loop outpt is:

50 40 30 20 10
Create some loop.
Yes,Loop Present in Linked List.

When run commenting loop outpt is:
50 40 30 20 10
No,Loop Present in Linked List.
*/

How to find middle node of a Linked List in C Linux Code

/*C Linux code for finding the middle of a linked list.
Method-1- count the node and the count/2 th element is the middle
element of the Linked List.
Methode 2:- take two pointers advance one by one and the other by 2.
When fast pointer reaches end of the loop the slow reaches to middle
node of the LinkedList.*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void insert(struct node **,int d);
void display(struct node *);
void findMiddle(struct node *);

int main()
{
insert(&head,10);
insert(&head,20);
insert(&head,30);
insert(&head,40);
insert(&head,50);
display(head);
findMiddle(head);

printf("\n");
return 0;
}
void insert(struct node **head,int d)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*head);
(*head)=newnode;
}
void display(struct node *head)
{
while(head)
{
printf("%d ",head->data);
head=head->next;
}
}
void findMiddle(struct node *head)
{
struct node *slow=head;
struct node *fast=head;
// if(head)
// {
while((fast->next!=NULL) && (fast->next->next!=NULL))
{
slow=slow->next;
fast=fast->next->next;
}
// }
printf("\nThe middle node is:%d ",slow->data);
}
/*

60 50 40 30 20 10
The middle node is:40

*/

Singly linked list all operations by passing head

/*Code for:
  linked list through head
  1.insert at beginning
  2.inser after given position
  3.inser at end/append
  4.display
  5.reverse
  6.delete from beginning
  7.delete the given node value
  8.countnumber of nodes
 */
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
struct node *head;//*head is start

void insertBegin(struct node **head, int d);
void insertPosition(struct node **head, int position, int data);
void insertEnd(struct node **head, int d);
void display(struct node *head);
void count(struct node *head);
void reverse(struct node **head);
void deleteBeginning(struct node **head);
void deleteKey(struct node **head);

Linked list all operations without head pass in c

/*   code for single linked list operations:
   1.insert at Begin
   2.insert at Last//append
   3.Insert at Given Position
   4.Display
   5.reverse
   6.delete at beginning
   7.delete given node
 */
#include<stdio.h>
#include<stdlib.h>

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

void insertAtBegin(int d);
void insertAtLast(int d);
void insertAtPosition(int d,int p);
void display();
void reverse();
void deleteAtBegin();
void deleteKey();
void count();