数据结构作业(链表)

/*
1合并两个递增序列成为递增序列
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void mergelink(linklist &a,linklist &b,linklist &c)
{
    node *pa,*pb,*pc;
	pa = a -> next; pb = b -> next;
	c = a;
	pc = c;
	while(pa && pb)
	{
		if(pa -> data < pb -> data)
		{
			pc -> next = pa;
			pc = pa;
			pa = pa -> next;
		}
		else if(pa -> data   == pb -> data)
		{
			pc -> next = pa;
			pc = pa;
			pa = pa -> next;
			pb = pb -> next;
	    }
	    else
	    {
	    	pc -> next = pb;
	    	pc = pb;
	    	pb = pb -> next;
		}
	}
	pc -> next = pa ? pa : pb;
	delete b;
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{

    ll n;
    linklist a,b,c;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cout << "请你输出你想创建第二个链表的长度"  << endl;
    cin >> n;
    creatlink(b,n);

    mergelink(a,b,c);

    cout << "这是合并后的链表" << endl;
    display(c);

    return 0;
}
/*
2合并序列让它变成一个递减序列
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void mergelink(linklist &a,linklist &b,linklist &c)
{
    node *pa,*pb,*pc,*p;
    pa = a -> next; pb = b -> next;
    c = a;
    pc = c;
    c -> next = NULL;
    while(pa && pb)
    {
        if(pa -> data <= pb -> data)
        {
            p = new node;
            p -> data = pa -> data;
            p -> next = pc -> next;
            pc -> next = p;
            pa = pa -> next;
        }
        else
        {
            p = new node;
            p -> data = pb -> data;
            p -> next = pc -> next;
            pc -> next = p;
            pb = pb -> next;
        }
    }

    while(pa)
    {
        p = new node;
        p -> data = pa -> data;
        p -> next = pc -> next;
        pc -> next = p;
        pa = pa -> next;
    }

    while(pb)
    {
        p = new node;
        p -> data = pb -> data;
        p -> next = pc -> next;
        pc -> next = p;
        pb = pb -> next;
    }

}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{

    ll n;
    linklist a,b,c;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cout << "请你输出你想创建第二个链表的长度"  << endl;
    cin >> n;
    creatlink(b,n);

    mergelink(a,b,c);

    cout << "这是合并后的链表" << endl;
    display(c);

    return 0;
}
/*
3求交集
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void mergecross(linklist &a,linklist &b,linklist &c)
{
    node *pa,*pb,*pc;
    pa = a -> next;
    pb = b -> next;
    c = a;
    pc = c;
    c -> next = NULL;
    while(pa)
    {
        ll flag = 0;
        while(pb)
        {
            if(pa -> data == pb -> data)
            {
                flag = 1;
                break;
            }
            else
            {
                pb = pb -> next;
            }
        }
        if(flag == 1)
        {
            pc -> next = pa;
            pc = pa;
        }
        pa = pa -> next;
        pb = b -> next;
    }
    pc -> next = NULL;
    delete b;
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{

    ll n;
    linklist a,b,c;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cout << "请你输出你想创建第二个链表的长度"  << endl;
    cin >> n;
    creatlink(b,n);

    mergecross(a,b,c);

    cout << "这是两个序列的交集" << endl;
    display(c);

    return 0;
}

/*
4求差集
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void mergeds(linklist &a,linklist &b,linklist &c)
{
    ll sum = 0;
    node *pa,*pb,*pc;
    pa = a -> next; pb = b -> next;
    c = a;
    pc = c;
    while(pa)
    {
        ll flag = 0;
        while(pb)
        {
            if(pa -> data == pb -> data)
            {
                flag = 1;
                pb = pb -> next;
                break;
            }
            else
            {
                pb = pb -> next;
            }
        }
        if(flag == 0)
        {
            pc -> next = pa;
            pc = pa;
            sum ++;
        }
        pa = pa -> next;
        pb = b -> next;

    }
    pc -> next = NULL;
    delete b;
    cout << sum << endl;
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{

    ll n;
    linklist a,b,c;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cout << "请你输出你想创建第二个链表的长度"  << endl;
    cin >> n;
    creatlink(b,n);

    cout << "这是两个序列的差集的个数" << endl;
    mergeds(a,b,c);

    cout << "这是两个序列的差集" << endl;
    display(c);

    return 0;
}

/*
5分割
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void cutlink(linklist &a,linklist &b,linklist &c)
{
    node *pa,*pb,*pc;
    pa = a -> next;
    b = new node;
    c = new node;
    pb = b;
    pc = c;
    while(pa)
    {
        if(pa -> data > 0)
    	{
			node *p;
			p = new node;
			p -> data = pa->data;
			p -> next = NULL;
			pb -> next = p;
			pb = p;
    		pa = pa -> next;
		}
		else if(pa -> data < 0)
		{
            node *p;
			p = new node;
			p -> data = pa->data;
			p -> next = NULL;
			pc -> next = p;
			pc = p;
			pa = pa -> next;
		}
		else
        {
            pa = pa -> next;
        }
    }
    pb -> next = NULL;
    pc -> next = NULL;
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{
    ll n;
    linklist a,b,c;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cutlink(a,b,c);

    cout << "这是分割后第一个序列" << endl;
    display(b);

    cout << "这是分割后第二个序列" << endl;
    display(c);

    return 0;
}

/*
6找最大值
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void findlink(linklist &a)
{
    ll ma = -99999;
    node *pa;
    pa = a -> next;
    while(pa)
    {
        ma = max(pa->data,ma);
        pa = pa -> next;
    }
    cout << ma << endl;
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{
    ll n;
    linklist a;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cout << "这是最大节点" << endl;
    findlink(a);

    return 0;
}

/*
7反转
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void reverselink(linklist &a,linklist &b)
{
    node *pa,*pb;
    pa = a -> next -> next  ;
	b = a;
	pb = b -> next ;
	pb -> next = NULL;
	while(pa)
	{
	    node *p;
	    p=new node;
		p -> data = pa -> data;
		p -> next = pb ;
		b -> next = p;
		pb = p;
		pa = pa -> next ;
	}
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{
    ll n;
    linklist a,b;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    reverselink(a,b);
    cout << "反转" << endl;
    display(a);

    return 0;
}

/*
8删除节点
*/
#include 
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void areaselink(linklist &a,linklist &b,ll c,ll d)
{
    node *pa,*pb;
    pa = a -> next ;
	b = a;
	pb = b;
	while(pa)
	{
		if(pa -> data > c && pa -> data < d)
		{
			pb -> next = pa -> next;
			pa = pa -> next ;

		}
		else
		{
			pb -> next = pa;
			pb = pa;
			pa = pa -> next ;

		}
	}
 }

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

