Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_DEPRECATE
- #define _CRT_SECURE_NO_WARNINGS
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct
- {
- struct node* next;
- int data;
- }node;
- typedef struct
- {
- node* start;
- }head;
- void insertbeg(head *t, int ele)
- {
- node *p;
- p = (node*)malloc(sizeof(node));
- p->data = ele;
- p->next = t->start;
- t->start = p;
- }
- void insertend(head *t, int ele)
- {
- node *p,*q;
- p = (node*)malloc(sizeof(node));
- p->data = ele;
- p->next = NULL;
- q = t->start;
- if (t->start == NULL)
- t->start = p;
- else
- {
- while (q->next != NULL)
- {
- q = q->next;
- }
- q->next = p;
- }
- }
- int count(head *t)
- {
- int c = 0;
- node* q;
- q = t->start;
- while (q)
- {
- c++;
- q = q->next;
- }
- return c;
- }
- int findmax(head *t)
- {
- int max;
- node* q;
- q = t->start;
- if (q == NULL)
- printf("\nLinked List Empty\n");
- else
- {
- max = q->data;
- while (q!=NULL)
- {
- if (q->data > max)
- max = q->data;
- q = q->next;
- }
- return max;
- }
- return 0;
- }
- void display(head *t)
- {
- node* q;
- q = t->start;
- if (!q)
- {
- printf("\nLinked List Empty\n");
- return;
- }
- while (q)
- {
- printf("%d\t", q->data);
- q = q->next;
- }
- printf("\n");
- }
- void sort(head *t)
- {
- node *p, *q = t->start;
- int temp;
- p = q->next;
- while (q->next)
- {
- if (p->data > q->data)
- {
- temp = p->data;
- p->data = q->data;
- q->data = temp;
- }
- p = p->next;
- q = q->next;
- }
- display(t);
- }
- void insertpos(head *t, int ele, int pos)
- {
- node *p, *q;
- int i;
- p = (node*)malloc(sizeof(node));
- p->data = ele;
- q = t->start;
- for (i = 0; i < pos - 2; ++i)
- q = q->next;
- p->next = q->next;
- q->next = p;
- }
- int search(head *t, int ele)
- {
- node *q;
- q = t->start;
- while (q)
- {
- if (q->data == ele)
- return 1;
- else
- q = q->next;
- }
- return 0;
- }
- int mean(head *t)
- {
- int avg=0;
- node *q;
- q = t->start;
- while (q->next)
- {
- avg += q->data;
- q = q->next;
- }
- avg /= count(t);
- return avg;
- }
- int deletebeg(head *t)
- {
- node* q = t->start;
- if (!q)
- {
- printf("\nLinked List empty.\n");
- return -1;
- }
- else
- t->start = q->next;
- return q->data;
- }
- void deleteele(head* t, int ele)
- {
- node *q,*p;
- if (!search(t, ele))
- {
- printf("\nElement not found\n");
- return;
- }
- q = t->start;
- p = q->next;
- while (q->next)
- {
- if (p->data = ele)
- {
- q->next = p->next;
- return;
- }
- q = q->next;
- p = p->next;
- }
- }
- void main()
- {
- head x;
- int ch, ele,pos;
- x.start = NULL;
- while (1)
- {
- printf("\nEnter your choice.\n1.Insertbeg\n2.Insertend\n3.Display\n4.Sort\n5.Count\n6.Deletebeg\n7.Deleteele\n8.InsertPos\n9.Findmax\n10.Search\n11.Mean\n12.Exit\n\n");
- scanf("%d", &ch);
- if (ch == 12)
- break;
- switch (ch)
- {
- case 1:
- printf("\nEnter the element to be inserted\n");
- scanf("%d", &ele);
- insertbeg(&x, ele);
- display(&x);
- break;
- case 2:
- printf("\nEnter the element to be inserted\n");
- scanf("%d", &ele);
- insertend(&x, ele);
- display(&x);
- break;
- case 3:
- display(&x);
- break;
- case 4:
- sort(&x);
- break;
- case 5:
- printf("\nThe number of elements in the Linked List is %d", count(&x));
- break;
- case 6:
- if (x.start == NULL)
- printf("\nLinked List empty\n");
- else
- printf("\nThe deleted element is %d", deletebeg(&x));
- break;
- case 7:
- printf("\nEnter the element to be deleted\n");
- scanf("%d", &ele);
- deleteele(&x, ele);
- display(&x);
- break;
- case 8:
- printf("\nenter the element to be inserted and its position\n");
- scanf("%d%d", &ele, &pos);
- insertpos(&x, ele, pos);
- display(&x);
- break;
- case 9:
- if (x.start == NULL)
- printf("\nLinked List Empty\n");
- else
- printf("\nMaximum of the Linked List is %d", findmax(&x));
- break;
- case 10:
- printf("\nEnter element to be searched\n");
- scanf("%d", &ele);
- if (search(&x, ele))
- {
- printf("\nElement is found\n");
- }
- else
- printf("\nNot Found\n");
- break;
- case 11:
- if (x.start == NULL)
- printf("\nLinked List Empty\n");
- else
- printf("\nAverage of the elements of the Linked List is %d", mean(&x));
- break;
- default:
- printf("\nInvalid Choice\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement