您的位置:首页 > 理论基础 > 数据结构算法

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

2018-01-04 16:30 274 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: