Skip to main content

Interactive C program to demonstrate Circular single linked list

/* Program to Demonstrate Circular single linked list */
#include <stdio.h>
#include <stdlib.h>

/* Circular single linked list */
struct clist
{
int data;
struct clist *next;
};
typedef struct clist *CL;

/* inserting elements into Circular list just after first(head) node */
void insert(CL *head)
{
 CL temp;
 int e;

 temp=malloc(sizeof(struct clist));
 if(temp == NULL){
    printf("Can't allocate memory\n");
    return;
 }

 printf("Enter element\n");
 scanf("%d", &e);
 temp->data = e;

 if(*head == NULL){
     *head = temp;
     (*head)->next = temp;
 } else {
    temp->next = (*head)->next;
   (*head)->next =temp;
    *head =temp;
   
 }

}
 
/* Displaying elements in list */
void display(CL head)
{
 CL temp;

 if(head == NULL) {
    printf("Empty list \n");
    return;
 }

 temp = head;
 printf("Elements are \n");

do {
     /* it circular list after head->next is  other end of the list*/
     printf("%d\n", temp->data);
     temp=temp->next;
 } while (temp != head);

}

/* deleting the element right after the root node from circular
 * single linked list
 */
void del(CL *head)
{
  CL temp;

  if(*head == NULL){
     printf("Empty list ..\n");
     return;
  }

  temp = (*head)->next;
  printf("Element deleted %d", temp->data);
  if( temp != *head){
      (*head)->next = temp->next;
      free(temp);
  } else {

    free(*head);
   *head = NULL;
  }

}

/* for main program you could refer below post to test above program */

/* end of the program */

Comments

Popular posts from this blog

C Program to Simulate Linux Cat command

/*  * C program to Simulate Implementation of  Linux *cat* command  * This program will not handle redirection  * you can try to improve this program to handle redirection of file contents  */ #include<stdio.h> #include<string.h> #define MAX_FILE_NAME_CHARS 255 int main(int argc, char *argv[]) {  FILE *fp;  char file_name[MAX_FILE_NAME_CHARS], ch;  int i;  /*   * after creating a.out, rename it as mycat for our own cat command   * and it usage is same as standard cat command   */  if(argc<1){     printf("Usage mycat <filename> \n");     return 0;  }  /*   * cat handles more than one file   * so we need this loop to handle all the files provided   * on command line   */  for(i=1; i<=argc;i++){     /* no need of copy but for understanding purpose, i have created      * str...

simple program to create Orphan process

shiv@ubuntu:~/ds/unix$ cat  orp.c /*  * Program to create orphan process @ Linux  * getpid() gives process PID and   * getppid() gives process's parent ID   * here main() process ID is parent id is current shells PID  * once process becomes orphan it is adopted by init process(it's PID is 1)  */   #include<stdio.h> #include<unistd.h> int main() {  pid_t p; /* create child process */  p=fork();  if(p==0) {     /* fork() returns Zero to child */     sleep(10);  }  printf("The child process pid is %d parent pid %d\n", getpid(), getppid()); /*parent/child waits for 20 secs and exits*/  sleep(20);  printf("\nProcess %d is done its Parent pid %d...\n", getpid(), getppid());  return 0; } O/p shiv@ubuntu:~/ds/unix$ ./a.out The child process pid is 2575 parent pid 1922 The child process pid is 2576 parent pid 2575 Process 2575 is done it...

C Program to find Armstrong numbers between range m and n

/*  * Program to print Armstrong number between range m and n  * Armstrong  number is nothing but sum of cubes of digits of number is  * equal to the number  * ex: 153 is Armstrong number since 1^3+5^3+3^3=153  * there are very few numbers hold above property; execute   * below program to find all numbers  * b/w m and n  */ #include <stdio.h> void is_armstrong(int num) {   int sum=0, n, d;   n=num;   while(n){   d=n%10;  /* extract digits of number*/   sum=sum+(d*d*d);  /*take sum of them */   n=n/10;  }  if(sum == num)  /*sum is same as number then it is Armstrong number*/     printf("%d\n", sum); } int main() { int i,n,m; printf("Enter range \n"); scanf("%d%d", &m, &n);  for(i=m;i<=n;i++){    is_armstrong(i);  } return 0; }