Required knowledge
Pointer, Dynamic Memory allocation, Knowledge of Malloc Function in c
Knowledge of free Function in c
Define a Global Structure
struct node { int data; struct node *link; };
Define a Global 'Structure Node' Type Pointer
struct node *start=NULL;
Function of create a new node
struct node *newnode() { struct node *n; n=(struct node*)malloc(sizeof(struct node)); return n; }
Function of insert value in first
void insert_value_in_first(int a) { struct node *temp; temp=newnode(); temp->link=NULL; temp->data=a; temp->link=start; start=temp; printf("%d Value Inserted in First-Position Successfully",a); }
Function of display all value
void display_all_value() { struct node *temp; temp=start; if(temp==NULL) { printf("Link List Empty"); return; } do { printf("%d\n",temp->data); temp=temp->link; }while(temp!=NULL); }
Function of delete first value
void delete_first_value() { struct node *temp; if(start==NULL) { printf("Link List Empty"); return; } temp=start; start=start->link; printf("%d Value Deleted From First-Position Successfully",temp->data); free(temp); }
Function of delete last value
void delete_last_value() { struct node *back,*temp; if(start==NULL) { printf("The Link-List Empty"); return; } if(start->link==NULL) { printf("%d Value Deleted From Last-Position Successfully",start->data); start=NULL; } else { temp=start; back=start; do { back=temp; temp=temp->link; }while(temp->link!=NULL); back->link=NULL; printf("%d Value Deleted From Last-Position Successfully",temp->data); free(temp); } }
Function of insert value in last
void insert_value_in_last(int a) { struct node *temp,*t; temp=newnode(); temp->data=a; temp->link=NULL; if(start==NULL) { start=temp; printf("%d Value Inserted in Last-Position Successfully",a); } else { t=start; while(t->link!=NULL) { t=t->link; } t->link=temp; printf("%d Value Inserted in Last-Position Successfully",a); } }
Function of delete value from any position
void delete_value_from_any_position(int a) { struct node *temp,*back; if(start==NULL) { printf("The Link List Empty"); return; } if(start->link==NULL) { if(start->data==a) { printf("%d Value Deleted Successfully",a); free(start); start=NULL; } else { printf("Value Not Found In Link List"); return; } } else { temp=start; back=start; do { if(temp->data==a) { if(start==temp) { start=temp->link; free(temp); printf("%d Value Deleted Successfully",a); } else { back->link=temp->link; free(temp); printf("%d Value Deleted Successfully",a); } return; } back=temp; temp=temp->link; } while(temp!=NULL); printf("Value Not Found in Link List"); return; } }
Function of Reverse link list
void Reverse_link_list() { struct node *temp,*start1,*back,*t=NULL; int count=1,i,j; if(start==NULL) { printf("The Link List Empty"); return; } else if(start->link==NULL) { printf("Only One Value in Link-List"); return; } do { temp=start; back=start; do { back=temp; temp=temp->link; }while(temp->link!=t); if(temp->link==NULL) { start1=temp; } temp->link=back; t=temp; }while(back!=start); back->link=NULL; start=start1; printf("Link List Reverse Successfully"); }
COMPLETE PROGRAM OF LINK-LIST
#include < stdio.h > #include < conio.h > struct node { int data; struct node *link; }; struct node *start=NULL; struct node *newnode() { struct node *n; n=(struct node*)malloc(sizeof(struct node)); return n; } void insert_value_in_first(int a) { struct node *temp; temp=newnode(); temp->link=NULL; temp->data=a; temp->link=start; start=temp; printf("%d Value Inserted in First-Position Successfully",a); } void display_all_value() { struct node *temp; temp=start; if(temp==NULL) { printf("Link List Empty"); return; } do { printf("%d\n",temp->data); temp=temp->link; }while(temp!=NULL); } void delete_first_value() { struct node *temp; if(start==NULL) { printf("Link List Empty"); return; } temp=start; start=start->link; printf("%d Value Deleted From First-Position Successfully",temp->data); free(temp); } void delete_last_value() { struct node *back,*temp; if(start==NULL) { printf("The Link-List Empty"); return; } if(start->link==NULL) { printf("%d Value Deleted From Last-Position Successfully",start->data); start=NULL; } else { temp=start; back=start; do { back=temp; temp=temp->link; }while(temp->link!=NULL); back->link=NULL; printf("%d Value Deleted From Last-Position Successfully",temp->data); free(temp); } } void insert_value_in_last(int a) { struct node *temp,*t; temp=newnode(); temp->data=a; temp->link=NULL; if(start==NULL) { start=temp; printf("%d Value Inserted in Last-Position Successfully",a); } else { t=start; while(t->link!=NULL) { t=t->link; } t->link=temp; printf("%d Value Inserted in Last-Position Successfully",a); } } void delete_value_from_any_position(int a) { struct node *temp,*back; if(start==NULL) { printf("The Link List Empty"); return; } if(start->link==NULL) { if(start->data==a) { printf("%d Value Deleted Successfully",a); free(start); start=NULL; } else { printf("Value Not Found In Link List"); return; } } else { temp=start; back=start; do { if(temp->data==a) { if(start==temp) { start=temp->link; free(temp); printf("%d Value Deleted Successfully",a); } else { back->link=temp->link; free(temp); printf("%d Value Deleted Successfully",a); } return; } back=temp; temp=temp->link; } while(temp!=NULL); printf("Value Not Found in Link List"); return; } } void Reverse_link_list() { struct node *temp,*start1,*back,*t=NULL; int count=1,i,j; if(start==NULL) { printf("The Link List Empty"); return; } else if(start->link==NULL) { printf("Only One Value in Link-List"); return; } do { temp=start; back=start; do { back=temp; temp=temp->link; }while(temp->link!=t); if(temp->link==NULL) { start1=temp; } temp->link=back; t=temp; }while(back!=start); back->link=NULL; start=start1; printf("Link List Reverse Successfully"); } void main() { int val,i; do { system("cls"); printf("1. Insert New Value in Link-List\n"); printf("2. Display All Value of the Link-List"); printf("\n3. Delete Value from Link-List\n"); printf("4. Reverse Link-List"); printf("\n5. Exit"); printf("\nEnter your choise: "); scanf("%d",&i); switch(i) { case 1: printf("1. Insert Value in First\n"); printf("2. Insert Value in Last\n"); printf("Enter You choise: "); scanf("%d",&i); switch(i) { case 1: printf("Enter The Value: "); scanf("%d",&val); insert_value_in_first(val); break; case 2: printf("Enter The Value: "); scanf("%d",&val); insert_value_in_last(val); break; } break; case 2: display_all_value(); break; case 3: printf("1. Delete Value From First\n"); printf("2. Delete Value From Last\n"); printf("3. Delete Any Value From Any Position\n"); printf("Enter You Choise: "); scanf("%d",&i); switch(i) { case 1: delete_first_value(); break; case 2: delete_last_value(); break; case 3: printf("\nEnter the Value Delete from Link List: "); scanf("%d",&val); delete_value_from_any_position(val); break; } break; case 4: Reverse_link_list(); break; case 5: exit(0); } getche(); }while(1); }
0 Comments