續上文----線性表之單鏈表(C實現)

來源:互聯網
上載者:User

標籤:順序表   鏈表   資料結構   儲存結構   

本文緒上文線性表之順序表(C實現)



本文將繼續使用單鏈表實現線性表

的另外一種儲存結構.這種使用

鏈表實現的儲存結構在記憶體中是

不連續的.



C實現代碼如下:

#include<stdio.h>typedef struct node{    int data;    struct node *next;}Node;//鏈表的初始化Node* InitList(int number){    int i;    Node *pHead=(Node *)malloc(sizeof(Node));    Node *TempHead=pHead;    Node *Head=pHead;    int data;    for(i=0;i<number;i++)    {        pHead=(Node *)malloc(sizeof(Node));        printf("Please input the %dst node data:\n",i+1);        scanf("%d",&data);        pHead->data=data;        pHead->next=NULL;        TempHead->next=pHead;        TempHead=TempHead->next;    }    return Head;}//顯示鏈表void ShowList(Node *Head){    Node *TempNode=Head;    Head=Head->next;    while(Head->next!=NULL)    {        printf("%d ",Head->data);        Head=Head->next;    }    printf("%d",Head->data);    printf("\n");}//輸出鏈表某個值的位置int ListLocation(Node *Head,int data,int number){    Node *TempNode=Head;    int location=1;    TempNode=TempNode->next;    while(TempNode->next!=NULL)    {        if(TempNode->data==data)            {                return location;            }            location++;            TempNode=TempNode->next;    }    if(location>=number)        printf("Not found!");}//輸出鏈表某個位置的值int ListData(Node *Head,int location,int number){    if(location>number)        printf("Not found!");    Node *TempNode=Head;    TempNode=TempNode->next;    int i;    for(i=1;i<=number;i++)    {        if(location==i)            return TempNode->data;        TempNode=TempNode->next;    }}//頭入法插入元素void HeadInsertData(Node *Head,int data){    Node *InsertNode=(Node *)malloc(sizeof(Node));    InsertNode->data=data;    InsertNode->next=Head->next;    Head->next=InsertNode;}//尾入插入除元素void TailInsertData(Node *Head,int data){    Node *TempNode=Head;    Node *InsertNode=(Node *)malloc(sizeof(Node));    InsertNode->data=data;    while(TempNode->next!=NULL)        TempNode=TempNode->next;    TempNode->next=InsertNode;    InsertNode->next=NULL;}//刪除頭結點void HeadDeleteData(Node *Head){    Head->next=Head->next->next;}//刪除尾結點void TailDeleteData(Node *Head){    Node *TempNode=Head;    while(Head->next->next!=NULL)        Head=Head->next;    Head->next=NULL;}int main(){    Node *Head;    int number;    printf("Please input the node number:\n");    scanf("%d",&number);    Head=InitList(number);    printf("The initital list is:\n");    ShowList(Head);    int flag;    printf("\n\n");    printf("**********************Your Choice********************\n");    printf("****************1-輸出鏈表某個值的位置***************\n");    printf("****************2-輸出鏈表某個位置的值***************\n");    printf("****************3-頭入法插入元素*********************\n");    printf("****************4-尾入法插入元素*********************\n");    printf("****************5-刪除頭結點*************************\n");    printf("****************6-刪除尾結點*************************\n");    printf("****************0-退出*******************************\n");    printf("\n\n");    printf("Please input flag:\n");    scanf("%d",&flag);    switch(flag)    {        case 1:        {            int data;            printf("Please input the data you want locate:\n");            scanf("%d",&data);            int location;            location=ListLocation(Head,data,number);            printf("The data's location is: %d",location);            break;        }        case 2:        {            int location;            printf("Please input the location you want  data:\n");            scanf("%d",&location);            int data;            data=ListData(Head,location,number);            printf("The location's data is: %d\n",data);            break;        }        case 3:        {            int data;            printf("Please input the data you want insert in head:\n");            scanf("%d",&data);            HeadInsertData(Head,data);            ShowList(Head);            break;        }        case 4:        {            int data;            printf("Please input the data you want insert in tail:\n");            scanf("%d",&data);            TailInsertData(Head,data);            ShowList(Head);            break;        }        case 5:        {            HeadDeleteData(Head);            ShowList(Head);            break;        }        case 6:        {            TailDeleteData(Head);            ShowList(Head);            break;        }        case 7:        {           printf("You choose to exit.\n");           break;        }    }    return 0;}

結果圖:




轉載請註明作者:小劉


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.