1 solutions

  • 0
    @ 2024-12-10 22:03:04

    C :

    #include<stdio.h>
    #include<stdlib.h>
    
    #define OK 1
    #define ERROR 0
    
    typedef int Status;
    typedef int ElemType;   
    typedef struct ListNode
    {
    	ElemType *data;
    	struct ListNode *next;
    }ListNode;
    typedef struct LinkList
    {
    	ListNode *head;
    	int length;
    }LinkList;
    
    void Create(LinkList *L,int n)
    {
    	ListNode *newnode;
    	int i;
    	L->head=(ListNode *)malloc(sizeof(ListNode));
    	L->head->next=NULL;
    	for(i=0;i<n;i++)
    	{
    		newnode=(ListNode *)malloc(sizeof(ListNode));
    		scanf("%d",&newnode->data);
    		newnode->next=L->head->next;
    		L->head->next=newnode;	
    	}
    }
    Status Reverse(LinkList *L,int n)
    {
    	ListNode *p,*q;
    	int i;
    	if(!L)
    		return ERROR;
    	p=L->head->next;
    	for(i=0;i<n-1;i++)
    	{
    		q=p->next;
    		p->next=q->next;
    		q->next=L->head->next;
    		L->head->next=q;
    
    	}
    	return OK;
    }
    void Delete(LinkList L)
    {
    	ListNode *p=L.head->next;
    	while(p!=NULL&&p->next!=NULL)
    	{
    		if (p->data==p->next->data)
    			p->next=p->next->next;
    		else p= p->next;
    	}
    
    }
    void Show(LinkList L)
    {
    	ListNode *p=L.head->next;
    	while(p)
    	{
    		printf("%d",p->data);
    	if(p)
    		printf(" ");
    	p=p->next;
    	}
    	printf("\n");
    }
    
    int main()
    {
    	int n;
    	LinkList L;
    	while(scanf("%d",&n)!=EOF)
    	{
    		if(n==0)
    		{
    			printf("list is empty\n");
    			continue;
    		}
    		Create(&L,n);
    		Reverse(&L,n);
    		Show(L);
    		Delete(L);
    		Show(L);
    	}
    	return 0;
    }
    

    C++ :

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    #define OK 1
    #define FAIL 0
    
    struct LinkNode{
        int data;
        LinkNode* next;
    };
    
    class LinkList{
    private:
        LinkNode* head;
        LinkNode* tail;
        int len;
    public:
        int getLen()const{
            return this->len;
        }
        LinkList(){
            this->head = new LinkNode();
            this->head->next = NULL;
            this->tail = NULL;
            this->len = 0;
        }
        // 前插法插入节点
        void insertHead(int data){
            LinkNode* node = new LinkNode();
            node->data = data;
            node->next = this->head->next;
            head->next = node;
            if(node->next == NULL){
                this->tail = node;
            }
            this->len++;
        }
    // 后插法插入节点
        void insertTail(int data){
            LinkNode* node = new LinkNode();
            node->data = data;
            node->next = NULL;
            if(this->tail == NULL){
                this->tail = node;
                this->head->next = node;
            }else{
                this->tail->next = node;
                this->tail = node;
            }
    
            this->len++;
        }
    
        void print()const{
            if(this->len == 0){
                cout << "list is empty" << endl;
                return;
            }
            LinkNode* node = head->next;
            while(node != NULL){
                cout << node->data << " ";
                node = node->next;
            }
            cout << endl;
        }
    };
    
    int main(){
        int n;
        while(cin >> n){
            LinkList list;
            int test;
            for(int i=0;i<n;i++){
                int temp;
                cin >> temp;
                cout << temp << " ";
                if(i == 0 || test != temp){
                    test = temp;
                    list.insertTail(temp);
                }
            }
            if(n != 0){
                cout << endl;
            }
            list.print();
        }
    
        return 0;
    }
    
    
    • 1

    Information

    ID
    521
    Time
    30000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By