1 solutions

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

    C :

    #include<stdio.h>
    #include<malloc.h>
    typedef struct Node
    {
       int data;
       struct Node *next;
    }Node,*PNODE;
    
    PNODE creat_list(int len)
    {
       int i;
       int val;
       PNODE pHead=(PNODE)malloc(sizeof(Node));
       pHead->next=NULL;
       PNODE pTail=pHead;
       pTail->next=NULL;
       for(i=0;i<len;i++)
       {
           scanf("%d",&val);
           PNODE pNew=(PNODE)malloc(sizeof(Node));
           pNew->data=val;
           pTail->next=pNew;
           pNew->next=NULL;
           pTail=pNew; 
       }
       return pHead; 
    }
    void traverse_list(PNODE pHead)
    {
         PNODE p=pHead->next;
         if(p)
         {
             while(p!=NULL)
              {
                printf("%d ",p->data);
                p=p->next;
             }
             printf("\n");
         }
         else
         ;
    }
    
    PNODE InversionNode(PNODE i)
    {
        PNODE r,p,h;
        h=(PNODE)malloc(sizeof(Node));
        h->next = NULL;
        r=i->next;
        while(r)
        {
              p=r;
              r = r->next;
              p->next=h->next;
              h->next = p;  
        }
        return h;
    }
    int main()
    {
        int k;
        while(scanf("%d",&k)!=EOF)
        { 
            if(k==0)
            {
                    printf("list is empty\n");
                    continue;
            }
            PNODE pHead=NULL;
            pHead=creat_list(k);
            traverse_list(pHead);
            pHead=InversionNode(pHead);
            traverse_list(pHead); 
        }
        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 print()const{
            if(this->len == 0){
                cout << "list is empty" << endl;
            }
            LinkNode* node = head->next;
            while(node != NULL){
                cout << node->data << " ";
                node = node->next;
            }
            cout << endl;
        }
    };
    
    int main(){
        int n;
        while(cin >> n){
            LinkList list;
            for(int i=0;i<n;i++){
                int temp;
                cin >> temp;
                cout << temp << " ";
                list.insertHead(temp);
            }
            if(n != 0){
                cout << endl;
            }
            list.print();
        }
    
        return 0;
    }
    
    

    Java :

    
    
    import java.util.Scanner;
    
    public class Main {
    public static void main(String[] args) {
    	Scanner in = new Scanner(System.in);
    	while(in.hasNextInt())
    	{
    		int n =in.nextInt();
    		if(n!=0)
    		{
    			int[] arr = new int[n];
    			for(int i =0;i<n;i++)
    			{
    				arr[i]=in.nextInt();
    			}
    			for(int i=0;i<n;i++)
    			{
    				if(i!=n-1)
    					System.out.print(arr[i]+" ");
    				else
    					System.out.println(arr[i]+" ");
    			}
    			for(int i=n-1;i>=0;i--)
    			{
    				if(i!=0)
    					System.out.print(arr[i]+" ");
    				else
    					System.out.println(arr[i]+" ");
    			}
    			
    		}
    		else
    			System.out.println("list is empty");
    		
    	}
    }
    }
    
    
    • 1

    Information

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