#include<stdio.h>
#include<malloc.h>
typedef struct Node
{
int data;
struct Node
*next;
}LinkList;
//就地反转
int LinkListRerverse(LinkList *head){
LinkList *q,*p;
p =
head->next;
head->next = NULL;
while(p != NULL){
q =
p->next;
p->next = head->next;
head->next = p;
p =
q;
}
return 0;
}
//输出数据
int LinkListPrintf(LinkList
*head){
LinkList *p;
p = head;
while(p->next){
p =
p->next;
printf("%d ",p->data);
}
printf("\n");
return
0;
}
//创建链表
int LinkListCreate(LinkList *head){
LinkList
*p,*newNode;
head->next = NULL;
p = head;
int i;
for(i = 0;i
< 11;i++){
//创建节点
newNode =
(LinkList*)malloc(sizeof(LinkList));
newNode->data =
i;
newNode->next = p->next;
p->next = newNode;
p =
newNode;
}
return 0;
}
//插入节点
int ListInsert(LinkList *L,int
i,int e)
{
LinkList *p, *s;
int j=0;
p=L;
if(i<1&&i>L->data)
return (-1);
while(--i)
p=p->next;
s=(LinkList*)malloc(sizeof(LinkList));
s->data=e;
s->next=p->next;
p->next=s;
return 0;
}
//删除节点
int ListDelete(LinkList *L,int i)
{
LinkList *p, *q;
p=L;
if(i<1&&i>L->data)
return (-1);
while(--i) p=p->next;
q=p->next;
p->next=q->next;
free(q);
return 0;
}
int main()
{
LinkList *head;
head =
(LinkList*)malloc(sizeof(LinkList));
//创建链表(n个节点)
LinkListCreate(head);
ListInsert(head,
4, 4);
//输出已建链表
LinkListPrintf(head);
ListDelete(head,
4);
LinkListPrintf(head);
LinkListRerverse(head);
ListInsert(head, 4, 4);
LinkListPrintf(head);
return
0;
}
原文地址:http://www.cnblogs.com/heyp/p/3767498.html