Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- void add(struct node **, int num);
- void display(struct node *);
- int count (struct node *);
- struct node
- {
- int data;
- struct node *link;
- };
- void main()
- {
- struct node *p;
- p=NULL;
- clrscr();
- add(&p,5);
- add(&p,3);
- add(&p,4);
- add(&p,7);
- add(&p,0);
- add(&p,41);
- add(&p,71);
- add(&p,20);
- add(&p,434);
- add(&p,-7);
- add(&p,80);
- printf("\n\nNo of Elements in the linked list %d\n\n",count(p));
- printf("\nElements in the linked list in the ascending fashion\n");
- display(p);
- getch();
- }
- /*add the element in the ascending order*/
- void add(struct node **q, int num)
- {
- struct node *temp=*q,*r;
- r = (struct node*) malloc (sizeof(struct node*));
- r->data = num;
- /*if the linked list is empty or
- the first node is to be added at begning*/
- if(*q==NULL ||(*q)->data>num)
- {
- *q = r;
- (*q)->link = temp;
- }
- else
- {
- /*traverse the entire linked list
- ti find the accurate position*/
- while(temp!=NULL)
- {
- if(temp->data<=num && (temp->link==NULL || temp->link->data>num))
- {
- r->link = temp->link;
- temp->link = r;
- return ;
- }
- temp = temp->link;/*go to the next node*/
- }
- }
- }
- /* displays the contenet of element */
- void display(struct node *q)
- {
- /* traverse the entire linked list */
- while(q!=NULL)
- {
- printf("%d ", q->data);
- q=q->link;
- }
- printf("\n");
- }
- /* counts the number of nodes present in the linked list*/
- int count(struct node *q)
- {
- int c=0;
- /* traverse the entire linked list */
- while(q!=NULL)
- {
- q=q->link;
- c++;
- }
- return c;
- }
Advertisement
Add Comment
Please, Sign In to add comment