Skip to main content

Posts

Showing posts with the label C Datat structures

Program to Swap Adjecent nodes in single linked list

shiv@ubuntu:~/ds/list$ cat   altxchangenode.c /*  * program to Xchange or swap all the adjecent nodes(not node data) in the single linked list  */ #include<stdio.h> #include<stdlib.h> #define MAX 10 struct list {  int d;  struct list *next; }; /*  * function to swap 2 adjecent nodes(linkes) in the list  */ void xchangenodes(struct list **head, struct list **ne, struct list **tm) {   struct list *temp, *x1, *x2;   if(*tm == NULL){      return;   }   if((*tm)->next && (*tm)->next->next){       /*next node when more than 3 nodes present in the list */      temp=(*tm)->next->next;   } else if((*tm)->next){       if(*head != NULL) {   /* only two nodes present in the list */          (*ne)->next = (*tm)->next;       ...

Interactive C program to Simulate Queue operations on Circular Queue using arrays

shiv@ubuntu:~/ds/queue$ cat  circularq.c /*  * Interactive program to simulate Circular Queue using arrays  */ #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_SIZE 5 #define FALSE 0 #define TRUE 1 /* Insert element into Queue */ int insert(int cq[], int e, int head, int *tail) {   if((*tail + 1)%MAX_SIZE == head){       printf("Queue is full\n");       return FALSE;   }   cq[*tail]=e;  *tail = (*tail +1)% MAX_SIZE;   return TRUE; } /* Delete element from queue */ int  del(int cq[], int *head, int tail) {  if(tail == *head) {     printf("Queue is empty");     return FALSE;  }  printf("deleted %d\n", cq[*head]);  *head = (*head + 1)%MAX_SIZE;  return TRUE;      } /* Display elements in the queue */ void display(int cq[], int h, int t...

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 == N...

Interactive C program to implement Instert begining, delete begining and reversing of single linked list

shiv@ubuntu:~/ds/list$ cat   revlist.c /*  * Interactive C program to simulate following operations  * on single linked list  * 1. insert begining of the list  * 2. display data in the list (traversing)  * 3. delete element at begining of the list  * 4. reverse the elements in single linked list  */ #include<stdio.h> #include<stdlib.h> struct list { int data; struct list *next; }; typedef struct list *L; /* function to insert element into list */ void insert(L *head) {  /* look for typedef temp is pointer not variable,   *  same will apply to all   */  L temp;  temp=malloc(sizeof(struct list));  if(!temp) {     printf("Can't allocate memory\n");     return;  }  printf("Enter element to insert\n");  scanf("%d", &temp->data);  temp->next = NULL;  if(*head == NULL) {     *head = temp; ...

Interactive program to simulate Queue functionality using Linked list

/*  * Copy right by Shiv Yaragatti  * you are free to use, modify  and redistribute code on own risk  * Interactive C Program to implement Queue using  linked list  */ /* header needs have all required header files like stdio.h etc */  #include "header.h" struct queue { int data; struct queue *next; }; typedef struct queue *Q; /* Queue head */ struct qhead_struct { struct queue  *head; struct queue  *tail; }; typedef struct qhead_struct  *H; /* Insert elements into queue */ void insert(H *hq) {   int e;   Q temp;  temp= malloc(sizeof(struct queue));  if(!temp) {    printf("Can't allocate memory\n");    return;  }  printf("Enter element\n");  scanf("%d", &e);  temp->data =e;  temp->next = NULL; /* add element queue at tail */  if((*hq)->head == NULL) {     (*hq)->head =(*hq)->tail =temp;  } else { ...