#include <stdio.h>
typedef struct node {
int data;
struct node * next;
}linknode;
linknode * initlink(){ //初始单链表
linknode * head = (linknode *)malloc(sizeof(linknode));
head->next=NULL;
return head;
}
int adddata(linknode * head,int arr[] ,int n) { //表尾增加数据
int i;
linknode * p1=NULL;
linknode * p2=NULL;
if(head==NULL) {
head=initlink();
p1=head;
}
else {
p1=head;
while(p1->next) {
p1=p1->next;
}
}
for(i=0;i<n;++i) {
p2=(linknode *)malloc(sizeof(linknode));
p2->data=arr[i];
p1->next=p2;
p1=p2;
}
p2->next=NULL;
return 1;
}
void print(linknode * head) { //打印数据
linknode * p=NULL;
if(head->next==NULL) {
printf("empty linklist!");
}
p=head->next;
while(p) {
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
}
int delete(linknode * head,int value) { //删除数据
linknode * p1=NULL;
linknode *p2=NULL;
if(head->next==NULL) {
return -1;
}
p1=head;
p2=p1->next;
while (p2 && p2->data!=value) {
p1=p2;
p2=p2->next;
}
if(p2){
p1->next=p2->next;
free(p2);
return 1;
}
else
return -2;
}
int length(linknode *head) { //单链表数据长度
int size=0;
linknode * p=head;
if(head->next==NULL) {
size=0;
}
while(p->next) {
size++;
p=p->next;
}
return size;
}
int reverselist(linknode *head){ //单链表的逆置
int size=0;
size=length(head);
linknode * p=NULL;
linknode * q=NULL;
if(size>1) {
p=head->next;
head->next=NULL;
while(p) {
q=p->next;
p->next=head->next;
head->next=p;
p=q;
}
}
return 1;
}
int main(void) {
int arr[]={1,2,3,4,12};
int i;
linknode * head=initlink();
adddata(head,arr,5);
print(head);
i=delete(head,12);
if(i==1) {
print(head);
printf("%d\n",length(head));
reverselist(head);
print(head);
}
else
printf("delete failed!\n");
return 1;
}
typedef struct node {
int data;
struct node * next;
}linknode;
linknode * initlink(){ //初始单链表
linknode * head = (linknode *)malloc(sizeof(linknode));
head->next=NULL;
return head;
}
int adddata(linknode * head,int arr[] ,int n) { //表尾增加数据
int i;
linknode * p1=NULL;
linknode * p2=NULL;
if(head==NULL) {
head=initlink();
p1=head;
}
else {
p1=head;
while(p1->next) {
p1=p1->next;
}
}
for(i=0;i<n;++i) {
p2=(linknode *)malloc(sizeof(linknode));
p2->data=arr[i];
p1->next=p2;
p1=p2;
}
p2->next=NULL;
return 1;
}
void print(linknode * head) { //打印数据
linknode * p=NULL;
if(head->next==NULL) {
printf("empty linklist!");
}
p=head->next;
while(p) {
printf("%d\t",p->data);
p=p->next;
}
printf("\n");
}
int delete(linknode * head,int value) { //删除数据
linknode * p1=NULL;
linknode *p2=NULL;
if(head->next==NULL) {
return -1;
}
p1=head;
p2=p1->next;
while (p2 && p2->data!=value) {
p1=p2;
p2=p2->next;
}
if(p2){
p1->next=p2->next;
free(p2);
return 1;
}
else
return -2;
}
int length(linknode *head) { //单链表数据长度
int size=0;
linknode * p=head;
if(head->next==NULL) {
size=0;
}
while(p->next) {
size++;
p=p->next;
}
return size;
}
int reverselist(linknode *head){ //单链表的逆置
int size=0;
size=length(head);
linknode * p=NULL;
linknode * q=NULL;
if(size>1) {
p=head->next;
head->next=NULL;
while(p) {
q=p->next;
p->next=head->next;
head->next=p;
p=q;
}
}
return 1;
}
int main(void) {
int arr[]={1,2,3,4,12};
int i;
linknode * head=initlink();
adddata(head,arr,5);
print(head);
i=delete(head,12);
if(i==1) {
print(head);
printf("%d\n",length(head));
reverselist(head);
print(head);
}
else
printf("delete failed!\n");
return 1;
}