Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node
- {
- int x;
- struct Node *ptr;
- }node;
- node *head = NULL;
- node *temp = NULL;
- int main()
- {
- int i, n, c, t, j, s, m=0, k, l, h;
- printf("Input The Number of Nodes:");
- scanf("%d", &n);
- for(i=1; i<=n; i++)
- {
- node q = (node)malloc(sizeof (node));
- printf("Input Data for %d: ", i);
- scanf("%d", &q -> x);
- q -> ptr = NULL;
- if(head == NULL)
- {
- head = q;
- }
- else
- {
- temp = head;
- while(temp -> ptr != NULL)
- {
- temp = temp -> ptr;
- }
- temp -> ptr = q;
- }
- }
- temp = head;
- printf("\nThe list is:\n");
- while(temp!= NULL)
- {
- printf("%d ", temp->x);
- temp = temp->ptr;
- m++;
- }
- printf("\nSearch test case:");
- scanf("%d", &t);
- for(j=1; j<=t; j++)
- {
- c = 1;
- printf("\nEnter data you want to search : ");
- scanf("%d", &s);
- temp = head;
- if(head->x == s)
- {
- printf("%d found at node %d", s, c);
- }
- else
- {
- while(temp != NULL && temp->x != s)
- {
- temp = temp->ptr;
- c++;
- }
- if(temp == NULL)
- {
- printf("%d does not exist in the list", s);
- }
- else
- {
- printf("%d found at node %d", s, c);
- }
- }
- }
- printf("\nDelete with position test case:");
- scanf("%d", &t);
- for(j=1; j<=t; j++)
- {
- printf("\n\nInput the position of node to delete : ");
- scanf("%d", &i);
- if(i > n)
- {
- printf("Node not found\n");
- break;
- }
- else
- {
- temp = head;
- if(i == 1)
- {
- head = head->ptr;
- free(temp);
- }
- else
- {
- c = 1;
- temp = head;
- node *previous = NULL;
- while(temp != NULL && c<=i-1)
- {
- previous = temp;
- temp = temp->ptr;
- c++;
- }
- previous->ptr = temp->ptr;
- free(temp);
- }
- printf("\nDeletion complete successfully.\n");
- printf("\nThe new list is :\n");
- temp = head;
- while(temp != NULL)
- {
- printf("%d ", temp->x);
- temp = temp->ptr;
- }
- printf("\n");
- break;
- }
- }
- printf("\nDelete with data test case:");
- scanf("%d", &t);
- for(k=1; k<=t; k++)
- {
- l = 0;
- h = 1;
- printf("\nEnter data you want to delete : ");
- scanf("%d", &s);
- temp = head;
- if(head->x == s)
- {
- head = head->ptr;
- printf("%d founded & deleted at node %d\n", s, h);
- free(temp);
- }
- else
- {
- node *previous = NULL;
- while(temp != NULL && temp->x != s)
- {
- previous = temp;
- temp = temp->ptr;
- h++;
- }
- if(temp == NULL)
- {
- printf("%d does not exist in the list\n", s);
- break;
- }
- else
- {
- previous->ptr = temp->ptr;
- printf("%d founded & deleted at node %d\n", s, h);
- free(temp);
- }
- }
- printf("\nThe List after deletion the data :\n");
- temp = head;
- while(temp != NULL)
- {
- printf("%d ", temp->x);
- temp = temp->ptr;
- l++;
- }
- printf("\n\nTotal Nodes : %d\n", l);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement