#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void insert_atfirst();
void insert_atend();
void getlength();
void insert();
void delete_fromfirst();
void delete_fromend();
void delete_frompos();
void reverse();
struct node{
int data;
struct node *next;
};
struct node *head, *newnode, *temp, *prevnode, *nextnode, *currentnode;
int count;
void main(){
create();
display();
insert_atfirst();
display();
insert_atend();
display();
getlength();
insert();
display();
delete_fromfirst();
display();
delete_fromend();
display();
delete_frompos();
display();
reverse();
display();
getch();
}
void create(){
head=0;
int choice=1;
while(choice){
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter data: ");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0){
head=temp=newnode;
}
else{
temp->next=newnode;
temp=newnode;
}
printf("Do you want to continue (0,1)?");
scanf("%d",&choice);
}
}
void display(){
temp=head;
while(temp!=0){
printf("%d\t",temp->data);
temp=temp->next;
}
}
void insert_atfirst(){
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data you want to insert at first: ");
scanf("%d",&newnode->data);
newnode->next=head;
head=newnode;
}
void insert_atend(){
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data you want to insert at end: ");
scanf("%d",&newnode->data);
newnode->next=0;
temp=head;
while(temp->next!=0)
temp=temp->next;
temp->next=newnode;
}
void getlength(){
//int count=0;
count=0;
struct node *temp;
temp=head;
while(temp!=0){
count++;
temp=temp->next;
}
printf("\nLength is %d",count);
}
void insert(){
int pos,i=1;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the position where you want to insert: ");
scanf("%d",&pos);
if(pos>count)
printf("Invalid Position");
else{
temp=head;
while(i<pos){
temp=temp->next;
i++;
}
printf("Enter data: ");
scanf("%d",&newnode->data);
newnode->next=temp->next;
temp->next=newnode;
}
}
void delete_fromfirst(){
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nAfter deleting from first:\n");
if(head==0)
printf("\nLinked List is empty");
else{
temp=head;
head=head->next;
free(temp);
}
}
void delete_fromend(){
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nAfter deleting from end:\n");
if(head==0)
printf("\nLinked List is empty");
else{
temp=head;
while(temp->next!=0){
prevnode=temp;
temp=temp->next;
}
if(temp==head)
head=0;
else
prevnode->next=0;
free(temp);
}
}
void delete_frompos(){
int pos,i=1;
temp=head;
printf("\nEnter position from where you want to delete: ");
scanf("%d",&pos);
if(pos==1)
delete_fromfirst();
else if(pos>count)
printf("Invalid Position");
else{
while(i<pos-1){
temp=temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
}
void reverse(){
printf("\nReverse linked list is:\n");
prevnode=0;
currentnode=nextnode=head;
while(nextnode!=0){
nextnode=nextnode->next;
currentnode->next=prevnode;
prevnode=currentnode;
currentnode=nextnode;
}
head=prevnode;
}
No comments:
Post a Comment