单链表基本操作C语言代码
时间: 2023-09-21 12:03:34 浏览: 101
当然,我可以为您提供单链表基本操作的C语言代码。请看下面的代码:
```
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *create_list(int n) {
struct node *head = NULL, *p = NULL, *prev = NULL;
int i = 0;
for (i = 0; i < n; i++) {
p = (struct node*)malloc(sizeof(struct node));
printf("Enter data for node %d: ", i+1);
scanf("%d", &p->data);
if (head == NULL) {
head = p;
}
else {
prev->next = p;
}
prev = p;
}
if (head != NULL) {
prev->next = NULL;
}
return head;
}
void display(struct node *head) {
struct node *p = head;
while (p != NULL) {
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n");
}
void search(struct node *head, int key) {
struct node *p = head;
int i = 0;
while (p != NULL && p->data != key) {
i++;
p = p->next;
}
if (p == NULL) {
printf("%d not found in the list.\n", key);
}
else {
printf("%d found at position %d.\n", key, i+1);
}
}
void insert_beg(struct node **head, int data) {
struct node *p = (struct node*)malloc(sizeof(struct node));
p->data = data;
p->next = *head;
*head = p;
}
void insert_end(struct node **head, int data) {
struct node *p = (struct node*)malloc(sizeof(struct node));
struct node *last = *head;
p->data = data;
p->next = NULL;
if (*head == NULL) {
*head = p;
return;
}
while (last->next != NULL) {
last = last->next;
}
last->next = p;
}
void insert_after(struct node *head, int key, int data) {
struct node *p = head;
while (p != NULL && p->data != key) {
p = p->next;
}
if (p == NULL) {
printf("%d not found in the list.\n", key);
return;
}
struct node *new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = p->next;
p->next = new_node;
}
void delete_beg(struct node **head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *p = *head;
*head = (*head)->next;
printf("Deleted node with data = %d.\n", p->data);
free(p);
}
void delete_end(struct node **head) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *p = *head;
struct node *prev = NULL;
while (p->next != NULL) {
prev = p;
p = p->next;
}
if (prev == NULL) {
*head = NULL;
}
else {
prev->next = NULL;
}
printf("Deleted node with data = %d.\n", p->data);
free(p);
}
void delete_node(struct node **head, int key) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct node *p = *head;
struct node *prev = NULL;
while (p != NULL && p->data != key) {
prev = p;
p = p->next;
}
if (p == NULL) {
printf("%d not found in the list.\n", key);
return;
}
if (prev == NULL) {
*head = p->next;
}
else {
prev->next = p->next;
}
printf("Deleted node with data = %d.\n", p->data);
free(p);
}
int main() {
struct node *head = NULL;
int n = 0, key = 0, data = 0, choice = 0;
while (1) {
printf("1. Create a list.\n");
printf("2. Display the list.\n");
printf("3. Search an element.\n");
printf("4. Insert at the beginning.\n");
printf("5. Insert at the end.\n");
printf("6. Insert after a node.\n");
printf("7. Delete from the beginning.\n");
printf("8. Delete from the end.\n");
printf("9. Delete a node.\n");
printf("10. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the number of nodes: ");
scanf("%d", &n);
head = create_list(n);
break;
case 2:
display(head);
break;
case 3:
printf("Enter the element to be searched: ");
scanf("%d", &key);
search(head, key);
break;
case 4:
printf("Enter the data to be inserted at the beginning: ");
scanf("%d", &data);
insert_beg(&head, data);
break;
case 5:
printf("Enter the data to be inserted at the end: ");
scanf("%d", &data);
insert_end(&head, data);
break;
case 6:
printf("Enter the data of the node after which you want to insert: ");
scanf("%d", &key);
printf("Enter the data to be inserted: ");
scanf("%d", &data);
insert_after(head, key, data);
break;
case 7:
delete_beg(&head);
break;
case 8:
delete_end(&head);
break;
case 9:
printf("Enter the data of the node to be deleted: ");
scanf("%d", &key);
delete_node(&head, key);
break;
case 10:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;
}
```
希望这段代码可以帮到您!
阅读全文