#include<stdio.h>
#include<stdlib.h>
typedef struct node{
struct node *next;
int data;
}Node;
Node *createlist(){
int size=0;
printf("请问你想创建多大的链表长度?\n");
scanf("%d",&size);
Node *head = (Node*)malloc(sizeof(Node));
Node *node1 = head;
printf("请输入%d个数据\n",size);
for(int i=0;i<size;i++){
Node *node2=(Node*)malloc(sizeof(Node));
node1->next = node2;
scanf("%d",&node2->data);
node1= node2;
}
node1->next =NULL;
printf("创建完毕!\n");
return head;
}
void deletelist(Node *head){
int num = 0,n = 0;
printf("请问你想删除第几个元素?\n");
scanf("%d",&n);
while(head->next !=NULL){
// head = head->next;
if(num == n-1){
head->next = head->next->next;
return ;
}
else{
head = head->next;
num++;
}
}
printf("删除完毕!\n");
}
void printlist(Node* head){
printf("当前链表元素为:\n");
while(head->next != NULL){
head = head->next;
printf("%d\t",head->data);
}
printf("\n");
}
int initgraph(){
int num;
printf("----------------------------\n");
printf("创建链表请按1\n");
printf("删除链表元素请按2\n");
printf("退出请按3\n");
printf("查看当前链表元素请按4\n");
printf("----------------------------\n");
scanf("%d",&num);
return num;
}
int main (){
Node *head=(Node*)malloc(sizeof(Node));
int num=0;
while(num!=3){
num=initgraph();
if(num == 1){
head=createlist();
}
if(num == 2){
deletelist(head);
}
if(num == 4){
printlist(head);
}
}
return 0;
}
链表的创建(C语言)
于 2023-03-27 00:00:19 首次发布