從尾到頭列印鏈表,尾到頭列印

來源:互聯網
上載者:User

從尾到頭列印鏈表,尾到頭列印


題目描述:

輸入一個鏈表,從尾到頭列印鏈表每個節點的值。

輸入:

每個輸入檔案僅包含一組測試範例。
每一組測試案例包含多行,每行一個大於0的整數,代表一個鏈表的節點。第一行是鏈表第一個節點的值,依次類推。當輸入到-1時代錶鏈表輸入完畢。-1本身不屬於鏈表。

輸出:

對應每個測試案例,以從尾到頭的順序輸出鏈表每個節點的值,每個值佔一行。

範例輸入:
12345-1
範例輸出:
54321
////  main.c//  從尾到頭列印鏈表////  Created by 李亞坤 on 14-9-29.//  Copyright (c) 2014年 李亞坤. All rights reserved.//#include <stdio.h>typedef struct ListElmt_    //鏈表元素宣告{    void *data;    struct ListElmt_ *next;}ListElmt;typedef struct List_        //鏈表聲明{    ListElmt *head;    ListElmt *tail;    int size;}List;void InitList(List *list)   //初始化鏈表{    list->size = 0;    list->head = NULL;    list->tail = NULL;    return;}int Insert_next(List *list, ListElmt *element, void *data)  // 在element元素後面插入{    ListElmt *new_element;    new_element = (ListElmt *)malloc(sizeof(ListElmt));    if (new_element == NULL) {        return -1;    }        new_element->data = data;    new_element->next = NULL;        if (element == NULL) {        if (list->size == 0) {            list->tail = new_element;        }        else        {            new_element->next = list->head;        }        list->head = new_element;    }    else    {        if (element->next == NULL) {            list->tail = new_element;        }                new_element->next = element->next;        element->next = new_element;    }    list->size++;    return 0;}void print_list_int(List *list)         // 輸出鏈表每一個元素{    if (list->size == 0) {        return;    }        ListElmt *element;        for (element = list->head; element != list->tail; element = element->next) {        printf("%d\n", *((int *)(element->data)));    }    printf("%d\n", *((int *)(element->data)));        return;}int main(int argc, const char * argv[]) {    int *input, i;    input = (int *)malloc(sizeof(int));        List * l;    l = (List *)malloc((sizeof(List)));    InitList(l);        do{        scanf("%d", &input[i]);        input = (int *)realloc(input, sizeof(int) * (i+1));        if (input[i] > 0) {         // 存入正整數            Insert_next(l, NULL, &input[i]);        }        i++;    }while (input[i-1] != -1);        print_list_int(l);    return 0;}



輸入一個鏈表的頭結點,從尾到頭反過來輸出每個結點的值 為何不會輸出結果,

p0= (node* ) malloc(sizeof(node));
p1= (node* ) malloc(sizeof(node));

p2= (node* ) malloc(sizeof(node));

p3= (node* ) malloc(sizeof(node));

在主函數裡面多加4句話
要給p0,p1,p2,p3,p4分配記憶體空間
 
c資料結構 實現單鏈表的建立、插入、刪除、列印與查詢

#include <iostream>
using namespace std;

typedef struct node
{
char data;
struct node *next;
}link;

link * get(link *l, int i)
{
link *p;int j=0;
p=l;
while((j<i) && (p->next!=NULL))
{p=p->next;j++;}
if(j==i)
return p;
else
return NULL;
}

link * ins (link *l, char ch,int i)
{ link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"輸入有誤"<<endl;
else
{
s=(link *)malloc(sizeof(link));
s->data=ch;
s->next=p->next;
p->next=s;
}
return l;
}

link * find(link *l, char ch)
{
link *p; int i=0; int j=0;
p=l;

while(p!=NULL)
{ i++;
if(p->data!=ch)
p=p->next;
else {cout<<"您尋找的資料在第"<<i-1<<"個位置."<<endl;
j=1;p=p->next;
}

}
if(j!=1)
cout<<"您尋找的資料不線上性表中."<<endl;
return l;
}

link * del(link *l, int i)
{
link *p,*s;
p=get(l,i-1);
if(p==NULL)
cout<<"輸入有誤"<<endl;
else
{
s=p->next;
p->next=s->next;
free(s);
}
return l;
}

link * add(link *l )
{
link *p,*s;
cout<<"請輸入一串單字元資料,以*結束!"<<endl;
char ch;
link *HEAD;
link *R,*P,*L;
HEAD=(link *)malloc(sizeof(link));
HEAD->next=NULL;
R=HEAD;
getchar();
ch=getchar();
while(ch!='*')
{
P=(link *)malloc(sizeof(link));
P->data=ch;P->next=NULL;
R->next=P;R=R->next;
getchar();
ch=getchar();

}

L=HEAD;
cout<<"當前輸入的線性表為:"<<endl;
P=L;P=P->next;
if(L!=NULL)......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.