c語言中除了數組以外,比較常見的數據結構就是鏈表了。
鏈表和數組的不同點在於一個是連續存儲,一個是不連續的。
數組更改元素的相對位置比較慢,比如把數組第k個元素去掉,然後k以後的元素整體前移。
鏈表只需釋放節點,更改指針就行
此篇是最簡單的單項鍊表的幾個功能。
首先是數據結構
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
}LNode;
建立鏈表
LNode *creat_L(){
LNode *h,*p,*s;
ElemType x;
h=(LNode*)malloc(sizeof(LNode));
h->next=NULL;
p=h;
printf("\n data=?");
scanf("%d",&x);
while(x!=-111){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->data=x;
s->next=NULL;
p->next=s;p=s;
printf("data=? (-111 end)");
scanf("%d",&x);
}
return h;
}
輸出鏈表
void out_L(LNode *L){
LNode *p;char ch;
p=L->next;
printf("\n\n");
while(p!=NULL){
printf("%5d",p->data);
p=p->next;
}
printf("\n\n按Enter鍵,繼續");
ch=getchar();
}
插入
void insert_L(LNode*L,int i,ElemType e){
LNode *s,*p,*q;int j;
p=L;
j=0;
while(p!=NULL){
p=p->next;
j++;
if(j==i-1)
break;
}
if(p==NULL||j>i-1)
printf("\n i ERROR!");
else{
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
}
}
刪除節點
ElemType delete_L(LNode *L,int i){
LNode *p,*q;int j;ElemType x;
p=L;j=0;
while(p->next!=NULL&&j<i-1){
p=p->next;j++;
}
if(p->next==NULL){
printf("\n i ERROR!");
return -1;
}
else{
q=p->next;
x=q->data;
p->next=q->next;free(q);
return x;
}
}
查找
int locat_L(LNode*L,ElemType e){
LNode *p;int j=1;
p=L->next;
while(p!=NULL&&p->data!=e){
p=p->next;
j++;
}
if(p!=NULL)
return j;
else
return -1;
}
最後是全部代碼
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
}LNode;
LNode * L;
LNode *creat_L();
void out_L(LNode *L);
void insert_L(LNode *L,int i,ElemType e);
ElemType delete_L(LNode *L,int i);
int locat_L(LNode *L,ElemType e);
int main(int argc, char *argv[])
{
int i,k,loc;
ElemType e,x;
char ch;
do{
printf("\n\n 1.建立線性鏈表");
printf("\n\n 2.在i位置插入元素");
printf("\n\n 3.刪除第i個元素,返回其值");
printf("\n\n 4.查找值爲e的元素");
printf("\n\n 5.結束程序運行");
printf("\n=====================");
printf("\n 請輸入你的選擇:(1,2,3,4,5)");
scanf("%d",&k);
switch(k)
{
case 1:{
L=creat_L();
out_L(L);
}break;
case 2:{
printf("\n i,e=?");
scanf("%d,%d",&i,&e);
insert_L(L,i,e);
out_L(L);
}break;
case 3:{
printf("\n i=?");
scanf("%d",&i);
x=delete_L(L,i);
out_L(L);
if(x!=-1)
printf("\n x=%d\n",x);
}break;
case 4:{
printf("\n e=?");
scanf("%d",&e);
loc=locat_L(L,e);
if(loc==-1)
printf("\n 未找到%d",loc);
else
printf("\n 已找到,元素位置是%d",loc);
}break;
}
getchar();
printf("\n-----------------");
}while(k>=1&&k<5);
printf("\n 再見!");
printf("\n 按enter鍵,返回");
ch=getchar();
return 0;
}
LNode *creat_L(){
LNode *h,*p,*s;
ElemType x;
h=(LNode*)malloc(sizeof(LNode));
h->next=NULL;
p=h;
printf("\n data=?");
scanf("%d",&x);
while(x!=-111){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->data=x;
s->next=NULL;
p->next=s;p=s;
printf("data=? (-111 end)");
scanf("%d",&x);
}
return h;
}
void out_L(LNode *L){
LNode *p;char ch;
p=L->next;
printf("\n\n");
while(p!=NULL){
printf("%5d",p->data);
p=p->next;
}
printf("\n\n按Enter鍵,繼續");
ch=getchar();
}
void insert_L(LNode*L,int i,ElemType e){
LNode *s,*p,*q;int j;
p=L;
j=0;
while(p!=NULL){
p=p->next;
j++;
if(j==i-1)
break;
}
if(p==NULL||j>i-1)
printf("\n i ERROR!");
else{
s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
}
}
ElemType delete_L(LNode *L,int i){
LNode *p,*q;int j;ElemType x;
p=L;j=0;
while(p->next!=NULL&&j<i-1){
p=p->next;j++;
}
if(p->next==NULL){
printf("\n i ERROR!");
return -1;
}
else{
q=p->next;
x=q->data;
p->next=q->next;free(q);
return x;
}
}
int locat_L(LNode*L,ElemType e){
LNode *p;int j=1;
p=L->next;
while(p!=NULL&&p->data!=e){
p=p->next;
j++;
}
if(p!=NULL)
return j;
else
return -1;
}