#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *next;
}LNode,*LinkedList;
//初始化
LinkedList LinkedListInit()
{// init linkedlist
int x;
LinkedList L; //define linkedlist
LNode *p;
L=(LNode*)malloc(sizeof(LNode));
L->next = NULL;
while(scanf("%d",&x)&&x!=EOF)
{ //头插法
p=(LNode*)malloc(sizeof(LNode));
p->data = x;
p->next=L->next;
L->next =p;
}
return L;
}
//清空链表
LinkedList LinkedListClear(LinkedList L)
{//clear the linkedlist
LNode* p,*q;
p=L->next;
while(p!=NULL)
{
q = p->next;
free(p);
p = q;
}
return L;
}
//求链长
int LinkedListLength(LinkedList L)
{ //length of linkedlist
LNode* p;
int j =0;
p = L->next;
while(p)
{
j++;
p = p->next;
}
return j;
}
//去第i个元素
LinkedList LinkedListGet(LinkedList L, int i)
{ //在单链表L中查找第i个元素结点,查找成功返回该结点,否则返回空指针
LNode* p;
int j = 1 ; //计数器初始化
if(j<1){printf("参数错误!/n"); exit(0);}
p = L->next;
while(p!=NULL && j<i)
{
p = p->next; j++;
}
if(j==i) return p;
else return NULL;
}
// 元素定位
int LinkedListLocate(LinkedList L, int x)
{
LNode* p;
int j = 1;
p = L->next;
while(p!=NULL && p->data != x)
{
p = p->next; j++;
}
if(p->data == x) return j ;
else return 0;
}
//盘空单链表
int LinkedListEmpty(LinkedList L)
{
return (L->next==NULL ? 1: 0); //1表示空,0 表示非空
}
//输出单链表
void ListOutput(LinkedList L)
{// Output single-linked list L of the node element values
LNode* p;
char c = 17;
p=L->next;
putchar(c);
while(p)
{
printf("%c%d",26,p->data);
p= p->next;
}
printf("/n");
}
//插入元素
LinkedList LinkedListInsert(LinkedList L,LNode *p, int x)
{//insert e befor node *P
LNode *pre,*s;
pre = L;
while(pre && pre->next!=p)
pre = pre->next;
if(!pre) {printf("不存在*P结点/n");exit(0);}
s= (LNode *)malloc(sizeof(LNode));
s->data = x;
s->next = pre->next;
pre->next = s;
return L;
}
//删除元素
LinkedList LinkedListDel(LinkedList L, int x)
{//delete the first i nodes on the linkedlist L
LNode *pre, * p;
int i=1;
if(x<1){printf("删除位置不正确/n"); exit(0);}
if(L->next == NULL) {printf("空表/n"); exit(0);}
pre = L;
while(pre->next && i<x)
{
pre = pre->next;
i++;
}
if(pre->next == NULL)
{printf("删除位置不正确"); exit(0);}
p = pre->next; //delete node
pre->next = p->next;
free(p);
return L;
}
LNode * LinkedListPrior(LinkedList L, int n)
{
LNode *p,*q;
p =L->next;
q = NULL;
while(p && p->data != n)
{
q = p;
p = p->next;
}
if(p != NULL)
{
if(q == NULL)
printf("该元素的前驱元素为头结点/n");
else
printf("该元素的前驱元素是: %d/n",q->data);
return q;
}
else
{
printf("不存在元素值为%d",n);
printf("的结点/n");
return NULL;
}
}
void converselist(LinkedList L)
{
LNode * p, *p1, *p2;
if(L->next)
{
p1=L->next;
p=p1->next;
p1->next = NULL;
}
while(p)
{
p2 = p;
p = p2->next;
L->next = p2;
p2->next = p1;
p1 = p2;
p2 = p;
}
}
int main(void)
{
LinkedList L,p;
LNode * s;
int n;
L = LinkedListInit(); //init linkedlist
// L=LinkedListClear(L); //cleare the linkedlist
ListOutput(L);
// n = LinkedListLength(L);
// printf("%d/n", n);
// L = LinkedListGet( L, 2);
// p=L->next;
// printf("%d/n",L->date);
// n=LinkedListEmpty( L); //1表示空,0 表示非空
// printf("%d/n",n);
// s = L->next->next->next;
// L=LinkedListInsert(L,s,10);
// L=LinkedListDel(L, 2);
// ListOutput(L);
// s = LinkedListPrior(L, 5);
converselist(L);
ListOutput(L);
return 0;
}