Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- struct node
- {
- int data;
- struct node *next;
- };
- struct node *head=NULL,*new_node=NULL,*temp=NULL;
- int i,n;
- void storing_node();
- void print_linked_list();
- void insert_at_first_position();
- void insert_after_a_given_position();
- void insert_at_the_end_of_linked_list();
- void delate_from_linked_list();
- int main()
- {
- printf("Adab sir.\n");
- printf("I am Misbah Uddin Tareq\n");
- printf("My Id is 1912020092\n");
- printf("___________________\n\n");
- printf("First of all,you have to store node in Linked List.So,\n");
- storing_node();
- printf("\nCurrently these are the standard of your link list\n");
- print_linked_list();
- printf("\n");
- printf("\nIf you want to insert an item at first position of your Linked List,then: ");
- insert_at_first_position();
- printf("\nCurrently these are the standard of your link list\n");
- print_linked_list();
- printf("\n");
- printf("\nIf you want to insert an item at any position of your Linked List,then: ");
- insert_after_a_given_position();
- printf("\n");
- printf("\nIf you want to insert an item at the end of your Linked List,then: ");
- insert_at_the_end_of_linked_list();
- printf("\nCurrently these are the standard of your link list\n");
- print_linked_list();
- printf("\n");
- printf("\nIf you want to delate an item from your Linked List,then: ");
- delate_from_linked_list();
- printf("\n\nThank you sir.\n");
- return 0;
- }
- void storing_node()
- {
- printf("Please,enter the number of elemnet you want to store: ");
- scanf("%d",&n);
- printf("Now,enter all of your %d element respectively:\n",n);
- for(i=1; i<=n; i++)
- {
- new_node = (struct node *) malloc(sizeof(struct node));
- if(new_node == NULL)
- {
- printf("ERROR!");
- break;
- }
- scanf("%d",&new_node->data);
- new_node->next=NULL;
- if(head == NULL)
- head=temp=new_node;
- else
- {
- temp->next=new_node;
- temp=new_node;
- }
- }
- }
- void print_linked_list()
- {
- temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp=temp->next;
- }
- }
- void insert_at_first_position()
- {
- printf("\nPlease,enter the number you want to insert at first position: ");
- new_node = (struct node*) malloc(sizeof(struct node));
- scanf("%d",&new_node->data);
- new_node->next=head;
- head=new_node;
- }
- void insert_after_a_given_position()
- {
- bool p = false;
- int item;
- printf("\nPlease,enter the element after which you want to insert: ");
- scanf("%d",&item);
- if(head==NULL)
- printf("Sorry,item is not found !!");
- else
- {
- temp = head;
- while(temp!=NULL)
- {
- if(item == temp->data)
- {
- new_node = (struct node*) malloc(sizeof(struct node));
- printf("Please,enter the item you want to insert: ");
- scanf("%d",&new_node->data);
- new_node->next = temp->next;
- temp->next = new_node;
- p=true;
- }
- temp=temp->next;
- }
- if(!p)
- printf("Sorry,item is not found !!");
- else
- {
- printf("\nCurrently these are the standard of your link list\n");
- print_linked_list();
- }
- }
- }
- void insert_at_the_end_of_linked_list()
- {
- temp = head;
- while(temp->next!=NULL)
- temp=temp->next;
- printf("\nPlease,enter the number you want to insert at the end: ");
- new_node = (struct node*) malloc(sizeof(struct node));
- scanf("%d",&new_node->data);
- temp->next = new_node;
- new_node->next = NULL;
- }
- void delate_from_linked_list()
- {
- int item;
- bool p=false;
- struct node *x;
- printf("\nPlease,enter the element you want to delete: ");
- scanf("%d",&item);
- if(head == NULL)
- printf("Sorry,deletion is not Possible !!");
- else if(item == head->data)
- {
- head = head->next;
- p=true;
- }
- else
- {
- temp=head;
- while(temp!=NULL)
- {
- if(item == temp->data)
- {
- p=true;
- break;
- }
- x = temp;
- temp = temp->next;
- }
- if(!p)
- printf("Sorry,item is not found !!");
- else
- {
- x->next = temp->next;
- free(temp);
- }
- }
- if(p)
- {
- printf("\nCurrently these are the standard of your link list\n");
- print_linked_list();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement