Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<stdlib.h>
- void append(struct node ** , int );
- void addtobeg(struct node ** , int );
- void addafter(struct node* , int , int);
- void display(struct node *);
- int count(struct node*);
- void del(struct node**,int);
- struct node
- {
- int data;
- struct node *link;
- };
- void main()
- {
- struct node *p;
- p = NULL;
- clrscr();
- printf("\n\nNumber of element in the linked list %d\n",count(p));
- append(&p,14);
- append(&p,51);
- append(&p,34);
- append(&p,76);
- append(&p,90);
- append(&p,154);
- //clrscr();
- printf("\nElement in the linked list after inserting \n");
- display(p);
- addtobeg(&p,657);
- addtobeg(&p,99);
- addtobeg(&p,99);
- printf("\nElement in the linked list after inserting at begning\n");
- display(p);
- addafter(p,7,0);
- addafter(p,17,4);
- addafter(p,56,2);
- printf("\nElement in the linked list after inserting in the middle \n");
- display(p);
- printf("No of element in the linked list::%d", count(p));
- del(&p,657);
- del(&p,99);
- del(&p,14);
- display(p);
- printf("No of element in the linked list::%d" , count(p));
- getch();
- }
- void append(struct node**q, int num)
- {
- struct node *temp, *r;
- if(*q==NULL)
- {
- temp = (struct node *)malloc(sizeof(struct node));
- temp->data = num;
- temp->link = NULL;
- *q = temp;
- }
- else
- {
- temp = *q;
- /*go to the last node*/
- while(temp->link!=NULL)
- temp = temp->link;
- /*add node at the end */
- r = (struct node *)malloc(sizeof(struct node));
- r->data = num;
- r->link = NULL;
- temp->link = r;
- }
- }
- /*Adds a new node at the begning of the lunked list*/
- void addtobeg(struct node **q, int num)
- {
- struct node *temp;
- /*add nes node*/
- temp = (struct node *)malloc(sizeof(struct node));
- temp->data = num;
- temp->link = *q;
- *q = temp;
- }
- /*Adds a new node after specified node*/
- void addafter(struct node *q , int loc , int num)
- {
- struct node *temp, *r;
- int i;
- temp=q;
- /*skip to desired position */
- for(i=0;i<loc;i++)
- {
- temp=temp->link;
- /* if end of the linked list is encountered */
- if(temp==NULL)
- {
- printf("There are less than %d element in list\n",loc);
- return ;
- }
- }
- /* insert new node */
- r = (struct node *)malloc(sizeof(struct node)) ;
- r->data = num;
- r->link = temp->data;
- temp->link = r;
- }
- /* 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;
- }
- void del(struct node **q, int num)
- {
- struct node *temp, *old;
- temp = *q;
- while(temp!=NULL)
- {
- if(temp->data == num)
- { /*if node is deleted at the 1st position*/
- if(temp==*q)
- *q=temp->link;
- /*delete the intermediate node in the lnked list*/
- else
- old->link=temp->link;
- /*free the node the occupied the memory*/
- free(temp);
- return ;
- }
- /*traverse the entire linked list*/
- else
- {
- old = temp; //old point to the previous node
- temp= temp->link;//go to the next node
- }
- }
- printf("\n\nElement is nod found %d",num);
- }
Advertisement
Add Comment
Please, Sign In to add comment