插入
插入元素,在已经有的链表里面插入新的内容。
多次插入任意位置
#include<string.h>
#include<windows.h>
#include<stdio.h>
#include<string.h>
struct Student {
char name[10];
struct Student *_next;
};
struct Student *create() {
struct Student *head, *current, *next;
char str[10];
char flag;
printf("请输入名:\n");
scanf("%s", str);
getchar();
head = (struct Student *)malloc( sizeof(struct Student));
strcpy(head->name, str);
current = head;
printf("是否继续输入:\n");
scanf("%c", &flag);
while(flag != 'N') {
printf("请输入名:\n");
scanf("%s", str);
getchar();
next = (struct Student *)malloc( sizeof(struct Student) );
strcpy(next->name, str);
current->_next = next;
current = next;
printf("是否继续输入:\n");
scanf("%c", &flag);
}
current->_next = NULL;
return head;
}
void list (struct Student *p) {
while(1) {
printf("%s \n", p->name);
if(p->_next != NULL) {
p = p->_next;
}else {
break;
}
}
}
int insert (struct Student *p) {
int position;
struct Student *insert, *current;
char str[10];
current = p;
printf("请输入名: \n");
scanf("%s", str);
getchar();
insert = (struct Student *)malloc( sizeof(struct Student) );
strcpy(insert->name, str);
printf("需要插入的位置为: \n");
scanf("%d", &position);
if (position > 0) {
// 插入在position-1的位置
while(position > 1) {
current = current->_next; // 指针指向下一个元素
position--;
}
insert->_next = current->_next; // 插入元素的指针 指向了 当前元素的下一个元素
//(一定要先做指向下一个元素这步步骤1,再让前一个元素指向插入的元素步骤2)
current->_next = insert; //当前元素 指向了 插入元素
}else if (position == 0) {
// 插入第一个元素的前面
insert->_next = current;
p = insert;
}
return p;
}
void main() {
struct Student *p;
p = create();
printf("当前链表元素为: \n");
list(p);
while(1) {
p = insert(p);
printf("插入元素后的链表为: \n");
list(p);
}
Sleep(10000);
}
//(一定要先做指向下一个元素这步步骤1,再让前一个元素指向插入的元素步骤2)
删除
改变链接对象即可:
----------------------链表结构----------------------
数据1 alan 数据1 zjc 数据1 alex
数据2 18 数据2 16 数据2 14
指针 指针 指针
---------------------------------------------------------
改变连接如下:
----------------------链表结构----------------------
数据1 alan 数据1 zjc 数据1 alex
数据2 18 数据2 16 数据2 14
指针 指针 指针
---------------------------------------------------------
绿色部分被删除,新的连接红色表示!!!
下面既可以插入又可以删除
删除->del
#include<string.h>
#include<windows.h>
#include<stdio.h>
#include<string.h>
struct Student {
char name[10];
struct Student *_next;
};
struct Student *create() {
struct Student *head, *current, *next;
char str[10];
char flag;
printf("请输入名:\n");
scanf("%s", str);
getchar();
head = (struct Student *)malloc( sizeof(struct Student));
strcpy(head->name, str);
current = head;
printf("是否继续输入:\n");
scanf("%c", &flag);
while(flag != 'N') {
printf("请输入名:\n");
scanf("%s", str);
getchar();
next = (struct Student *)malloc( sizeof(struct Student) );
strcpy(next->name, str);
current->_next = next;
current = next;
printf("是否继续输入:\n");
scanf("%c", &flag);
}
current->_next = NULL;
return head;
}
void list (struct Student *p) {
while(1) {
printf("%s \n", p->name);
if(p->_next != NULL) {
p = p->_next;
}else {
break;
}
}
}
int insert (struct Student *p) {
int position;
struct Student *insert, *current;
char str[10];
current = p;
printf("请输入插入名: \n");
scanf("%s", str);
getchar();
insert = (struct Student *)malloc( sizeof(struct Student) );
strcpy(insert->name, str);
printf("需要插入的位置为: \n");
scanf("%d", &position);
if (position > 0) {
// 插入在position-1的位置
while(position > 1) {
current = current->_next; // 指针指向下一个元素
position--;
}
insert->_next = current->_next; // 插入元素的指针 指向了 当前元素的下一个元素
current->_next = insert; //当前元素 指向了 插入元素
}else if (position == 0) {
// 插入第一个元素的前面
insert->_next = current;
p = insert;
}
return p;
}
int del (struct Student *p) {
int position;
struct Student *current, *next;
current = p;
printf("需要删除的位置为: \n");
scanf("%d", &position);
if (position > 0) {
// 插入在position-1的位置
while(position > 1) {
current = current->_next; // current 变成其原来指向的下一个元素
position--;
}
next = current->_next; // 下一个元素
current->_next = next->_next;
}else if (position == 0) {
// 插入第一个元素的前面
current = current->_next;
p = current;
}
return p;
}
void main() {
char flag1, flag2;
struct Student *p;
p = create();
printf("当前链表元素为: \n");
list(p);
while(1) {
printf("请插入");
p = insert(p);
printf("请删除");
p = del(p);
printf("修改后的链表为: \n");
list(p);
}
Sleep(10000);
}
上面的升级版本:
每次 插入&删除 前都询问是否插入&删除。
如果执行插入&删除,就输入数字1,不执行就输入别的数字。
#include<string.h>
#include<windows.h>
#include<stdio.h>
#include<string.h>
struct Student {
char name[10];
struct Student *_next;
};
struct Student *create() {
struct Student *head, *current, *next;
char str[10];
char flag;
printf("请输入名:\n");
scanf("%s", str);
getchar();
head = (struct Student *)malloc( sizeof(struct Student));
strcpy(head->name, str);
current = head;
printf("是否继续输入:\n");
scanf("%c", &flag);
while(flag != 'N') {
printf("请输入名:\n");
scanf("%s", str);
getchar();
next = (struct Student *)malloc( sizeof(struct Student) );
strcpy(next->name, str);
current->_next = next;
current = next;
printf("是否继续输入:\n");
scanf("%c", &flag);
}
current->_next = NULL;
return head;
}
void list (struct Student *p) {
while(1) {
printf("%s \n", p->name);
if(p->_next != NULL) {
p = p->_next;
}else {
break;
}
}
}
int insert (struct Student *p) {
int position;
struct Student *insert, *current;
char str[10];
current = p;
printf("请输入插入名: \n");
scanf("%s", str);
getchar();
insert = (struct Student *)malloc( sizeof(struct Student) );
strcpy(insert->name, str);
printf("需要插入的位置为: \n");
scanf("%d", &position);
if (position > 0) {
// 插入在position-1的位置
while(position > 1) {
current = current->_next; // 指针指向下一个元素
position--;
}
insert->_next = current->_next; // 插入元素的指针 指向了 当前元素的下一个元素
current->_next = insert; //当前元素 指向了 插入元素
}else if (position == 0) {
// 插入第一个元素的前面
insert->_next = current;
p = insert;
}
return p;
}
int del (struct Student *p) {
int position;
struct Student *current, *next;
current = p;
printf("需要删除的位置为: \n");
scanf("%d", &position);
if (position > 0) {
// 插入在position-1的位置
while(position > 1) {
current = current->_next; // current 变成其原来指向的下一个元素
position--;
}
next = current->_next; // 下一个元素
current->_next = next->_next;
}else if (position == 0) {
// 插入第一个元素的前面
current = current->_next;
p = current;
}
return p;
}
void main() {
int f1, f2;
struct Student *p;
p = create();
printf("当前链表元素为: \n");
list(p);
while(1) {
printf("是否插入? \n");
scanf("%d", &f1);
if (f1 == 1) {
printf("成功进入插入if语句");
p = insert(p);
}
printf("是否删除? \n");
scanf("%d", &f2);
if (f2 == 1) {
printf("成功进入删除if语句");
p = del(p);
}
printf("修改后的链表为: \n");
list(p);
}
Sleep(10000);
}
如下这种 char f1, f2还没成功,原因可能是我对字符不甚了解
char f1, f2;
struct Student *p;
p = create();
printf("当前链表元素为: \n");
list(p);
while(1) {
printf("是否插入? \n");
scanf("%d", &f1);
if (f1 == 'Y') {
printf("成功进入插入if语句");
p = insert(p);
}
printf("是否删除? \n");
scanf("%d", &f2);
if (f2 == 'Y') {
printf("成功进入删除if语句");
p = del(p);
}
printf("修改后的链表为: \n");
list(p);
}