c语言实现单链表的一些基本操作
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
typedef struct Node
{
int data;
struct Node *next;
} Node;
Node *createLinkedList(){
return NULL;
}
Node *insertAtBeginning(Node *head, int data){
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = head;
return newNode;
}
Node *insertAtEnd(Node *head, int data){
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL){
return newNode;
}
Node* current = head;
while(current->next!=NULL){
current = current->next;
}
current->next = newNode;
return head;
}
Node *insertAtPosition(Node* head, int data, int position){
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
if(position==0){
newNode->next = head;
return newNode;
}
Node* current = head;
for(int i =0;i<position-1;i++){
if(current == NULL){
printf("位置超出链表存储范围。 \n");
return head;
}
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
return head;
}
Node *deleteAtBeginning(Node* head){
if(head==NULL){
printf("链表为空\n");
return NULL;
}
Node* temp = head;
head = head->next;
free(temp);
return head;
}
Node *deleteAtEnd(Node* head){
if(head == NULL){
printf("链表为空!!!\n");
return NULL;
}
if(head->next == NULL){
free(head);
return NULL;
}
Node* current = head, *prev;
while(current->next !=NULL){
prev = current;
current = current->next;
}
prev->next = NULL;
free(current);
return head;
}
Node *deleteByValue(Node *head, int data){
if(head == NULL){
printf("链表为空!!!\n");
return NULL;
}
Node *current = head, *prev;
while(current !=NULL&¤t->data != data){
prev = current;
current = current->next;
}
if(current == NULL){
printf("数据不存在!!!\n");
return head;
}
if(current==head){
head = head->next;
}else{
prev->next = current->next;
}
free(current);
return head;
}
void printList(Node *head){
Node* current = head;
printf("链表所有数据:");
while(current!=NULL){
printf("%d ",current->data);
current = current->next;
}
printf("\n");
}
int main() {
Node* head = createLinkedList();
printf("链表已经创建完成,可以正常使用!!!\n");
while(1){
printf("1、添加新的数据到链表头。\n");
printf("2、添加新的数据到链表尾。\n");
printf("3、添加新的数据到固定位置。\n");
printf("4、删除链表头的数据。\n");
printf("5、删除链表尾的数据。\n");
printf("6、删除指定数据。\n");
printf("7、退出程序。\n");
printf("请输入你的选择:");
int ch;
bool flag=false;
scanf("%d",&ch);
system("cls");
switch(ch){
case 1:{
int insertN;
printf("请输入需要插入的数据:");
scanf("%d",&insertN);
head = insertAtBeginning(head, insertN);
break;
}
case 2:{
int insertEndN;
printf("请输入需要插入的数据:");
scanf("%d",&insertEndN);
head = insertAtEnd(head, insertEndN);
break;
}
case 3:{
int insertPosN,pos;
printf("请输入需要插入的数据和位置:");
scanf("%d %d",&insertPosN,&pos);
head = insertAtPosition(head, insertPosN, pos-1);
break;
}
case 4:{
head = deleteAtBeginning(head);
break;
}
case 5:{
head = deleteAtEnd(head);
break;
}
case 6:{
int delN;
printf("请输入需要删除的数据:");
scanf("%d",&delN);
head = deleteByValue(head, delN);
break;
}
case 7:{
printf("程序结束,欢迎使用。\n");exit(0);
}
default:{
printf("您的输入有误,请检查后重新输入!\n");
flag=true;
break;
}
}
if(!flag){
printf("****-----------------------------****\n");
printList(head);
printf("****-----------------------------****\n\n");
}
getchar();
}
return 0;
}