数据结构单链表的操作

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct LNode{
        int data;
        struct LNode *next;
}L,*LNode;
void Print(LNode l)
{
        LNode p = l;
        while(p->next)
        {
                p = p->next;
                printf("%d ", p->data);
        }
        printf("\n");
}
int Length(LNode l)
{
        LNode p = l;
        int cnt = 0;
        while(p->next)
        {
                p = p->next;
                cnt++;
        }
        return cnt;
}
void CreatT(LNode *l,int n)
{
        (*l) = (LNode)malloc(sizeof(L));
        (*l)->next = NULL;
        LNode r = (*l);
        int cnt = 0;
        while(++cnt<=n)
        {
                int tmp;
                scanf("%d", &tmp);
                LNode p = (LNode)malloc(sizeof(L));
                p->next = NULL; 
                p->data = tmp;
                r->next = p;
                r = r->next;
        }
        LNode p = (*l);
}
void CreatH(LNode *l,int n)
{
        (*l) = (LNode)malloc(sizeof(L));
        (*l)->next = NULL;
        int cnt = 0;
        while(++cnt<=n)
        {
                int tmp;
                scanf("%d", &tmp);
                LNode p = (LNode)malloc(sizeof(L));
                p->data = tmp;
                p->next = (*l)->next;
                (*l)->next  = p;
        }
}
int GetElem(L *l,int i)
{
        L *p = l;
        int cnt = 0;
        while(cnt < i && p->next)
        {
                p = p->next;
                cnt++;     
                            
        }
        if(cnt > i ||  !(p))
        {
                printf("输入错误");
                return -1;
        }
        return p->data;
    
} 
int GetData(L *l,int elem)
{
        L *p = l;
        int pos = 0;
        while( p->next->data!=elem && p->next)
        {
                p = p->next;   
                pos++;
                            printf("p->data:%d     ", p->data);
        }
        
        if(!(p))
        {
                printf("输入错误");
                return -1;
        }
        return pos+1;
    
}
int Init(LNode *l,int i,int elem)
{
        LNode tmp = (LNode)malloc(sizeof(L));
        tmp->data = elem;
        int cnt = 0;
        LNode p = (*l);
        if(i>=1&&i<=Length(*l)+1)
        { 
        while(cnt < i-1&&p->next)
        {
                p = p->next;
                cnt++;
        }
        tmp->next = p->next;
        p->next = tmp; 
        return 1;
		}
		else 
		return -1;
     
}
int Delete(LNode *l,int i)
{
        int cnt = 0;
        LNode p = (*l);
        if(i>=1&&i<=Length(*l))
        { 
        while(cnt < i-1&&p->next)
        {
                p = p->next;
                cnt++;
        }
        printf("删除%d\n", p->data);
        LNode tmp = p->next;
        p->next = tmp->next;
     
        return 1;
		}
		else 
		return -1;
     
}
int Uni(LNode A,LNode B,LNode *C)
{
	LNode pa = (A)->next;
	LNode pb = (B)->next;
	(*C) = (LNode)malloc(sizeof(L));
    (*C)->next = NULL;
	LNode pc  = (*C);
	while(pa&&pb)
	{
		LNode c = (LNode)malloc(sizeof(L));
		if(pa->data < pb->data)
		{
			c->data = pa->data;
			c->next = NULL;
			pa = pa->next;
			pc->next = c;
			pc  = pc->next;
			
		}
		else if(pa->data == pb->data)
		{
			c->data = pa->data;
			c->next = NULL;
			pa = pa->next;
			pc->next = c;
			pc  = pc->next;	
			pb = pb->next;
		}	
		else
		{
			c->data = pb->data;
			c->next = NULL;
			pb = pb->next;
			pc->next = c;
			pc  = pc->next;
		}
	}
	pc->next = pa?pa:pb;	

}
LNode l;
LNode A;
LNode B;
LNode C;
int main(){
    printf("1尾插入法构造单链表\n");
    printf("2头插入法构造单链表\n");
    printf("3获取表的长度\n");
    printf("4进行位置查询操作\n");
    printf("5进行数值查询操作\n");
    printf("6进行插入操作\n");
    printf("7进行删除操作\n");
    printf("8进行两个链表的有序合并\n");
    while(1)
    {
    int op;
    scanf("%d", &op);
    if(op == 1)
    {
            printf("请输入你要构造的数组长度\n");
            int tmp;
            scanf("%d", &tmp);
            
            CreatT(&l,tmp);
            Print(l);
    }
    else if(op == 2)
    {
            printf("请输入你要构造的数组长度\n");
            int tmp;
            scanf("%d", &tmp);
            
            CreatH(&l,tmp);
            Print(l);
    }
    else if(op == 3)
    {
            printf("%d", Length(l));
    }
    else if(op == 4)
    {
            printf("请输入你要查询的位置\n");
            int tmp;
            scanf("%d", &tmp);
             printf("%d", GetElem(l,tmp));
    }
    else if(op == 5)
    {
            printf("请输入你要查询的数字");
            int tmp;
            scanf("%d", &tmp);
            printf("%d", GetElem(l,tmp));
     } 
	else if(op == 6)
	{
		printf("请输入你要插入的位置和元素\n");
		int i;
		int elem;
		scanf("%d%d", &i, &elem);
		int t = Init(&l,i,elem);
		if(t== 1)
		{
			printf("插入成功\n");
		}
		else
		{
			printf("插入失败\n");
		}
		Print(l);
	 } 
	 else if(op == 7)
	 {
	 	printf("输入删除的位置\n");
	 	int pos;
	 	scanf("%d", &pos);
	 	int t =  Delete(&l,pos);
	 	if(t == 1)
	 	{
	 		printf("删除成功\n");
		 }
		 else
		 {
		 	printf("删除失败\n");
		 	
		 }
		 Print(l);	  
	 }
	 else if(op == 8)
	 {
	 		printf("请输入你要构造的数组长度\n");
            int tmp;
            scanf("%d", &tmp);
            CreatT(&A,tmp);
            Print(A);
            printf("请输入你要构造的数组长度\n");
            scanf("%d", &tmp);
            CreatT(&B,tmp);
            Print(B);
            Uni(A,B,&C);
            Print(C);
	 	
	 }
	else if(op == 0)
	{
		 
            
		break;
	 } 
	}
	return 0;
}
  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值