Skip to main content
- #include<iostream>
- using namespace std;
- struct node
- {
- int data;
- struct node *next;
- };
- void insrt(node *s, int data)
- {
- while(s->next != NULL)
- {
- s = s->next;
- }
- s->next = (node*) malloc(sizeof(node));
- s->next -> data = data;
- s->next ->next = NULL;
- }
- void insrtF(node *s, int data)
- {
- node *t = s->next;
- s->next = (node*) malloc(sizeof(node));
- s->next->data = data;
- s->next->next = t;
- }
- void insrtAfter(node *s, int srch, int data)
- {
- while(s->next != NULL)
- {
- if(s->next->data == srch)
- {
- node *str = s->next->next;
- s->next ->next = (node*) malloc(sizeof(node));
- s->next->next->data = data;
- s->next->next->next = str;
- break;
- }
- s = s->next;
- }
- }
- void srch(node *s, int data)
- {
- int c = 0;
- while(s ->next != NULL)
- {
- if(s->next ->data == data)
- {
- c++;
- }
- s = s->next;
- }
- cout << data << " Found " << c << " Times." << endl << endl;
- }
- void deleteIndivisual(node *s, int data)
- {
- int c= 0;
- while(s->next != NULL)
- {
- if(s->next->data == data)
- {
- s->next = s->next->next;
- c = 1;
- break;
- }
- s = s->next;
- }
- if(c == 1)
- {
- cout << data << " Successfully Deleted." << endl << endl;
- }
- else
- {
- cout << data << " is not Found." << endl << endl;
- }
- }
- void deleteF(node *s)
- {
- s->next = s->next->next;
- }
- void deleteL(node *s)
- {
- while(s->next != NULL)
- {
- if(s->next->next == NULL)
- {
- s->next = NULL;
- break;
- }
- s = s->next;
- }
- }
- void display(node *s)
- {
- while(s->next != NULL)
- {
- cout << s->next->data << endl;
- s = s->next;
- }
- cout << endl;
- }
- void countnode(node *s)
- {
- int c = 0;
- while(s->next != NULL)
- {
- c++;
- s = s->next;
- }
- cout << "Total " << c << " Node Found." << endl << endl;
- }
- int main()
- {
- node *h = (node*) malloc(sizeof(node));
- h ->data = INT_MAX;
- h->next = NULL;
- insrt(h,1);
- insrt(h,2);
- insrt(h,3);
- insrt(h,4);
- insrt(h,5);
- insrtF(h, 99);
- insrtAfter(h, 3, 10);
- srch(h, 5);
- display(h);
- deleteIndivisual(h, 4);
- deleteL(h);
- display(h);
- countnode(h);
- return 0;
- }
Comments
Post a Comment