Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct data
- {
- int a;
- struct data *next;
- }data;
- data *head=NULL;
- void insert_at_first(int x)
- {
- data *newnode = (*data)malloc(sizeof(data));
- newnode ->a = x;
- newnode ->next = NULL;
- if(head==NULL)
- {
- head=newnode;
- return;
- }
- newnode -> next = head;
- newnode = head;
- }
- void insert_at_end(int x)
- {
- data *node = (*data)malloc(sizeof(data));
- node -> a = x;
- node -> next = NULL;
- data *temp = head;
- if(head==NULL)
- {
- head = node;
- return;
- }
- while(temp -> next!=NULL)
- {
- temp = temp->next;
- }
- temp -> next = node;
- }
- void insert_at_ntn(int n, int x)
- {
- data *new = (*data)malloc(sizeof(data));
- new -> a= x;
- new -> next = NULL;
- data *temp = head;
- if(head==NULL || n==1)
- {
- new -> next =head;
- head = new;
- return;
- }
- n = n-2;
- while(n-- && temp -> next != NULL)
- {
- temp = temp -> next;
- }
- new -> next = temp ->next;
- temp -> next = new;
- }
- void delete_by_pos(int n)
- {
- data *temp = head;
- if(head==NULL)
- {
- printf("not found\n");
- return;
- }
- if(n==1)
- {
- head = temp -> next;
- free (temp);
- return;
- }
- n=n-2;
- while(n-- && temp ->next!=NULL)
- {
- temp = temp -> next;
- }
- data *del = temp -> next;
- if(del==NULL)
- {
- return;
- }
- temp -> next = del -> next;
- free (del);
- }
- void delete_by_value(int n)
- {
- data *temp = head;
- if(temp ->a==n)
- {
- head = temp -> next;
- free (temp);
- }
- while(temp -> next != NULL)
- {
- if(temp ->next ->a==n)
- {
- data *del = temp ->next;
- temp ->next = temp ->next ->next;
- free (del);
- }
- temp = temp ->next;
- }
- }
- void search(int x)
- {
- data *temp = head;
- int c=1;
- while(temp!=NULL)
- {
- if(temp ->a ==x)
- {
- printf("%d Found!\n",x);
- return;
- }
- else
- {
- c++;
- temp = temp -> next;
- }
- }
- if(c>1)
- {
- printf("%d not found\n",x);
- return;
- }
- }
- int count()
- {
- int count = 0;
- data *temp = head;
- while(temp!=NULL)
- {
- count ++;
- temp = temp -> next;
- }
- printf("total element no: %d\n",count);
- return count;
- }
- int sum()
- {
- int add = 0;
- data *temp = head;
- while(temp!=NULL)
- {
- add=add+temp->a;
- temp=temp->next;
- }
- printf("Sum of our total element: %5d\n",add);
- return add;
- }
- void avg(float a, float b)
- {
- float avg;
- avg = a/b;
- printf("Avg: %.2f\n",avg);
- }
- void print()
- {
- data *temp = head;
- while(temp!=NULL)
- {
- printf("%5d",temp->a);
- temp=temp->next;
- }
- printf("\n");
- }
- int main()
- {
- int a1,a2,a3,a4,a5,a6,a7,a8,a9,a10;
- float c,s;
- printf("insert any data at first : ");
- scanf("%d", &a1);
- insert_at_first(a1);
- print();
- printf("Insert any data at first: ");
- scanf("%d",&a2);
- insert_at_first(a2);
- print();
- printf("Insert any data at first: ");
- printf("Insert any data at end: ");
- scanf("%d",&a4);
- insert_at_first(a4);
- print();
- printf("Insert any data at end: ");
- scanf("%d",&a5);
- insert_at_first(a5);
- print();
- printf("Insert any data at nth_position: ");
- scanf("%d",&a6);
- insert_at_first(a6);
- print();
- printf("Insert any data at nth_position: ");
- scanf("%d",&a7);
- insert_at_first(a7);
- print();
- printf("Insert any data at nth_position: ");
- scanf("%d",&a8);
- insert_at_first(a8);
- print();
- printf("Enter any value which you want to delete: ");
- scanf("%d",&a9);
- insert_at_first(a9);
- print();
- printf("Which value you want to search!: ");
- scanf("%d",&a10);
- search(a10);
- print();
- c=count();
- s=sum();
- avg(s,c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement