一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C/C++ - C語言實現線性表的基本操作詳解

C語言實現線性表的基本操作詳解

2022-03-06 17:56小柱同學要努力 C/C++

線性表是最基本、最簡單、也是最常用的一種數據結構。一個線性表是n個具有相同特性的數據元素的有限序列,這篇文章帶你學習如何通過C語言實現線性表的順序存儲和鏈式存儲

前言

這里使用的工具是DEV C++

可以借鑒一下

一、實訓名稱

線性表的基本操作

二、實訓目的

1.掌握線性表的基本概念

2.掌握線性表的存儲結構(順序存儲與鏈式存儲)

3.掌握線性表的基本操作

三、實訓要求

1.線性表可以順序表也可以用單鏈表實現,鼓勵大家用兩種方式實現。

2.創建線性表時,數據從鍵盤輸入整形數據

3.線性表類型定義和或各種操作的實現,可以用教材給出的方法,也可以自己設計。

四、實現效果

C語言實現線性表的基本操作詳解

五、順序存儲代碼實現

源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
#define LISTINCREMENT 10
 
 
typedef struct{
    int *elem;
    int length;
    int listsize;
}sqlist;
 
 void Creatlist(sqlist &L){
    //創建一個線性表
     L.elem=(int *)malloc(MAX*sizeof(int)) ;
     if(!L.elem)return ;
     L.listsize=MAX;
     printf("輸入表的長度:");
     scanf("%d",&L.length);
     printf("輸入%d的個數:",L.length);
     for(int i=0;i<L.length;i++)
     scanf("%d",&L.elem[i]);
 }
 
 //開始添加取地址符號
 void Traverse(sqlist &L){
    //遍歷
     printf("表中數據為:") ;
     for(int i=0;i<L.length;i++)
     printf("%3d",L.elem[i]) ;
     printf("\n");
 }
 
//查找
 void LocateElem(sqlist &L,int e){
     int i;
     printf("輸入要查找的元素:\n");
     scanf("%d",&e);
     for(int i=0;i<L.length;i++)
     {
        if(L.elem[i]==e){
            printf("查找成功,查找元素為%d",L.elem[i]);
            printf("\n");
            return ;
         }//if
      } //for
    printf("查找失敗!");
     printf("\n");
     
 }//void
 
