top of page



#include <stdio.h>

#include <stdlib.h>


struct node{

int data;

struct node *next;

};


struct node *head,*newnode,*nextnode;

int choice,i=1,pos;


void main() {

printf("\n----Insertion At Beginning----");

struct node *temp;

do

{

newnode = (struct node *)malloc(sizeof(struct node));

printf("\nEnter data : ");

scanf("%d",&newnode->data);

newnode->next=head;

head=newnode;

printf("\nEnter your choice : (0/1) ");

scanf("%d",&choice);

}while(choice);

temp=head;

printf("\n");

while(temp!=0)

{

printf("--%d",temp->data);

temp=temp->next;

}

printf("\n\n----Deletion From Any Position----");

temp=head;

printf("\nEnter position to delete : ");

scanf("%d",&pos);

while(i<pos-1)

{

temp=temp->next;

i++;

}

if(temp==head)

{

head=0;

free(temp);

}

else

{

nextnode=temp->next;

temp->next=nextnode->next;

free(nextnode);

}

temp=head;

printf("\n");

while(temp!=0)

{

printf("--%d",temp->data);

temp=temp->next;

}

}




#include <stdio.h>

#include <stdlib.h>


struct node{

int data;

struct node *next;

};


struct node *head,*newnode,*prevnode;

int choice;


void main() {

printf("\n----Insertion At Beginning----");

struct node *temp;

do

{

newnode = (struct node *)malloc(sizeof(struct node));

printf("\nEnter data : ");

scanf("%d", &newnode->data);

newnode->next=head;

head=newnode;

printf("\nEnter your choice : (0/1) ");

scanf("%d", &choice);

}while(choice);

temp=head;

printf("\n");

while(temp!=0)

{

printf("--%d", temp->data);

temp=temp->next;

}

printf("\n\n----Deletion From End----");

temp=head;

while(temp->next!=0)

{

prevnode=temp;

temp=temp->next;

}

if(temp==head)

{

head=0;

}

else

{

prevnode->next=0;

}

free(temp);

temp=head;

printf("\n");

while(temp!=0)

{

printf("--%d", temp->data);

temp=temp->next;

}

}

2 views0 comments




#include <stdio.h>

#include <stdlib.h>


struct node{

int data;

struct node *next;

};


struct node *head,*newnode;

int choice;


void main() {

printf("\n----Insertion At Beginning----");

struct node *temp;

do

{

newnode = (struct node *)malloc(sizeof(struct node));

printf("\nEnter data : ");

scanf("%d",&newnode->data);

newnode->next=head;

head=newnode;

printf("\nEnter your choice : (0/1) ");

scanf("%d",&choice);

}while(choice);

temp=head;

printf("\n");

while(temp!=0)

{

printf("--%d",temp->data);

temp=temp->next;

}

printf("\n\n----Deletion From Beginning----");

temp=head;

printf("\n\n---%d is successfully deleted---\n",head->data);

head=head->next;

free(temp);

temp=head;

printf("\n");

while(temp!=0)

{

printf("--%d",temp->data);

temp=temp->next;

}

}

bottom of page