具有对链表进行增删改查的功能
#include "stdio.h"
#include "stdlib.h"
struct ListNode{
int val;
struct ListNode *next;
};
void Menu();
struct ListNode *Create(struct ListNode *head);
void Print(struct ListNode *head);
struct ListNode *Head_Add(struct ListNode *head , int target , int insert);
struct ListNode *Tail_Add(struct ListNode *head , int target , int insert);
struct ListNode *Delete(struct ListNode *head , int target);
void Search(struct ListNode *head , int locate);
struct ListNode *Modify(struct ListNode *head , int target , int new_val);
int main(){
int option,target,insert,new_val,locate;
struct ListNode *head = NULL;
while(1){
Menu();
printf("请输入功能序号:");
scanf("%d",&option);
switch(option){
case 1 :
if(head != NULL){
printf("链表已被创建,添加数据请选择对应选项!\n");
break;
}
head = Create(head);
break;
case 2 :
if(head == NULL){
printf("请先创建链表!\n");
break;
}
Print(head);
break;
case 3 :
if(head == NULL){
printf("请先创建链表!\n");
break;
}
printf("请输入被插入的数据:");
scanf("%d",&target);
printf("请输入插入的数据:");
scanf("%d",&insert);
head = Head_Add(head , target , insert);
break;
case 4 :
if(head == NULL){
printf("请先创建链表!\n");
break;
}
printf("请输入被插入的数据:");
scanf("%d",&target);
printf("请输入插入的数据:");
scanf("%d",&insert);
head = Tail_Add(head , target , insert);
break;
case 5 :
if(head == NULL){
printf("请先创建链表!\n");
break;
}
printf("请输入删除的数据:");
scanf("%d",&target);
head = Delete(head , target);
break;
case 6 :
if(head == NULL){
printf("请先创建链表!\n");
break;
}
printf("请输入查找数据的位置:");
scanf("%d",&locate);
Search(head , locate);
break;
case 7 :
if(head == NULL){
printf("请先创建链表!\n");
break;
}
printf("请输入需要修改的数据:");
scanf("%d",&target);
printf("请输入修改后的数据:");
scanf("%d",&new_val);
head = Modify(head , target , new_val);
break;
case 8 :
printf("退出成功!\n");
return 0;
default :
printf("请输入正确的序号!\n");
break;
}
}
}
void Menu() {
printf("-------------------------链表功能-------------------------\n");
printf(" ①创建链表\n");
printf(" ②打印链表\n");
printf(" ③头插法\n");
printf(" ④尾插法\n");
printf(" ⑤删除数据\n");
printf(" ⑥查找数据\n");
printf(" ⑦修改数据\n");
printf(" ⑧退出\n");
printf("--------------------------------------------------------\n");
}
struct ListNode *Create(struct ListNode *head){
struct ListNode *p,*q;
q = NULL;
int count,n;
count = 0;
printf("请输入链表的数据(输入-1停止录入):");
while(1){
scanf("%d",&n);
if(n == -1) break;
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->val = n;
if(count == 0){
head = p;
count++;
}
else q->next = p;
q = p;
}
if(q != NULL) q->next = NULL;
printf("创建成功!\n");
return head;
}
void Print(struct ListNode *head){
printf("已存储链表为:");
while(head != NULL){
printf("%d ",head->val);
head = head->next;
}
printf("\n");
}
struct ListNode *Head_Add(struct ListNode *head , int target , int insert){
struct ListNode *pre,*cur,*new_list;
cur = head;
while(cur != NULL && cur->val != target){
pre = cur;
cur = cur->next;
}
if(cur == NULL){
printf("查无此数!\n");
return head;
}
new_list = (struct ListNode *)malloc(sizeof(struct ListNode));
new_list->val = insert;
pre->next = new_list;
new_list->next = cur;
printf("添加成功!\n");
return head;
}
struct ListNode *Tail_Add(struct ListNode *head , int target , int insert){
struct ListNode *h,*new_list,*temp;
h = head;
while(h != NULL && h->val != target) h = h->next;
if(h == NULL){
printf("查无此数!\n");
return head;
}
temp = h->next;
new_list = (struct ListNode *)malloc(sizeof(struct ListNode));
new_list->val = insert;
h->next = new_list;
new_list->next = temp;
printf("添加成功!\n");
return head;
}
struct ListNode *Delete(struct ListNode *head , int target){
struct ListNode *cur,*pre,*temp;
cur = head;
while(cur != NULL && cur->val != target){
pre = cur;
cur = cur->next;
}
if(cur == NULL){
printf("查无此数!\n");
return head;
}
temp = cur->next;
pre->next = temp;
printf("删除成功!\n");
return head;
}
void Search(struct ListNode *head , int locate){
int count = 0;
while(head != NULL){
count++;
if(count == locate){
printf("%d位的数据为:%d\n",locate,head->val);
break;
}
head = head->next;
}
if(head == NULL) printf("查无此数!\n");
}
struct ListNode *Modify(struct ListNode *head , int target , int new_val){
struct ListNode *cur;
cur = head;
while(cur != NULL && cur->val != target) cur = cur->next;
if(cur == NULL){
printf("查无此数!\n");
return head;
}
while(cur != NULL){
if(cur->val == target) cur->val = new_val;
cur = cur->next;
}
printf("修改成功!\n");
return head;
}