Created
March 30, 2025 06:51
-
-
Save in1yan/ea4eaa7224a454203b1a5a239239a0eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <stdio.h> | |
# include <stdlib.h> | |
struct node { | |
char ch; | |
struct node *next; | |
}; | |
void append(struct node **head, char ch); | |
void insert(struct node **head, int pos, char ch); | |
void display_list(struct node *head); | |
int main(){ | |
int n; | |
scanf("%d", &n); | |
struct node *head = NULL; | |
for(int i=0; i<n; i++) | |
{ | |
char ch; | |
scanf(" %c", &ch); | |
append(&head, ch); | |
} | |
/* display_list(head); */ | |
int pos; | |
char new_char; | |
scanf("%d", &pos); | |
scanf(" %c", &new_char); | |
insert(&head, pos, new_char); | |
printf("Updated list: "); | |
display_list(head); | |
} | |
void append(struct node **head, char ch){ | |
struct node *new_node = (struct node *)malloc(sizeof(struct node)); | |
new_node->ch = ch; | |
new_node->next = NULL; | |
if(*head == NULL){ | |
*head=new_node; | |
return; | |
} | |
struct node *tmp = *head; | |
while(tmp->next != NULL){ | |
tmp = tmp->next; | |
} | |
tmp->next = new_node; | |
} | |
void insert(struct node **head, int pos, char ch){ | |
struct node *new_node=(struct node *)malloc(sizeof(struct node)); | |
new_node->ch = ch; | |
new_node->next = NULL; | |
struct node *tmp = *head; | |
for(int i=0; i<pos && tmp!=NULL; i++) | |
tmp = tmp->next; | |
if (tmp==NULL){ | |
printf("Invalid index\n"); | |
return; | |
} | |
new_node->next = tmp->next; | |
tmp->next = new_node; | |
} | |
void display_list(struct node *head){ | |
struct node *tmp = head; | |
while(tmp != NULL) | |
{ | |
printf("%c ", tmp->ch); | |
tmp = tmp->next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment