Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- struct Node
- {
- int data;
- struct Node *next;
- } *temp, *node;
- struct Head
- {
- int count;
- struct Node *first;
- } *head;
- void Head_create()
- {
- head = (struct Head*) malloc(sizeof(struct Head));
- head->count = 0;
- head->first = NULL;
- }
- void Add_value_start(int value)
- {
- node = (struct Node*) malloc(sizeof(struct Node));
- node->data = value;
- node->next = head->first;
- head->first = node;
- (head->count)++;
- }
- void Add_value_end(int value)
- {
- if(head->count != 0)
- {
- temp = head->first;
- while( temp->next != NULL)
- {
- temp = temp->next;
- }
- node = (struct Node*) malloc(sizeof(struct Node));
- node->data = value;
- node->next = NULL;
- temp->next = node;
- (head->count)++;
- }
- else
- Add_value_start(value);
- }
- void Add_value_anywhere()
- {
- int Search_values,v,flag=0;
- //struct Node *otherTemp;
- printf("\t\tEnter a value after you want to put a new Value: ");
- scanf("%d",&Search_values);
- node = (struct Node*) malloc (sizeof(struct Node));
- temp = head->first;
- while(temp != NULL)
- {
- if(temp->data == Search_values)
- {
- printf("\t\tEnter a Value: ");
- scanf("%d",&v);
- node->da = v;
- /*otherTemp = temp->next;
- temp->next = node;
- node->next = otherTemp;
- flag++;*/
- node->next=temp->next;
- temp->next=node;
- flag=1;
- (head->count)++;
- break;
- }
- temp = temp->next;
- }
- if(flag == 0)
- printf("\t\t%d is not found the list.\n",Search_values);
- //free(otherTemp);
- }
- void Delete_value_start()
- {
- if(head->count != 0)
- {
- temp = head->first;
- //head->first = head->first->next;
- head->first = temp->next;
- free(temp);
- (head->count)--;
- }
- else
- printf("\t\tLinkedlist is empty.\n");
- }
- void Delete_value_end()
- {
- if(head->count != 0)
- {
- temp = head->first;
- while(temp->next != NULL)
- {
- node = temp;
- temp = temp->next;
- }
- node->next = NULL;
- //free(node->next);
- free(temp);
- (head->count)--;
- }
- else
- printf("\t\tLinkedlist is empty.\n");
- }
- void Delete_value_anywhere()
- {
- if(head->count != 0)
- {
- int Search_values,val,flag=0;
- struct Node *otherTemp;
- printf("\t\tEnter a value after you want to delete: ");
- scanf("%d",&Search_values);
- temp = head->first;
- while(temp != NULL)
- {
- if(temp->data==Search_values&&temp->next==NULL)
- {
- printf("\t\tData is in last position.\n\t\tUse option 5 to delete this data.\n");
- flag=1;
- }
- else if(temp->data == Search_values)
- {
- otherTemp = temp->next;
- node = otherTemp->next;
- temp->next = node;
- flag=1;
- free(otherTemp);
- (head->count)--;
- break;
- }
- temp = temp->next;
- }
- if(flag == 0)
- printf("\t\t%d is not found.\n",Search_values);
- }
- else
- printf("\t\tLinkedlist is empty.\n");
- }
- void Search_values(int value)
- {
- int flag = 0, i;
- for(temp = head->first, i=1; temp != NULL; temp = temp->next,i++)
- {
- if(temp->data == value)
- {
- printf("\t\t%d Found in the list in no %d position.\n",temp->data,i);
- flag=1;
- break;
- }
- }
- if(flag == 0)
- {
- printf("\t\t%d not Found in the list!\n",value);
- }
- }
- void Show_values()
- {
- temp = head->first;
- printf("\t\tCurrent Values in the Linked List\n");
- while(temp != NULL)
- {
- printf("\t\t%d\n",temp->data);
- temp = temp->next;
- }
- }
- void Menu_function()
- {
- printf("\n\n\t\t------------------------------------------------\n");
- printf("\t\t: < Linked List > :\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t: Add data :\n");
- printf("\t\t: 1. At First 2. At Last 3. At Anywhere :\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t: Delete data :\n");
- printf("\t\t: 4. At First 5. At Last 6. At Anywhere :\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t: 7. Search a Data :\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t: 8. Show All Data :\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\t: 9. Exit program :\n");
- printf("\t\t------------------------------------------------\n");
- printf("\t\tEnter choice :");
- }
- int Take_number()
- {
- int value;
- printf("\t\tEnter a value: ");
- scanf("%d", &value);
- return value;
- }
- int main()
- {
- int choice;
- Head_create();
- for( ; ; )
- {
- Menu_function();
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- Add_value_start(Take_number());
- break;
- case 2:
- Add_value_end(Take_number());
- break;
- case 3:
- Add_value_anywhere();
- break;
- case 4:
- Delete_value_start();
- break;
- case 5:
- Delete_value_end();
- break;
- case 6:
- Delete_value_anywhere();
- break;
- case 7:
- Search_values(Take_number());
- break;
- case 8:
- Show_values();
- break;
- case 9:
- free(temp);
- free(node);
- free(head);
- return EXIT_SUCCESS;
- default:
- printf("\t\tplease only press 1 to 9\n");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement