Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Database{
- int d;
- struct Database *n;
- }node;
- node *h;
- int main(){
- createList();
- printList();
- int c=countNode();
- printf("Total Node = %d\n",c);
- deleteNode();
- printList();
- findNode();
- printList();
- }
- void deleteNode(){
- printf("Enter N : ");
- int n;
- scanf("%d",&n);
- int c=countNode();
- if(n<2||n>(c+1)){
- printf("%d No Node Not Available\n",n-1);
- return;
- }
- if(n==2){
- printf("Deleting %d No Node\n",n-1);
- node *tmp=h;
- h=h->n;
- free(tmp);
- return;
- }
- node *list=h;
- int i=0;
- while(list){
- i++;
- if(i==(n-2)){
- node *tmp=list->n;
- list->n=list->n->n;
- free(tmp);
- return;
- }
- list=list->n;
- }
- }
- void findNode(){
- printf("Enter The Data : ");
- int x;
- scanf("%d",&x);
- if(x==h->d){
- printf("Node Position = 1\n");
- node *tmp=h;
- h=h->n;
- free(tmp);
- tmp=(node*)malloc(sizeof(node));
- printf("Enter Data In New Node : ");
- int n;
- scanf("%d",&n);
- tmp->d=n;
- tmp->n=h->n;
- h=tmp;
- return;
- }
- node *list=h;
- int i=0;
- while(list->n){
- i++;
- if(list->n->d==x){
- printf("Node Position = %d\n",i+1);
- node *tmp=list->n;
- list->n=list->n->n;
- free(tmp);
- tmp=(node*)malloc(sizeof(node));
- printf("Enter Data In New Node : ");
- int n;
- scanf("%d",&n);
- tmp->d=n;
- tmp->n=list->n;
- list->n=tmp;
- return;
- }
- list=list->n;
- }
- }
- int countNode(){
- node *list=h;
- int i=0;
- while(list){
- i++;
- list=list->n;
- }
- return i;
- }
- void createList(){
- node *n1,*n2,*n3,*n4;
- n1=(node*)malloc(sizeof(node));
- n2=(node*)malloc(sizeof(node));
- n3=(node*)malloc(sizeof(node));
- n4=(node*)malloc(sizeof(node));
- n1->d=1;
- n1->n=n2;
- n2->d=2;
- n2->n=n3;
- n3->d=4;
- n3->n=n4;
- n4->d=5;
- n4->n=NULL;
- h=n1;
- }
- void printList(){
- node *list=h;
- while(list){
- printf("%d ",list->d);
- list=list->n;
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement