Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int id;
- struct node *next;
- struct node *prev;
- }*start=NULL,*end=NULL;
- void create()
- {
- int n,i;
- struct node *new_node,*current;
- printf("Enter the number of student: \n");
- scanf("%d",&n);
- for(i=0; i<n; i++)
- {
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("Enter the id of student no %d: \n",i+1);
- scanf("%d",&new_node->id);
- new_node->next=NULL;
- new_node->prev=NULL;
- if(start==NULL&&end==NULL)
- {
- start=new_node;
- end=new_node;
- current=new_node;
- }
- else
- {
- current->next=new_node;
- new_node->prev=current;
- current=new_node;
- end=new_node;
- }
- }
- }
- void f_insert()
- {
- struct node *new_node,*current;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("\nEnter the first id of student no : \n");
- scanf("%d",&new_node->id);
- new_node->next=NULL;
- new_node->prev=NULL;
- current=start;
- start=new_node;
- new_node->next=current;
- current->prev=new_node;
- }
- void l_insert()
- {
- struct node *new_node,*current;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("\nEnter the last id of student no : \n");
- scanf("%d",&new_node->id);
- new_node->next=NULL;
- new_node->prev=NULL;
- current=end;
- current->next=new_node;
- new_node->prev=current;
- end=new_node;
- }
- int search(int s_key)
- {
- int pos=0;
- struct node *current;
- current=start;
- while(current!=NULL)
- {
- pos++;
- if(current->id==s_key)
- {
- return pos;
- }
- current=current->next;
- }
- return -1;
- }
- void mb_insert()
- {
- int key,i,pos;
- struct node *temp,*current,*new_node;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("\nEnter the middle(before) id of student no : \n");
- scanf("%d",&new_node->id);
- new_node->next=NULL;
- new_node->prev=NULL;
- printf("\nEnter the id before the new node insert:\n");
- scanf("%d",&key);
- pos=search(key);
- current=start;
- for(i=1;i<=pos-1;i++)
- {
- current=current->next;
- }
- temp=current->prev;
- temp->next=new_node;
- new_node->prev=temp;
- new_node->next=current;
- current->prev=new_node;
- }
- void ma_insert()
- {
- int key,i,pos;
- struct node *temp,*current,*new_node;
- new_node=(struct node*)malloc(sizeof(struct node));
- printf("\nEnter the middle(after) id of student no : \n");
- scanf("%d",&new_node->id);
- new_node->next=NULL;
- new_node->prev=NULL;
- printf("\nEnter the id after the new node insert:\n");
- scanf("%d",&key);
- pos=search(key);
- current=start;
- for(i=1;i<=pos-1;i++)
- {
- current=current->next;
- }
- //printf("\nCurrent=%d",current->id);
- temp=current->next;
- current->next=new_node;
- new_node->prev=current;
- new_node->next=temp;
- temp->prev=new_node;
- }
- void delete_ll()
- {
- int dkey,pos,i;
- struct node *current,*temp1,*temp2;
- printf("\nEnter the value for deleting:\n");
- scanf("%d",&dkey);
- pos=search(dkey);
- current=start;
- for(i=1;i<=pos-1;i++)
- {
- current=current->next;
- }
- if(current==start)
- {
- temp1=current->next;
- temp1->prev=NULL;
- start=temp1;
- }
- else if(current==end)
- {
- temp1=current->prev;
- temp1->next=NULL;
- end=temp1;
- }
- else
- {
- temp1=current->prev;
- temp2=current->next;
- temp1->next=temp2;
- temp2->prev=temp1;
- }
- printf("\nDelete Successfully.");
- }
- void f_display()
- {
- struct node *current;
- current=start;
- printf("\nThe Link List from start is:\n");
- while(current!=NULL)
- {
- printf("%d-->",current->id);
- current=current->next;
- }
- printf("NULL\n");
- }
- void b_display()
- {
- struct node *current;
- current=end;
- printf("\nThe Link List from last is:\n");
- while(current!=NULL)
- {
- printf("%d-->",current->id);
- current=current->prev;
- }
- printf("NULL\n");
- }
- void max()
- {
- int max=-100000;
- struct node *current;
- current=start;
- while(current!=NULL)
- {
- if(current->id>max)
- {
- max=current->id;
- }
- current=current->next;
- }
- printf("\nThe maximum value is:%d.",max);
- }
- void sum_avg()
- {
- int sum=0,c=0;
- float avg;
- struct node *current;
- current=start;
- while(current!=NULL)
- {
- sum=sum+current->id;
- current=current->next;
- }
- printf("\nThe summation value is:%d.",sum);
- }
- void count()
- {
- int c=0;
- struct node *current;
- current=start;
- while(current!=NULL)
- {
- c++;
- current=current->next;
- }
- printf("\nThe num of node in the LL is:%d.",c);
- }
- void even_freq()
- {
- int even=0;
- struct node *current;
- current=start;
- while(current!=NULL)
- {
- if(current->id%2==0)
- {
- even++;
- }
- current=current->next;
- }
- printf("\nThe total even num is:%d.",even);
- }
- int main()
- {
- int s_key,pos;
- create();
- f_display();
- b_display();
- /*f_insert();
- f_display();
- b_display();
- l_insert();
- f_display();
- b_display();
- printf("\nEnter the id for searching:");
- scanf("%d",&s_key);
- pos=search(s_key);
- if(pos==-1)
- {
- printf("\nNOT FOUND");
- }
- else
- {
- printf("\n FOUND at %d",pos);
- }
- mb_insert();
- f_display();
- b_display();
- ma_insert();
- f_display();
- b_display();*/
- delete_ll();
- f_display();
- b_display();
- max();
- sum_avg();
- count();
- even_freq();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment