Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node ListNode;
- struct Node {
- int element;
- ListNode *next;
- };
- void initialize_list (ListNode **head) {
- *head = NULL;
- }
- void add_element (ListNode **head, int element_data) {
- ListNode *new_node;
- new_node = (ListNode *)malloc(sizeof(ListNode));
- new_node -> element = element_data;
- new_node -> next = NULL;
- if (*head == NULL) {
- *head = new_node;
- return;
- }
- ListNode *current;
- current = *head;
- while (current -> next != NULL) {
- current = current -> next;
- }
- current -> next = new_node;
- }
- void delete_element (ListNode **head, int element_data) {
- ListNode *current;
- ListNode *previous;
- previous = *head;
- current = *head;
- while (current != NULL) {
- if (current -> element == element_data) {
- if (previous != current) {
- previous -> next = current -> next;
- }
- else {
- *head = current -> next;
- }
- free(current);
- return;
- }
- previous = current;
- current = current -> next;
- }
- }
- void print_list (ListNode **head) {
- ListNode *current;
- current = *head;
- int i = 0;
- while (current != NULL) {
- printf("Singly_linked_list[%d] == %d \n", i, current -> element);
- ++i;
- current = current -> next;
- }
- printf("\n");
- }
- int main()
- {
- ListNode *head;
- initialize_list(&head);
- add_element(&head, 23);
- add_element(&head, 4);
- add_element(&head, 1);
- print_list(&head);
- delete_element(&head, 1);
- print_list(&head);
- add_element(&head, 7);
- add_element(&head, 2);
- print_list(&head);
- system("PAUSE");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement