1.顺序表
//静态分配数组
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
struct SqListNode{
char data[MAXSIZE];//此处的datatype为char
int last;/*last为数组下标,而length为线性表长度 length=last+1
注意在查找的时候用的是位序 ,不是数组下标 */
};
typedef struct SqListNode *List;
List creat()
{
int i=0;
char ch;
List L=(List)malloc(sizeof(struct SqListNode));
L->last=-1;
printf("请输入顺序表L中元素,以#结束\n" );
while((ch=getchar())!='#')
{
L->data[i++]=ch;
L->last++;
}
return L;
}
void insert(List L,char x,int i)
{
//注意插入时的溢出情况和位置不合法情况
int j;
if((L->last)>=MAXSIZE-1)
{
printf("overflow\n");
}
else if(i<1||i>(L->last)+2)
{
printf("error\n");
}
else
{
for(j=L->last;j>=i-1;j--) //数组元素向后退
{
L->data[j+1]=L->data[j];
}
L->last++;
L->data[i-1]=x;
printf("success\n");
}
}
void output(List L)
{
int i;
for(i=0;i<=L->last;i++)
printf("%c",L->data[i]);
printf("\n");
}
int main()
{
char ch;
int i=0;
List L=creat();
output(L);
printf("INPUT ch:\n");
ch=getche(); //这里要用getche()才行......
printf("INPUT position:\n");
scanf("%d",&i);
insert(L,ch,i);
output(L);
}
//动态分配数组
struct seqList{
int *data;
int maxsize,length;
};
typedef struct seqList *List;
List L=(List)malloc(struct seqList);
L->data=(int)malloc(sizeof(int)*InitSize)
链表
灰常不容易啊找了好久BUG!!!求length的时候cnt要++,p也要++啊!!!死在这个bug上了 还有删除节点时,第一个节点要注意!!!
#include<stdio.h>
#include<stdlib.h>
typedef struct node *List;
struct node
{
int data;
List next;
};
List creat1()
{
int k;
List s,head;
List L=(List)malloc(sizeof(struct node));
L->next=NULL;
head=L;
printf("please enter list until -1");
while(1)
{
scanf("%d",&k);
if(k==-1) break;
else
{
s=(List)malloc(sizeof(struct node));
s->data=k;
s->next=L->next;
L->next=s;
}
}
return head;
}
List creat2()
{
int k;
List s,r,head;
List L=(List)malloc(sizeof(struct node));
L->next=NULL;
head=L;
r=L;
printf("please enter list until -1\n");
while(1)
{
scanf("%d",&k);
if(k==-1) break;
else
{
s=(List)malloc(sizeof(struct node));
s->data=k;
r->next=s;
r=s;
}
}
r->next=NULL;
return head;
}
int length(List L)
{
List p=L;
int num=0;
while(p->next!=NULL)
{
num++;
p=p->next;
}
return num;
}
List findKth(List L,int k)//查找位序为i
{
List p=L->next;
int cnt=1;
while(p!=NULL&&cnt!=k)
{
p=p->next;
cnt++;
}
if(p) return p;//此处返回的是指针,,也可以返回值p->data
else return NULL;
}
List find(List L,int x)//查找值为x
{
List p=L->next;
while(p!=NULL&&p->data!=x)
{
p=p->next;
}
if(p->data==x) return p;
else return NULL;
}
List insert(List L,int x,int i) //位置i处插入x
{
List p,s;
if(i<=0||i>length(L)+1)
{
printf("error\n");
return NULL;
}
else
{
s=(List)malloc(sizeof(struct node));
p=findKth(L,i-1); //找到前驱结点
s->data=x;
s->next=p->next;
p->next=s;
printf("success");
return L;
}
}
List delete0(List L,int i) //删除位序i的元素 或者根据元素删...种类好多啊
{
List tmp=L;
if(i<=0||i>length(L))
{
printf("error\n");
return NULL;
}
else //关键容易bug的地方 当i取1时要分开考虑,让2号位接到head后面
{
List s=findKth(L,i);
if(i!=1)
{
List p=findKth(L,i-1);
p->next=s->next;
free(s);
}
else if(i==1)
{
tmp->next=s->next;
free(s);
}
}
return L;
}
void output(List L)
{
List p=L->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
int main()
{
int k,i;
List L=creat2();
output(L);
printf("插入元素k=");
scanf("%d",&k);
printf("插入位置i=");
scanf("%d",&i);
L=insert(L,k,i);
output(L);
printf("删除位置i=");
scanf("%d",&i);
L=delete0(L,i);
output(L);
}
习题
做一个链表复习集合吧><
typedef struct node *PtrToNode;//指向链表的指针
typedef struct node List;//通常所说的“给定一个单链表”,表示给定一个指向链表头部的指针
struct node{
int data;
PtrToNode next;
};
求单链表结点的阶乘和
本题要求实现一个函数,求单链表L结点的阶乘和。这里默认所有结点的值非负,且题目保证结果在int范围内。
输入样例:
3
5 3 6
输出样例:
846
#include <stdio.h>
#include <stdlib.h>
typedef struct Node *PtrToNode;
struct Node {
int Data; /* 存储结点数据 */
PtrToNode Next; /* 指向下一个结点的指针 */
};
typedef PtrToNode List; /* 定义单链表类型 */
int FactorialSum( List L );
int main()
{
int N, i;
List L, p;
scanf("%d", &N);
L = NULL;
for ( i=0; i<N; i++ ) {
p = (List)malloc(sizeof(struct Node));
scanf("%d", &p->Data);
p->Next = L; L = p;
}
printf("%d\n", FactorialSum(L));
return 0;
}
int FactorialSum( List L )
{
int fact,sum0=0,i;
PtrToNode P=L;
while(P)
{
fact=1;
for(i=2;i<=P->Data;i++)
fact*=i;
sum0+=fact;
P=P->Next;
}
return sum0;
}
线性表的链式存储实现
#include<cstdio>
typedef struct LNode PtrToNode;
typedef struct PtrToNode List;
struct LNode{
int data;
PtrToNode Next;
};
int findKth(List L,int K)
{
List p;
p=L;
int i=1;
while(i<K && p)
{
p=p->Next;
i++;
}
if(i==K&&p) return p->data;
else return error;
}
int findK(List L,int K)
{
List p=L;
int i=1;
while(p && p->data!=K)
{
p=p->Next;
i++;
}
if(K==p->data) return i;
else return error;
}
List insert(List L,int k,int data);//插入操作,返回的是链表头
{
PtrToNode tmp;
tmp=(List)malloc(sizeof(struct LNode));
tmp->data=data;
List p=L;
int cnt=1;
while(p && cnt<k-1)
{
p=p->Next;
cnt++;
}
if(cnt!=k-1)
{
puts("error");
free(tmp);
return error;
}*/
if(k==1)
{
tmp->Next=L;
return tmp;
}
else
{
tmp->Next=p->Next;
p->Next=tmp;
return L;
}
}
顺序表之二分查找
int BinarySearch( List L, int X )
{
int left=1;
int right=L->Last;
int center;
int flag=0;
while(left<=right)
{
center=(left+right)/2;
if(X<L->Data[center]) right=center+1;
else if(X>L->Data[center]) left=center-1;
else if (X==L->Data[center])
{
flag=1;
return center;
}
}
if(flag==0) return NOTFOUND
}