#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node* next;
};
void add(struct node** tail, int val) {
struct node* temp = malloc(sizeof(struct node));
if(*tail == NULL) {
temp->data = val;
temp->next = temp;
*tail = temp;
} else {
temp->data = val;
temp->next = (*tail)->next;
(*tail)->next = temp;
}
}
void disp(struct node* tail) {
if(tail == NULL)
printf("NULL");
else {
struct node* p = tail->next;
do {
printf("<-%d", p->data);
p = p->next;
}while(p!=tail->next);
}
}
int search(struct node* tail, int val) {
if(tail == NULL){
printf("list is empty");
} else {
struct node* p = tail->next;
do {
if(p->data == val){
return 1;
break;
}
else {
p = p->next;
}
} while(p!=tail->next);
return -1;
}
}
void deleteNode(struct node** tail, int position) {
struct node* p = (*tail)->next;
(*tail)->next = p->next; //Updating tails next to next node
free(p);
p = NULL;
}
int main() {
int search_val = 4;
int del_pos = 3;
struct node* tail = NULL;
add(&tail, 0);
add(&tail, 1);
add(&tail, 2);
add(&tail, 3);
add(&tail, 4);
add(&tail, 5);
disp(tail);
int x = search(tail, search_val);
if(x==1) {
printf("\nsearch result found");
} else {
printf("\nsearch result not found");
}
deleteNode(&tail, 1);
printf("\n");
disp(tail);
return 0;
}
Thursday, November 25, 2021
Circular Linked List : deleting first position node
Subscribe to:
Post Comments (Atom)
Priority queue deleting elements after ascending inputs Gaand Fadd
using namespace std; #include<iostream> #define N 5 class pq { public: int* arr; int front; int rear; pq() { ...
-
using namespace std; #include<stdio.h> #define N 5 class q{ public: int* arr; int front; int rear; q() { ...
-
#include<stdio.h> int fnCmp(char* str1, char* str2) { while( (*str1 != '\0' && *str2 != '\0') && ...
-
#include<stdio.h> void fncpy(char* str2, char* str1) { int i = 0; while(*str1 != '\0') { str2[i] = *str1; ...
No comments:
Post a Comment