Untitled

 avatar
unknown
plain_text
2 months ago
3.2 kB
5
Indexable
#include<stdio.h>
#include<stdlib.h>
struct node
{
	int data;
	struct node *next;
};
struct node *head;
void createlist(int n)
{
	struct node *fnode;
	fnode = (struct node *)malloc(sizeof(struct node ));
	struct node *temp;
	head = fnode;
	temp = fnode;
	printf("Enter Data for Node 1 :- ");
	scanf("%d",&fnode->data);
	fnode->next = NULL;
	struct node *newnode;
	for(int i =2;i<=n;i++)
	{
      newnode = (struct node *)malloc(sizeof(struct node));
      printf("Enter Data for Node %d :- ",i);
      scanf("%d",&newnode->data);
      newnode->next = NULL;
      temp->next = newnode;
      temp = newnode;
	}

}
void display()
{
    struct node *temp;
    temp = head;
    while(temp!=NULL)
    {
        printf("%d\n",temp->data);
        temp = temp->next;
    }
}
void insert_At_Begining()
{
	struct node *newnode;
	newnode = (struct node *)malloc(sizeof(struct node));
	printf("Enter data of the node for the first at the begining :-");
	scanf("%d",&newnode->data);
	newnode->next = head;
	head = newnode;
}
void insert_At_end()
{
	struct node *temp = head;
	while(temp->next!=NULL)
    {
        temp= temp->next;
    }
    struct node *newnode ;
    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter Data of the node insert At the end :- ");
    scanf("%d",&newnode->data);
    newnode->next = NULL;
    temp->next = newnode;
    temp = newnode;
}
void insert_At_anyposition()
{
    int pos;
    struct node *newnode,*temp;
    newnode = (struct node *)malloc(sizeof(struct node));
    printf("Enter position for insert :- ");
    scanf("%d",&pos);
    printf("Enter Data to insert At the position :-");
    scanf("%d",&newnode->data);
    temp = head;
    for(int i = 1;i<pos-1;i++)
    {
        temp = temp->next;
    }
    newnode->next = temp->next;
    temp->next = newnode;
}

void del_first(){
    struct node *temp;
    temp=head;
    head=head->next;
    free(temp);
    printf("\nAfter deleting first node:\n");
}
 void del_end()
 {
     struct node *temp,*current,*prev;
     temp = head;
     while(temp->next!= NULL)
     {
         prev = temp;
         current = temp->next;
         temp = temp->next;
     }
     free(current);
     prev->next = NULL;
    printf("\nAfter deleting last node:\n");
 }
 void del_anyposition()
 {
       int pos;
        printf("Enter position for deletion :- ");
        scanf("%d",&pos);
        struct node *temp,*prev,*current;
        temp = head;
        for(int i =1;i<pos-1;i++)
        {
            prev = temp;
            current = temp->next;
            temp = temp->next;
        }
        prev->next = current->next;
        free(current);
        printf("\nAfter deleting at the position %d:\n",pos);
 }
int main()
{
	int num;
	printf("Enter Total Number of Nodes :- ");
	scanf("%d",&num);
	createlist(num);
	display();

	insert_At_Begining();
	display();

	insert_At_anyposition();
	display();

	insert_At_end();
	display();

	del_first();
	display();

	del_anyposition();
	display();

	del_end();
	display();

	return 0;

}
Editor is loading...
Leave a Comment