#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
void display(struct node *head)
{
struct node *ptr = head;
do
{
printf(" %d ", ptr->data);
ptr = ptr->link;
} while (ptr != head);
}
struct node *insert_beg(struct node *head,int s){
struct node *e = malloc(sizeof(struct node));
e->data = s;
struct node *ptr;
ptr = head->link;
while(ptr->link!=head){
ptr=ptr->link;
}
ptr->link = e;
e->link = head;
head = e;
return head;
}
struct node *insert_end(struct node *head,int s){
struct node *e = malloc(sizeof(struct node));
e->data = s;
struct node *ptr;
ptr = head->link;
while(ptr->link!=head){
ptr=ptr->link;
}
ptr->link = e;
e->link = head;
return head;
}
struct node *insert_pos(struct node *head,int s,int pos){
struct node *e = malloc(sizeof(struct node));
struct node *ptr=head;
int j=0;
while(j!=pos-1){
ptr=ptr->link;
j++;
}
e->data = s;
e->link = ptr->link;
ptr->link = e;
return head;
}
int main()
{
struct node *a;
struct node *b;
struct node *c;
struct node *d;
a = malloc(sizeof(struct node));
b = malloc(sizeof(struct node));
c = malloc(sizeof(struct node));
d = malloc(sizeof(struct node));
a->data = 1;
a->link = b;
b->data = 2;
b->link = c;
c->data = 3;
c->link = d;
d->data = 4;
d->link = a;
display(a);
printf("\n");
// a = insert_beg(a,0);
// a = insert_end(a,5);
a = insert_pos(a,0,2);
display(a);
return 0;
}
Comments
Post a Comment