top of page

Search Results

61 items found for ""

  • Circular Linked List

    A circular linked list is a type of linked list where the last node in the list points back to the first node, forming a circle. #include #include struct Node { int data; struct Node* next; }; struct CircularLinkedList { struct Node* head; int count; }; struct Node* create_node(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->next = NULL; return node; } struct CircularLinkedList* create_list() { struct CircularLinkedList* list = (struct CircularLinkedList*)malloc(sizeof(struct CircularLinkedList)); list->head = NULL; list->count = 0; return list; } void append(struct CircularLinkedList* list, int data) { struct Node* node = create_node(data); if (list->head == NULL) { list->head = node; node->next = node; // make the node point to itself } else { node->next = list->head->next; list->head->next = node; list->head = node; } list->count++; } int remove_node(struct CircularLinkedList* list, int data) { if (list->head == NULL) { return 0; } struct Node* current = list->head; struct Node* prev = NULL; do { if (current->data == data) { if (current == list->head) { list->head = prev; } prev->next = current->next; free(current); list->count--; return 1; } prev = current; current = current->next; } while (current != list->head->next); return 0; } void print_list(struct CircularLinkedList* list) { if (list->head == NULL) { return; } struct Node* current = list->head->next; do { printf("%d ", current->data); current = current->next; } while (current != list->head->next); printf("\n"); } int main() { struct CircularLinkedList* list = create_list(); append(list, 1); append(list, 2); append(list, 3); print_list(list); // output: 1 2 3 remove_node(list, 2); print_list(list); // output: 1 3 return 0; }

  • Doubly Linked List

    A doubly linked list is a data structure in which each node contains not only a data element but also two pointers, one pointing to the previous node and one pointing to the next node in the list. This allows for efficient traversal in both directions, as well as easy insertion and deletion of nodes at any position in the list. #include #include struct Node { int data; struct Node* prev; struct Node* next; }; struct DoublyLinkedList { struct Node* head; struct Node* tail; int count; }; struct Node* create_node(int data) { struct Node* node = (struct Node*)malloc(sizeof(struct Node)); node->data = data; node->prev = NULL; node->next = NULL; return node; } struct DoublyLinkedList* create_list() { struct DoublyLinkedList* list = (struct DoublyLinkedList*)malloc(sizeof(struct DoublyLinkedList)); list->head = NULL; list->tail = NULL; list->count = 0; return list; } void append(struct DoublyLinkedList* list, int data) { struct Node* node = create_node(data); if (list->head == NULL) { list->head = node; list->tail = node; } else { node->prev = list->tail; list->tail->next = node; list->tail = node; } list->count++; } int remove_node(struct DoublyLinkedList* list, int data) { struct Node* current = list->head; while (current != NULL) { if (current->data == data) { if (current == list->head && current == list->tail) { list->head = NULL; list->tail = NULL; } else if (current == list->head) { list->head = current->next; list->head->prev = NULL; } else if (current == list->tail) { list->tail = current->prev; list->tail->next = NULL; } else { current->prev->next = current->next; current->next->prev = current->prev; } free(current); list->count--; return 1; } current = current->next; } return 0; } void print_list(struct DoublyLinkedList* list) { struct Node* current = list->head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } int main() { struct DoublyLinkedList* list = create_list(); append(list, 1); append(list, 2); append(list, 3); print_list(list); // output: 1 2 3 remove_node(list, 2); print_list(list); // output: 1 3 return 0; }

  • Reverse Linked List

    #include #include struct node{ int data; struct node *next; }; struct node *head,*newnode,*prevnode,*nextnode; int choice; void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d",&choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } printf("\n\n"); prevnode=0,temp=nextnode=head; while(nextnode!=0) { nextnode=nextnode->next; temp->next=prevnode; prevnode=temp; temp=nextnode; } head=prevnode; temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } }

  • Linked List Deletion At Any Position

    #include #include struct node{ int data; struct node *next; }; struct node *head,*newnode,*nextnode; int choice,i=1,pos; void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d",&choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } printf("\n\n----Deletion From Any Position----"); temp=head; printf("\nEnter position to delete : "); scanf("%d",&pos); while(inext; i++; } if(temp==head) { head=0; free(temp); } else { nextnode=temp->next; temp->next=nextnode->next; free(nextnode); } temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } }

  • Linked List Deletion At End

    #include #include struct node{ int data; struct node *next; }; struct node *head,*newnode,*prevnode; int choice; void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d", &newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d", &choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d", temp->data); temp=temp->next; } printf("\n\n----Deletion From End----"); temp=head; while(temp->next!=0) { prevnode=temp; temp=temp->next; } if(temp==head) { head=0; } else { prevnode->next=0; } free(temp); temp=head; printf("\n"); while(temp!=0) { printf("--%d", temp->data); temp=temp->next; } }

  • Linked List Deletion At Beginning

    #include #include struct node{ int data; struct node *next; }; struct node *head,*newnode; int choice; void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d",&choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } printf("\n\n----Deletion From Beginning----"); temp=head; printf("\n\n---%d is successfully deleted---\n",head->data); head=head->next; free(temp); temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; } }

  • Length of a Linked List

    #include struct node{ int data; struct node *next; }; struct node *head,*newnode; int choice; int length=0; void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d",&choice); }while(choice); temp=head; printf("\n\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; length++; } printf("\n\nLength of linked list : %d",length); }

  • Linked List Insertion At Any Position

    #include struct node{ int data; struct node *next; }; struct node *head,*newnode; int choice; int pos, i=1; void main() { printf("\n----Insertion At Any position----"); struct node *temp; int count=0; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d", &newnode->data); printf("\nEnter position : "); scanf("%d", &pos); newnode->next=0; if(head==0) { head=temp=newnode; } else{ temp=head; while(inext; } newnode->next=temp->next; temp->next=newnode; } printf("\nEnter your choice : (0/1) "); scanf("%d", &choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d",temp->data); temp=temp->next; count++; } printf("\nLength of linked list : %d",count); }

  • Linked List Insertion At End

    #include struct node{ int data; struct node *next; }; struct node *head,*newnode; int choice; void main() { printf("\n----Insertion At End----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d", &newnode->data); newnode->next=0; if(head==0) { head=temp=newnode; } else{ temp=head; while(temp->next!=0) { temp=temp->next; } temp->next=newnode; } printf("\nEnter your choice : (0/1) "); scanf("%d", &choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d", temp->data); temp=temp->next; } }

  • Linked List Insertion At Beginning

    #include struct node{ int data; struct node *next; }; struct node *head,*newnode; int choice; void main() { printf("\n----Insertion At Beginning----"); struct node *temp; do { newnode = (struct node *)malloc(sizeof(struct node)); printf("\nEnter data : "); scanf("%d",&newnode->data); newnode->next=head; head=newnode; printf("\nEnter your choice : (0/1) "); scanf("%d",&choice); }while(choice); temp=head; printf("\n"); while(temp!=0) { printf("--%d", temp->data); temp=temp->next; } }

  • What are Arrays

    In C programming, an array is a collection of elements of the same data type, such as integers, characters, or floating-point numbers, stored in contiguous memory locations. Each element in an array is identified by an index or a subscript, which is a non-negative integer value that starts from zero and goes up to the size of the array minus one. Arrays are useful for storing and manipulating large amounts of data efficiently. They can also be used to represent tables, matrices, and other multi-dimensional data structures. In C, you can declare an array by specifying the data type of its elements, followed by its name, and its size in square brackets, like this: int numbers[5]; numbers[0] = 10; numbers[1] = 20; int numbers[5] = {10, 20, 30, 40, 50};

  • Array Deletion At Any Position

    #include void main() { int a[100],i; int eb,size,pos; printf("\nEnter the size of array : "); scanf("%d",&size); for(i=0;i

bottom of page