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();
- lastAdd();
- printList();
- deletMiddle();
- printList();
- int c=countNode();
- printf("Total Nodes = %d\n",c);
- }
- void lastAdd(){
- printf("Enter Data To Add : ");
- int x;
- scanf("%d",&x);
- node *list=h;
- while(list->n){
- list=list->n;
- }
- node *tmp=(node*)malloc(sizeof(node));
- tmp->d=x;
- tmp->n=NULL;
- list->n=tmp;
- }
- void deletMiddle(){
- int c=countNode();
- c=(c%2==0)?c/2:(c/2)+1;
- printf("**Deleting Node**\n");
- if(c==1){
- node *tmp=h;
- h=h->n;
- free(tmp);
- return;
- }
- int i=0;
- node *list=h;
- while(list){
- i++;
- if(i==(c-1)){
- node *tmp=list->n;
- list->n=list->n->n;
- free(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