#include<stdio.h>
typedef struct node{
int data;
struct node *next;
}list,*nlist;
nlist create() {
nlist head=(nlist)malloc(sizeof(list)), end; // new =(nlist)malloc(sizeof(list))
int x=0;
if (head != NULL) {
printf("请输入第1个数字:");
scanf_s("%d",&x); //注意1:此处必须用&,没有次符号编译的时候不会出错,但是运行时会出错;2:scanf_s接受数据的时候不能用scanf_s("%d\n",&x),不能有\n
head->data = x;
head->next = NULL;
}
end = head;
for (int i = 2; i < 9; i++) {
nlist new = (nlist)malloc(sizeof(list));
printf("请输入第%d个数字:",i);
scanf_s("%d",&x);
new->data = x;
new->next = NULL;
while(end->next !=NULL) {
end=end->next;
}
end->next = new;
end = new;
}
return head;
}
void show(nlist L) {
nlist sh;
sh = (nlist)malloc(sizeof(list));
sh = L;
while (sh != NULL) {
printf("%d\n",sh->data);
sh = sh->next;
}
}
nlist delete(nlist L,int y) {
printf("**************");
nlist p, q;
q = NULL;
p = L;
printf("%d", p->data);
//下面是当结点仅仅一个,且是要删除的数据时,此算法没有用到,因为链表的长度为8
/*if (p->data == y &&p->next == NULL) {
L = NULL;
return L;
}*/
while (p->next!=NULL) {//当不是第一个,也不是最后一个时
//当要删除结点是第一个节点时
if (L->next == p->next&&p->data==y) {
L = L->next;
p = L;
}
for (; p->data != y; p = p->next) {
q = p;
}
q->next=p->next;
p = p->next;
}
//当最后一个结点也是要删除的数据时
if (p->data == y) {
q->next = NULL;
}
return L;
}
void main() {
nlist T,T1;
int y=0;
T=create();
show(T);
printf("请输入要删除的数字:");
scanf_s("%d",&y);
T1=delete(T,y);
show(T1);
//逆转链表
//reverse()
system("pause");
}
typedef struct node{
int data;
struct node *next;
}list,*nlist;
nlist create() {
nlist head=(nlist)malloc(sizeof(list)), end; // new =(nlist)malloc(sizeof(list))
int x=0;
if (head != NULL) {
printf("请输入第1个数字:");
scanf_s("%d",&x); //注意1:此处必须用&,没有次符号编译的时候不会出错,但是运行时会出错;2:scanf_s接受数据的时候不能用scanf_s("%d\n",&x),不能有\n
head->data = x;
head->next = NULL;
}
end = head;
for (int i = 2; i < 9; i++) {
nlist new = (nlist)malloc(sizeof(list));
printf("请输入第%d个数字:",i);
scanf_s("%d",&x);
new->data = x;
new->next = NULL;
while(end->next !=NULL) {
end=end->next;
}
end->next = new;
end = new;
}
return head;
}
void show(nlist L) {
nlist sh;
sh = (nlist)malloc(sizeof(list));
sh = L;
while (sh != NULL) {
printf("%d\n",sh->data);
sh = sh->next;
}
}
nlist delete(nlist L,int y) {
printf("**************");
nlist p, q;
q = NULL;
p = L;
printf("%d", p->data);
//下面是当结点仅仅一个,且是要删除的数据时,此算法没有用到,因为链表的长度为8
/*if (p->data == y &&p->next == NULL) {
L = NULL;
return L;
}*/
while (p->next!=NULL) {//当不是第一个,也不是最后一个时
//当要删除结点是第一个节点时
if (L->next == p->next&&p->data==y) {
L = L->next;
p = L;
}
for (; p->data != y; p = p->next) {
q = p;
}
q->next=p->next;
p = p->next;
}
//当最后一个结点也是要删除的数据时
if (p->data == y) {
q->next = NULL;
}
return L;
}
void main() {
nlist T,T1;
int y=0;
T=create();
show(T);
printf("请输入要删除的数字:");
scanf_s("%d",&y);
T1=delete(T,y);
show(T1);
//逆转链表
//reverse()
system("pause");
}