单链表的程序实现

#include<stdio.h>
//#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
    int data;
    struct Node * pnext;
}NODE, * PNODE;
PNODE create_list()
{
    PNODE phead=(PNODE)malloc(sizeof(NODE));
    if(phead==NULL)
    {
        printf("内存分配失败,程序终止。\n");
        exit(-1);
    }

    int val,len,i;
    printf("请输入您要建立的链表的长度:");
    scanf("%d",&len);
    PNODE ptail;
    ptail=phead;
    for(i=0;i<len;i++)
    {
        PNODE pnew=(PNODE)malloc(sizeof(NODE));
        if(pnew==NULL)
        {
            printf("内存分配失败,程序终止。\n");
            exit(-1);
        }
        printf("请输入第%d个节点的值:",i+1);
        scanf("%d",&val);
        pnew->data =val;
        ptail->pnext =pnew;
        pnew->pnext =NULL;
        ptail=pnew;
    }
    return phead;
}
int length(PNODE phead)
{
    PNODE p=phead->pnext;
    int n=0;
    while(p)
    {
        n++;
        p=p->pnext;
    }
    return n;
}
void traverse_list(PNODE phead,int len)
{
    int i;
    PNODE p=phead->pnext;
    for(i=0;i<len;i++)
    {
        printf("%d ",p->data);
        p=p->pnext ;
    }
    printf("\n");
}
int main(void)
{
    PNODE phead;
    int len;
    phead=create_list();
    len=length(phead);
    printf("链表的长度是%d\n",len);
    traverse_list(phead,len);
    return 0;
}
#include<iostream> using namespace std; enum yaya { error,success }; struct node { int data; node*next; }; class List{ public: List(); ~List(); int length(); yaya set_data(int n); node* get_ptr(int m); yaya inset_node(int x,int y); int delete_node(int i); void put_in(int x); void breaktwo(List&la,List&lb); void same_data(List&l1,List&l2,List&l); int out_data(); private: int count; node* head; }; List::List(){ head=new node; head->next=NULL; count=0; } int List::length(){ return count; } yaya List::set_data(int n){ node*p=head; for(int j=0;j<n;j++){ cin>>p->data; p=p->next; count++; if(p==NULL){ return error; } } return success; } node* List::get_ptr(int m){ node*p=head; int j=0; while(p!=NULL&&j<m){ j++; p=p->next; } return p; } yaya List::inset_node(int x,int y){ if(x<=0||x>count+1){ return error; } node*p=new node; p->data=y; node*current=head; for(int j=1;j<x;j++){ current=current->next; } p->next=current->next; current->next=p; count++; return success; } int List::delete_node(int i){ if(i<=0||i>count) return NULL; node*current=head; for(int j=1;j<i;j++){ current=current->next; }node*u=current->next; current->next=u->next; delete u; count--; return success; } void List::put_in(int x){ node*p=new node; p->data=x; node*current=head; for(;current!=NULL&&current->next!=NULL&&current->next->data<x;){ current=current->next; } p->next=current->next; current->next=p; count++; return; } void List::breaktwo(List& la,List& lb){ node*q=head->next; la.inset_node(la.count+1,q->data); while(q!=NULL&&q->next!=NULL&&q->next->next!=NULL){ q=q->next->next; la.inset_node(la.count+1,q->data); } node*w=head->next->next; lb.inset_node(lb.count+1,w->data); while(w!=NULL&&w->next!=NULL&&w->next->next!=NULL){ w=w->next->next; lb.inset_node(lb.count+1,w->data); } } void List::same_data(List&l1,List&l2,List&l){ node*p=l1.head; for(int i=1;i<=l1.count;i++){ p=p->next; node*q=l2.head; for(int j=1;j<=l2.count;j++){ q=q->next; if(p->data==q->data) l.inset_node(l.length()+1,p->data); } } } int List::out_data(){ node*p=head; for(int j=1;j<=count;j++){ p=p->next; cout<<(p->data)<<" "; } cout<<endl; return 0; } List::~List(){ node*p=head,*s; while(p!=NULL){ s=p; p=p->next; delete s; } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值