//確定要刪除的元素
 int makesureElem(sqlist &L,int e){
    int i;
    
     for(i=0;i<L.length;i++){
        if(L.elem[i]==e){
            printf("要刪除的元素 位置為%d",i+1);
            printf("\n");
            return (i+1);
         }//if
     } //for
     printf("元素不存在!");
     printf("\n");
     
     return 0;
 }
 
 //插入某個元素
 void Listinsert(sqlist &L){
    int i;int e;
    printf("輸入要插入位置及元素\n");
    scanf("%d%d",&i,&e);
    printf("在順序線性表中第%d個位置之前插入新的元素%d。\n",i,e);
    //在順序線性表L中第i個位置之前插入新的元素e
     if(i<1||i>L.length+1)return;
//   1<=i<=listlength(L)+1;
int *p,*q;
q=&(L.elem[i-1]);
for(p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
 
++L.length;
 
return;
 }
 
 int listdelete(sqlist &L){
    //刪除元素
     int i,e;
     printf("輸入要刪除的元素\n");
     scanf("%d",&e);
     i=makesureElem(L,e);
     if((i<1)||(i>L.length))return 0;
     else{
        int *p,*q;
        p=&(L.elem[i-1]);
        e=*p;
        q=L.elem+L.length-1;
        for(++p;p<=q;++p)*(p-1)=*p;//被刪除元素之后的元素左移
         -L.length;
         printf("元素被刪除!");
          
     }
      return 0;
 }
 
 
 
int main(){
    sqlist L;
Creatlist(L);
 
    Traverse(L);
    LocateElem(L,1);
    
    Listinsert(L);
    Traverse(L);;
    
    listdelete(L);
    Traverse(L);
    return 0;
}

六、鏈式存儲代碼實現

源碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
 
struct  danlianbiao_Node{
 
int data;
struct danlianbiao_Node *next;
 
};
 
//base的結構體
 struct  danlianbiao{
 
int length;
struct danlianbiao_Node *base;
 
};
 
  struct danlianbiao *danlianbiao_init();
 void danlianbiao_free(struct danlianbiao *list);
//清除
 void danlianbiao_clear(struct danlianbiao *list);
 int danlianbiao_length(struct danlianbiao *list);
 
//添加
 void danlianbiao_add(struct danlianbiao *list,int index,int value);
 void danlianbiao_remove(struct danlianbiao *list,int index);
 
//修改
 void danlianbiao_set(struct danlianbiao *list,int index,int value);
 int danlianbiao_get(struct danlianbiao *list,int index );
 
 //查詢
 int danlianbiao_lookup(struct danlianbiao *list,int value);
int danlianbiao_isempty(struct danlianbiao *list,int value);
 
 
 
void menu()
{
    printf("********目錄*******\n");
    printf("輸出單鏈表中的各元素值1\n");
    printf("在單鏈表中插入數據元素2\n");
    printf("在單鏈表中插入數據元素3\n");
    printf("尋找單鏈表中某一個數值4\n");
    printf("結束程序           0\n");
    printf("**********************************\n");
}
 
int main(int argc, char *argv[]) {
    
    int n,m,i,e;
    
  printf("請輸入元素的個數 :");
  scanf("%d",&n);
  printf("以此輸入%d個數據元素 :",n);
  menu();
  printf("輸入你的選擇:");
  scanf("%d",&m);
  switch(m)
  {
    
    case 1:
        printf("現在鏈表的元素為:");
           break;
    case 2:
        printf("依次輸入插入位置和數據元素(空格隔開):");
        scanf("%d %d",&i,&e);
        
   }
    return 0;
}
 
 
 
 struct danlianbiao *danlianbiao_init(){
 struct danlianbiao *list=NULL;
 list=( struct danlianbiao *)malloc(sizeof( struct danlianbiao ));
 list->base=(struct danlianbiao_Node *)malloc(sizeof(struct danlianbiao_Node));
 list->base->data=-1;
  list->base->next=NULL;
  list->length=0;
  return list;
 
 }
 
 
  
 void danlianbiao_free(struct danlianbiao *list){
 struct danlianbiao_Node *pnode=NULL;//可能出現問題指針
 while(list->base!=NULL){
    pnode=list->base;
    list->base=pnode->next;
    free(pnode);
    
 }//while
  free(list);
 
 }
 
 
//清除
 void danlianbiao_clear(struct danlianbiao *list){
    
    struct danlianbiao_Node *next=NULL;//可能出現問題指針
    struct danlianbiao_Node *pnode=NULL;
 while(list->base->next!=NULL){
 next=list->base->next;
    list->base->next=next->next;
    free(pnode);
    
 }//while
 list->length=0;
 }
 
//獲得單鏈表的長度
 int danlianbiao_length(struct danlianbiao *list){
    
    
    
 }
 
//添加 ,該函數沒有錯誤
 
 void danlianbiao_add(struct danlianbiao *list,int index,int value){
    int i=0;
    struct danlianbiao_Node *pnode=NULL;
     struct danlianbiao_Node *p=NULL;
     p=(struct danlianbiao_Node *)malloc(sizeof(struct danlianbiao_Node ));
     p->data=value;
     pnode=list->base;
     while(i<index-1&&pnode!=NULL){
        pnode=pnode->next;
         i++;
     }//while
     p->next=pnode->next;
     pnode->next=p;
     list->length++;
    
 }
 //
 //移除這一項沒有錯誤
 
 void danlianbiao_remove(struct danlianbiao *list,int index){
 struct danlianbiao_Node *pnode=NULL;
     struct danlianbiao_Node *p=NULL;
     p=(struct danlianbiao_Node *)malloc(sizeof(struct danlianbiao_Node ));
     //有錯誤
     int value;int i;
     p->data=value;
     pnode=list->base;
     while(i<index-1&&pnode!=NULL){
        pnode=pnode->next;
         i++; }
         p=pnode->next;
         pnode->next=p->next;
         free(p);
 
 }
  
//設置值 ,這一項沒有錯誤
 
 void danlianbiao_set(struct danlianbiao *list,int index,int value){
        int i=0;
    struct danlianbiao_Node *pnode=NULL;
     struct danlianbiao_Node *p=NULL;
     p=(struct danlianbiao_Node *)malloc(sizeof(struct danlianbiao_Node ));
    
     p->data=value;
     pnode=list->base;
     while(i<index-1&&pnode!=NULL){
        pnode=pnode->next;
         i++;
     }//while
    pnode->data=value;
    
 }
 
 
//得到一個數值函數方法沒有錯誤
 
 int danlianbiao_get(struct danlianbiao *list,int index ){
        int i=0;
        int value;
    struct danlianbiao_Node *pnode=NULL;
     struct danlianbiao_Node *p=NULL;
     p=(struct danlianbiao_Node *)malloc(sizeof(struct danlianbiao_Node ));
    
     p->data=value;
     pnode=list->base;
     while(i<index-1&&pnode!=NULL){
        pnode=pnode->next;
         i++;
     }//while
 return(    pnode->data);
    
    
    
 }
 
 
 //
 
 int danlianbiao_lookup(struct danlianbiao *list,int value){
    
    int i=0;
        struct danlianbiao_Node *pnode=NULL;
        for(pnode=list->base->next;pnode !=NULL;pnode=pnode->next){
            i++;
            if(pnode->data==value) {
                return i;
             }//if
         }//for
    return 0;
 }
 
int danlianbiao_isempty(struct danlianbiao *list,int value){
}<font face="Arial, Verdana, sans-serif"><span style="white-space: normal;"> </span></font>

到此這篇關于C語言實現線性表的基本操作詳解的文章就介紹到這了,更多相關C語言 線性表內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/weixin_43304253/article/details/121539680

延伸 · 閱讀

精彩推薦
  • C/C++C語言中炫酷的文件操作實例詳解

    C語言中炫酷的文件操作實例詳解

    內存中的數據都是暫時的,當程序結束時,它們都將丟失,為了永久性的保存大量的數據,C語言提供了對文件的操作,這篇文章主要給大家介紹了關于C語言中文件...

    針眼_6702022-01-24
  • C/C++深入理解goto語句的替代實現方式分析

    深入理解goto語句的替代實現方式分析

    本篇文章是對goto語句的替代實現方式進行了詳細的分析介紹,需要的朋友參考下...

    C語言教程網7342020-12-03
  • C/C++C語言實現電腦關機程序

    C語言實現電腦關機程序

    這篇文章主要為大家詳細介紹了C語言實現電腦關機程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    xiaocaidayong8482021-08-20
  • C/C++C++之重載 重定義與重寫用法詳解

    C++之重載 重定義與重寫用法詳解

    這篇文章主要介紹了C++之重載 重定義與重寫用法詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下...

    青山的青6062022-01-04
  • C/C++詳解c語言中的 strcpy和strncpy字符串函數使用

    詳解c語言中的 strcpy和strncpy字符串函數使用

    strcpy 和strcnpy函數是字符串復制函數。接下來通過本文給大家介紹c語言中的strcpy和strncpy字符串函數使用,感興趣的朋友跟隨小編要求看看吧...

    spring-go5642021-07-02
  • C/C++C/C++經典實例之模擬計算器示例代碼

    C/C++經典實例之模擬計算器示例代碼

    最近在看到的一個需求,本以為比較簡單,但花了不少時間,所以下面這篇文章主要給大家介紹了關于C/C++經典實例之模擬計算器的相關資料,文中通過示...

    jia150610152021-06-07
  • C/C++學習C++編程的必備軟件

    學習C++編程的必備軟件

    本文給大家分享的是作者在學習使用C++進行編程的時候所用到的一些常用的軟件,這里推薦給大家...

    謝恩銘10102021-05-08
  • C/C++c++ 單線程實現同時監聽多個端口

    c++ 單線程實現同時監聽多個端口

    這篇文章主要介紹了c++ 單線程實現同時監聽多個端口的方法,幫助大家更好的理解和學習使用c++,感興趣的朋友可以了解下...

    源之緣11542021-10-27
主站蜘蛛池模板: 皇上好大好硬好涨好深好爽 | 久草在线福利资站免费视频 | 国产盗摄女厕美女嘘嘘 | 91九色丨porny丨制服 | 日本午夜色 | 四虎影视e456fcom四虎影视 | 欧美三级免费观看 | 亚洲丰满女人ass硕大 | 欧美人与日本人xx在线视频 | 国内精品久久久久影院男同志 | 午夜 在线播放 | 王晶经典三级 | 精品精品国产yyy5857香蕉 | 国内精品视频一区二区三区 | 精品视频日本 | 国模李丽莎大尺度啪啪 | 91最新高端约会系列178 | 国亚洲欧美日韩精品 | 精品国产欧美一区二区三区成人 | 青青青久热国产精品视频 | 国产激情一区二区三区四区 | 国产日韩精品欧美一区 | 日本性生活免费看 | 国产自在线观看 | 日本69视频在线观看 | 欧美视频精品一区二区三区 | 国产精品1 | 肉大捧一进一出视频免费播放 | 99视频免费在线 | 国产日本欧美亚洲精品视 | 亚洲一区二区精品推荐 | 免费观看a毛片一区二区不卡 | 国产精品不卡高清在线观看 | 国产精品毛片高清在线完整版 | 紧身牛仔裤美女被啪啪久久网 | 四虎影视在线观看2413 | 91你懂的| 亚洲国产情侣一区二区三区 | 国产在线观看精品 | 男女小视频在线观看 | 欧美日韩不卡视频 |