#include <stdio.h>
#include <stdlib.h>
struct Test
{
int data;
struct Test *next;
};
void printLink(struct Test *head)
{
struct Test *p;
p=head;
while(p!=NULL){
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
int serchData(struct Test* p,int data) //查
{
while(p != NULL){
if(p->data == data){
return 1;
}
p = p->next;
}
return 0;
}
struct Test* changeData(struct Test* head,int data,int newdata) //改
{
struct Test *p = head;
while(p != NULL){
if(p->data == data){
p->data = newdata;
}
p = p->next;
}
return head;
}
struct Test* inserthead(struct Test* head,int data,struct Test* new1) //在前面插入一个节点
{
struct Test* p = head;
if(p->data == data){
new1->next = head;
return new1;
}
while(p->next!=NULL){
if(p->data == data){
new1->next = p->next;
p->next = new1;
}
p=p->next;
}
return head;
}
struct Test* insertbehind(struct Test* head,int data,struct Test* new1) //在后面插入一个节点
{
struct Test* p = head;
while(p!=NULL){
if(p->data == data){
new1->next = p->next;
p->next = new1;
}
p = p->next;
}
return head;
}
struct Test* insertdelet(struct Test* head,int data) //删除指定节点
{
struct Test* p = head;
if(p->data == data){
head = head->next;
return head;
}
while(p->next!=NULL){
if(p->next->data == data){
p->next = p->next->next;
return head;
}
p = p->next;
}
return head;
}
struct Test* insertBehind(struct Test *head,struct Test *new) //尾插法,1,2,3, 123
{
struct Test *p = head;
if(p == NULL){
head = new;
return head;
}
while(p->next != NULL){
p = p->next;
}
p->next = new;
return head;
}
struct Test *creatLink2(struct Test *head) //创建链表
{
struct Test *new;
while(1){
new = (struct Test *)malloc(sizeof(struct Test));
printf("input your new node data:\n");
scanf("%d",&(new->data));
new->next = NULL;
if(new->data == 0){
printf("0 quit\n");
return head;
}
head = insertBehind(head,new);
}
}
struct Test* insertHead(struct Test *head,struct Test *new) //头插法功能
{
if(head == NULL){
head = new;
}else{
new->next = head;
head = new;
}
return head;
}
struct Test* creatLink1(struct Test *head) //创建链表
{
struct Test *new;
while(1){
new = (struct Test *)malloc(sizeof(struct Test));
printf("input your new node data:\n");
scanf("%d",&(new->data));
new->next = NULL;
if(new->data == 0){
printf("0 quit\n");
return head;
}
head=insertHead(head,new);
}
}
int main()
{
struct Test *head = NULL;
head = creatLink1(head);
printLink(head);
/*int i;
int ret =0;
int len;
struct Test t1 = {1,NULL};
struct Test t2 = {2,NULL};
struct Test t3 = {3,NULL};
struct Test t4 = {4,NULL};
struct Test t5 = {5,NULL};
t1.next = &t2;
t2.next = &t3;
t3.next = &t4;
t4.next = &t5;
//len = sizeof(struct Test);
struct Test *head = NULL;
head = &t1;
printLink(&t1);
//head = changeData(&t1,3,66);
//printLink(head);
struct Test new1 = {999,NULL};
head = inserthead(&t1,2,&new1);
printLink(head);
struct Test new2 = {777,NULL};
head = insertbehind(&t1,4,&new2);
printLink(head);
head = insertdelet(&t1,5);
printLink(head);
ret=serchData(&t1,3);
if(ret =1){
printf("恭喜你找到了\n");
}else{
printf("没有这个数\n");
}
*/
return 0;
}