int main()
{
    ll n,c,d;
    linklist a,b;
    cout << "请你输出你想创建第一个链表的长度"  << endl;
    cin >> n;
    creatlink(a,n);

    cout << "请你输出你想删除点的范围"  << endl;
    cin >> c >> d;

    areaselink(a,b,c,d);
    cout << "删除后的链表" << endl;
    display(a);

    return 0;
}

/*
9交换
*/
#include 
#define null NULL
#define maxn 1551

typedef long long ll;

using namespace std;

typedef struct node
{
    ll data;
    node *next;
}node,*linklist;

typedef struct dunode
{
	ll data;
	dunode *prior;
	dunode *next;
}dunode,*dulinklist;

void creatlink(linklist &l,ll n)
{
    node *p,*r;
    l = new node;
    l -> next = NULL;
    r = l;
    for(int i = 1; i <= n; i ++)
    {
        p = new node;
        cin >> p -> data;
        p -> next = NULL;
        r -> next = p;
        r = p;
    }
}

void creatlinkdu(dulinklist &a,ll n)
{
	a = new dunode;
	a -> next = NULL;
	dulinklist r;
	dunode *p;
	r=a;
	for(int i = 1; i <= n; i ++)
	{
		p = new dunode;
		cin >> p -> data ;
		p -> next = NULL;
		p -> prior = r;
		r -> next = p;
		r = p;
	}
}

 void changelink(dulinklist &a,ll c)
 {
 	dunode *p;
    dunode *pa;
    pa = a -> next;
    while(pa -> data != c)
        pa = pa -> next;
    p = pa -> prior -> prior;
    pa -> prior -> next = pa -> next;
    if(pa -> next != null)
    pa -> next -> prior = pa -> prior;
    pa -> next = p -> next;
    pa -> prior = p;
    p -> next -> prior = pa;
    p -> next = pa;
}

void display(linklist c)
{
    node *p;
    p = c -> next;
    while(p)
    {
        cout << p -> data << " ";
        p = p -> next;
    }
    cout << endl;
}

void displaydu(dulinklist l)
{
    dunode *p = l -> next;
	while(p)
	{
		cout << p -> data << " ";
		p = p -> next;
	}
	cout<> n;
    creatlinkdu(a,n);

    cout << "请你输出你要交换的后一个点的值"  << endl;
    cin >> c;

    changelink(a,c);
    cout << "交换后的链表" << endl;
    displaydu(a);

    return 0;
}

/*
10删除特定的值
*/
#include 

#define maxn 100005

using namespace std;

typedef struct
{
    int *elem;
    int length;
}SqList;

void deletelink(SqList L,int d)
{
	int p,i;
	p = 0;
	for(i = 0; i < L.length; i ++)
	{
	    if(L.elem[i] != d)
		  L.elem[p++] = L.elem[i];
	}

	for(i = 0; i < p; i ++)
	{
		cout << L.elem [i] << " ";
	}
	cout<>len;
    cout<<"请输入单链表的值";

    for(i = 0; i < len; i ++)
    {
		int x;
        cin >> a.elem[i];
    }
	a.length = len;

    cout<<"请输入需要删除的值" ;
    int k;
    cin >> k;

    deletelink(a,k);

    return 0;
}



 

你可能感兴趣的:(数据结构,静态链表)