zoukankan      html  css  js  c++  java
  • 续上文----线性表之单链表(C实现)

    本文绪上文线性表之顺序表(C实现)

    本文将继续使用单链表实现线性表的另外一种存储结构.这种使用链表实现的存储结构在内存中是不连续的.

    C实现代码如下:

      1 #include<stdio.h>
      2 
      3 typedef struct node
      4 {
      5     int data;
      6     struct node *next;
      7 }Node;
      8 
      9 //链表的初始化
     10 Node* InitList(int number)
     11 {
     12     int i;
     13     Node *pHead=(Node *)malloc(sizeof(Node));
     14     Node *TempHead=pHead;
     15     Node *Head=pHead;
     16     int data;
     17     for(i=0;i<number;i++)
     18     {
     19         pHead=(Node *)malloc(sizeof(Node));
     20         printf("Please input the %dst node data:
    ",i+1);
     21         scanf("%d",&data);
     22         pHead->data=data;
     23         pHead->next=NULL;
     24         TempHead->next=pHead;
     25         TempHead=TempHead->next;
     26     }
     27     return Head;
     28 }
     29 
     30 //显示链表
     31 void ShowList(Node *Head)
     32 {
     33     Node *TempNode=Head;
     34     Head=Head->next;
     35     while(Head->next!=NULL)
     36     {
     37         printf("%d ",Head->data);
     38         Head=Head->next;
     39     }
     40     printf("%d",Head->data);
     41     printf("
    ");
     42 }
     43 
     44 //输出链表某个值的位置
     45 int ListLocation(Node *Head,int data,int number)
     46 {
     47     Node *TempNode=Head;
     48     int location=1;
     49     TempNode=TempNode->next;
     50     while(TempNode->next!=NULL)
     51     {
     52         if(TempNode->data==data)
     53             {
     54                 return location;
     55             }
     56             location++;
     57             TempNode=TempNode->next;
     58     }
     59     if(location>=number)
     60         printf("Not found!");
     61 }
     62 
     63 //输出链表某个位置的值
     64 int ListData(Node *Head,int location,int number)
     65 {
     66     if(location>number)
     67         printf("Not found!");
     68 
     69     Node *TempNode=Head;
     70     TempNode=TempNode->next;
     71     int i;
     72     for(i=1;i<=number;i++)
     73     {
     74         if(location==i)
     75             return TempNode->data;
     76         TempNode=TempNode->next;
     77     }
     78 }
     79 
     80 //头入法插入元素
     81 void HeadInsertData(Node *Head,int data)
     82 {
     83     Node *InsertNode=(Node *)malloc(sizeof(Node));
     84     InsertNode->data=data;
     85     InsertNode->next=Head->next;
     86     Head->next=InsertNode;
     87 }
     88 
     89 //尾入插入除元素
     90 void TailInsertData(Node *Head,int data)
     91 {
     92     Node *TempNode=Head;
     93     Node *InsertNode=(Node *)malloc(sizeof(Node));
     94     InsertNode->data=data;
     95     while(TempNode->next!=NULL)
     96         TempNode=TempNode->next;
     97 
     98     TempNode->next=InsertNode;
     99     InsertNode->next=NULL;
    100 }
    101 
    102 
    103 
    104 //删除头结点
    105 void HeadDeleteData(Node *Head)
    106 {
    107     Head->next=Head->next->next;
    108 }
    109 
    110 
    111 //删除尾结点
    112 void TailDeleteData(Node *Head)
    113 {
    114     Node *TempNode=Head;
    115     while(Head->next->next!=NULL)
    116         Head=Head->next;
    117 
    118     Head->next=NULL;
    119 }
    120 
    121 int main()
    122 {
    123     Node *Head;
    124     int number;
    125     printf("Please input the node number:
    ");
    126     scanf("%d",&number);
    127     Head=InitList(number);
    128     printf("The initital list is:
    ");
    129     ShowList(Head);
    130 
    131     int flag;
    132     printf("
    
    ");
    133     printf("**********************Your Choice********************
    ");
    134     printf("****************1-输出链表某个值的位置***************
    ");
    135     printf("****************2-输出链表某个位置的值***************
    ");
    136     printf("****************3-头入法插入元素*********************
    ");
    137     printf("****************4-尾入法插入元素*********************
    ");
    138     printf("****************5-删除头结点*************************
    ");
    139     printf("****************6-删除尾结点*************************
    ");
    140     printf("****************0-退出*******************************
    ");
    141     printf("
    
    ");
    142     printf("Please input flag:
    ");
    143     scanf("%d",&flag);
    144 
    145     switch(flag)
    146     {
    147         case 1:
    148         {
    149             int data;
    150             printf("Please input the data you want locate:
    ");
    151             scanf("%d",&data);
    152             int location;
    153             location=ListLocation(Head,data,number);
    154             printf("The data's location is: %d",location);
    155             break;
    156         }
    157         case 2:
    158         {
    159             int location;
    160             printf("Please input the location you want  data:
    ");
    161             scanf("%d",&location);
    162             int data;
    163             data=ListData(Head,location,number);
    164             printf("The location's data is: %d
    ",data);
    165             break;
    166         }
    167         case 3:
    168         {
    169             int data;
    170             printf("Please input the data you want insert in head:
    ");
    171             scanf("%d",&data);
    172             HeadInsertData(Head,data);
    173             ShowList(Head);
    174             break;
    175         }
    176         case 4:
    177         {
    178             int data;
    179             printf("Please input the data you want insert in tail:
    ");
    180             scanf("%d",&data);
    181             TailInsertData(Head,data);
    182             ShowList(Head);
    183             break;
    184         }
    185         case 5:
    186         {
    187             HeadDeleteData(Head);
    188             ShowList(Head);
    189             break;
    190         }
    191         case 6:
    192         {
    193             TailDeleteData(Head);
    194             ShowList(Head);
    195             break;
    196         }
    197         case 7:
    198         {
    199            printf("You choose to exit.
    ");
    200            break;
    201         }
    202     }
    203     return 0;
    204 }

    结果图:


     
  • 相关阅读:
    Compression algorithm (deflate)
    tcpip数据包编码解析(chunk and gzip)_space of Jialy_百度空间
    What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
    gzip压缩算法: gzip 所使用压缩算法的基本原理
    Decompressing a GZip Stream with Zlib
    Frequently Asked Questions about zlib
    how to decompress gzip stream with zlib
    自己动手写web服务器四(web服务器是如何通过压缩数据,web服务器的gzip模块的实现)
    What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
    C语言抓http gzip包并解压 失败 C/C++ ChinaUnix.net
  • 原文地址:https://www.cnblogs.com/vpoet/p/4659720.html
Copyright © 2011-2022 走看看