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();
- findNode();
- printList();
- findNode();
- printList();
- }
- void findNode(){
- printf("Enter The Position : ");
- int p;
- scanf("%d",&p);
- int c=countNode();
- if(p<1||p>c){
- printf("**%d Position Not Available**\n",p);
- return;
- }
- if(p==1){
- printf("Node Data = %d\n",h->d);
- printf("Deleting This Node\n");
- node *tmp=h;
- h=h->n;
- free(tmp);
- printf("Adding Node In Position : %d\n",p);
- int x;
- printf("Enter The Data : ");
- scanf("%d",&x);
- node *tmp1=(node*)malloc(sizeof(node));
- tmp->d=x;
- tmp->n=h;
- h=tmp;
- return;
- }
- node *list=h;
- int i=0;
- while(list){
- i++;
- if(i==(p-1)){
- printf("Node Data = %d\n",list->n->d);
- printf("Deleting This Node\n");
- node *tmp=list->n;
- list->n=list->n->n;
- free(tmp);
- printf("Adding Node In Position : %d\n",p);
- int x;
- printf("Enter The Data : ");
- scanf("%d",&x);
- node *tmp1=(node*)malloc(sizeof(node));
- tmp->d=x;
- 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");
- }
Add Comment
Please, Sign In to add comment