Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void createList();
- void printList();
- void insertAtLast();
- int countNode();
- void deleteFromMid();
- typedef struct Database{
- int d;
- struct Database *n;
- }node;
- node *h;
- int main(void) {
- createList();
- printList();
- insertAtLast();
- printList();
- deleteFromMid();
- printList();
- printf("%d",countNode());
- return 0;
- }
- void insertAtLast(){
- printf("Enter Data : ");
- int a;
- scanf("%d",&a);
- node *list=h;
- while(list->n){
- list=list->n;
- }
- node *tmp=(node*)malloc(sizeof(node));
- tmp->d=a;
- tmp->n=NULL;
- list->n=tmp;
- }
- void deleteFromMid(){
- node *list=h;
- int c=countNode();
- c= (c%2==0)?c/2:(c/2)+1;
- int i=1;
- while(i<(c-1)){
- list=list->n;
- i++;
- }
- node *tmp=list->n;
- list->n=list->n->n;
- free(tmp);
- }
- int countNode(){
- node *list=h;
- int i=0;
- while(list){
- i++;
- list=list->n;
- }
- return i;
- }
- void createList(){
- node *n1=(node*)malloc(sizeof(node));
- node *n2=(node*)malloc(sizeof(node));
- node *n3=(node*)malloc(sizeof(node));
- node *n4=(node*)malloc(sizeof(node));
- n1->d=1;
- n1->n=n2;
- n2->d=2;
- n2->n=n3;
- n3->d=3;
- n3->n=n4;
- n4->d=4;
- 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