数据结构复习第一天单链表的操作集合

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef int ElemType;
typedef struct LNode
{
    ElemType data;
    struct LNode *next;
}LNode,*LinkList;
LinkList Forward_Insert()
{
    ElemType n,cur;
    LinkList L;
    L=(LNode *)malloc(sizeof(LNode));
    L->next=NULL;
    while(scanf("%d",&cur) != EOF)
    {
        LNode *p;
        p=(LinkList)malloc(sizeof(LNode));
        p->data=cur;
        p->next=L->next;
        L->next=p;
    }
    return L;
}
LinkList Tail_Insert()
{
    LinkList L;
    ElemType cur;
    LNode *tail;
    L=(LNode *)malloc(sizeof(LNode));
    L->next=NULL;
    tail=L;
    while(scanf("%d",&cur) != EOF)
    {
        LNode *p;
        p=(LinkList)malloc(sizeof(LNode));
        p->data=cur;
        tail->next=p;
        tail=p;
    }
    tail->next=NULL;
    return L;
}
void Print_List(LinkList L)
{
    LinkList p;
    p=L;
    while(p->next)
    {
        printf("%d ",p->next->data);
        p=p->next;
    }
    printf("\n");
}
LNode *Find_Position(LinkList L,int i)
{
    int key=0;
    LNode *cur=L;
    while(cur!=NULL&&key<i)
    {
        cur=cur->next;
        key++;
    }
    return cur;
}
void Delete_LNode(LinkList L,int i)
{
    LNode *front,*cur;
    front=Find_Position(L,i-1);
    cur=Find_Position(L,i);
    front->next=cur->next;
    free(cur);
}
void Delete_LNode_By_Value(LinkList L,int i)
{
    LNode *cur,*front;
    front=L;
    cur=L->next;
    while(cur->data!=i)
    {
        front=cur;
        cur=cur->next;
    }
    front->next=cur->next;
    free(cur);
}
void Insert_Tail(LinkList L,int data,int pos)
{
    LNode *temp;
    LNode *new_LNode=(LNode *)malloc(sizeof(LNode));
    temp=Find_Position(L,pos);
    new_LNode->data=data;
    new_LNode->next=temp->next;
    temp->next=new_LNode;
}
LNode *Reverse_List_Loop(LNode *head)
{
    LNode *next;
    LNode *prev=NULL;
    while(head!=NULL)
    {
        next=head->next;
        head->next=prev;
        prev=head;
        head=next;
    }
    return prev;
}
LNode *Reverse_List_Recurrence(LNode *head)
{
    LNode * new_Head;
    if((head==NULL)||(head->next==NULL))
    {
        return head;
    }
    new_Head=Reverse_List_Recurrence(head->next);
    head->next->next=head;
    head->next=NULL;
    return new_Head;
}
int main()
{
    LinkList L;
    LNode *cur;
    L=Tail_Insert();
    Print_List(L);
    //Delete_LNode(L,2);
    //Delete_LNode_By_Value(L,2);
    //Insert_Tail(L,0,3);
    L->next=Reverse_List_Recurrence(L->next);
    Print_List(L);
    return 0;
}
关于单链表的逆序也是看了这个大佬的博客
大家可以去看看
http://blog.csdn.net/autumn20080101/article/details/7607148
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值