#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()
{
push(&start,10);
push(&start,20);
push(&start,30);
push(&start,40);
push(&start,50);
display(start);
insertAfterNode(start,150,50);
insertAfterNode(start,140,40);
insertAfterNode(start,130,30);
insertAfterNode(start,120,20);
insertAfterNode(start,110,10);
display(start);
return 0;
}
-------------------------------------------
/*
50 40 30 20 10
50 150 40 140 30 130 20 120 10 110
*/
//2.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()
{
push(&start,10);
push(&start,20);
push(&start,30);
push(&start,40);
push(&start,50);
display(start);
insertAfterNode(start,150,50);
insertAfterNode(start,140,40);
insertAfterNode(start,130,30);
insertAfterNode(start,120,20);
insertAfterNode(start,110,10);
display(start);
return 0;
}
/*
50 40 30 20 10
50 150 40 140 30 130 20 120 10 110
*/
--------------------------------
//3. Inserting before 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;
struct node* push(struct node** ref,int d)
{
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
newnode->next=(*ref);
(*ref)=newnode;
return (*ref);
}
void display(struct node *start)
{
struct node *ptr=start;
while(ptr)
{
printf("%d\t",ptr->data);
ptr=ptr->next;
}
printf("\n");
}
struct node* insertBeforeNode(struct node *start,int d, int Beforenode)
{
int i;
struct node *newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=d;
struct node* current=start;
if(Beforenode==start->data)
{
printf("\nbefore1\n");
newnode->next=start;
start=newnode;
printf("\nbefore2\n");
return start;
printf("\nbefore3\n");
}
current=start;
while(current->next!=NULL)
{
if(current->next->data==Beforenode)
{
newnode->next=current->next;
current->next=newnode;
return start;//must else no display
}
current=current->next;
}
}
int main()
{
push(&start,10);
push(&start,20);
push(&start,30);
push(&start,40);
push(&start,50);
display(start);
insertBeforeNode(start,130,30);
insertBeforeNode(start,120,20);
insertBeforeNode(start,110,10);
insertBeforeNode(start,150,50);
display(start);
return 0;
}
/*
50 40 30 20 10
50 40 130 30 120 20 110 10
*/
No comments:
Post a Comment