线性链表
- 链表中结点的逻辑次序和物理次序不一定相同。即:逻辑上相邻未在物理上相邻。结点之间的相对位置由链表中的指针域指示,而结点在存储器中的存储位置是随意的。
- 节点由数据域和指针域组成
- 每个节点只有一个链域
- 单链表由头指针唯一确定,单链表可以用头指针的名字来命名。
- code
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node /*结点类型定义*/
{
datatype data; /*数据域*/
struct node *next; /*next为指针域,指向该结点的后继*/
}SList;
SList *head1,*head2,*head3,*p; /*指针类型说明*/
SList *CreateListF();
SList *CreateListR();
void ShowSList(SList* head);
void InsertBefore(SList *head,SList *p,datatype x); //前插
void InsertAfter(SList *p,datatype x); //后插
SList *Get(SList* head, int i); //返回链表位置为i的节点指针值
SList *LOCATE(SList* head, datatype key); //返回data域为key的节点指针值
void DeleteA(SList *p);
SList *CreateListR1();
int main()
{
SList *p1,*p2;
// head1=CreateListF();
// printf("头插建表结果head1:\n");
// ShowSList(head1);
//
// head2=CreateListR();
// printf("尾插建表结果head2:\n");
// ShowSList(head2);
head3=CreateListR1();
printf("改进尾插建表结果head3:\n");
ShowSList(head3);
//
// printf("\nThe list dealed with is the 3th list\n");
p1=Get(head3, 2);
printf("\nThe data of 2nd node of list head3 is %d\n",p1->data);
p2=LOCATE(head3, 6);
if (p2==NULL) printf("\nLOCATE 6 FAILURE\n");
else printf("\nLOCATE 6 SUCCESS\n");
// p2=LOCATE(head3, 4);
// if (p2==NULL) printf("\nLOCATE 4 FAILURE\n");
// else printf("\nLOCATE 4 SUCCESS\n");
DeleteA(p2);
printf("\nDelete the 3th node\n");
ShowSList(head3);
InsertAfter(p2,99);
InsertBefore(head3,p2,88) ;
printf("\nInsert before and after the 2th node\n");
ShowSList(head3);/**/
DeleteA1(head3,99);
printf("\nDelete the data=99 node\n");
ShowSList(head3);
system("pause");
}
SList *CreateListF() /*头插法*/
{
int ch; /*逐个插入字符,以"$"为结束符,返回单链表头指针*/
SList *head,*s;
head=NULL; /*链表开始为空*/
printf("Please intput integer until '0':\n");
scanf("%d",&ch); /*读入第一个结点的值*/
while(ch!=0)
{
s=(SList *)malloc(sizeof(SList)); /*生成新结点*/
s->data=ch;
s->next=head; //s-->head
head=s; //head后移,后一个在前
printf("Please intput integer until '0':\n");
scanf("%d",&ch);
}
return head;
} /*CreateListF*/
SList *CreateListR() /*尾插法*/
{
int ch;
SList *head,*s,*r;
head=NULL; /*链表开始为空*/
r=NULL; /*尾指针初值为空*/
printf("Please intput more than 4 integers until '0':\n");
scanf("%d",&ch); /*读入第一个结点的值*/
while(ch!=0) /*'0'为输入结束符*/
{
s=(SList *)malloc(sizeof(SList)); /*生成新结点*/
s->data=ch; /*保存数据到链表*/
if (head==NULL) head=s; /*如果当前链表为空,head==s*/
else r->next=s; //如果当前链表不为空,r-->s
r=s; //r后移,后一个在后
printf("Please intput more than 4 integers until '0':\n");
scanf("%d",&ch);
}
if (r!=NULL) r->next=NULL;
return head;
} /*CreateListR*/
SList *CreateListR1() /*改进尾插法(不用判断链表是否为空)*/
{
int ch;
SList *head,*s,*r;
head=(SList *)malloc(sizeof(SList)); /*生成头结点*/
head->data=0;
r=head; /*尾指针初值等于头结点*/
printf("Please intput integer until '0':\n");
scanf("%d",&ch);
while(ch!=0) /*'0'为输入结束符*/
{
s=(SList *)malloc(sizeof(SList)); /*生成新结点*/
s->data=ch;
r->next=s; /*新结点插入表尾*/
r=s; /*尾指针r的值指向新的表尾*/
printf("Please intput integer until '0':\n");
scanf("%d",&ch); /*读下一结点*/
}
r->next=NULL;
return head;
} /*CreateListR1*/
SList *Get(SList* head, int i)
{
int j;
SList* p;
p=head; /*从头结点起扫描*/
j=0;
if (p->data==0) {p=p->next; j++;} /*对有头结点的链表进行特殊处理*/
while((p->next!=NULL)&&(i>j)) /*'$'为输入结束符*/
{
p=p->next;
j++;
}
if (i==j) return p;
else return NULL;
}
SList *LOCATE(SList* head, int key)
{
SList* p;
p=head->next; /*从第一个结点起扫描*/
while(p!=NULL) /*扫描整个表*/
if (p->data!=key) p=p->next;
else break;
return p;
}
void InsertAfter(SList *p,datatype x) /*将值为X的新结点插入*P之后(O[1])*/
{
SList *s;
s=(SList *)malloc(sizeof(SList)); /*生成新结点*/
s->data=x;
s->next=p->next;p->next=s; /*将*s插入*p之后*/
} /*InsertAfter */
void InsertBefore(SList *head,SList *p,datatype x)
/*在带头结点的单链表head中,将值为X的新结点插入*P之前(O[n])*/
{
SList *q,*s;
s=(SList *)malloc(sizeof(SList)); /*生成新结点*/
s->data=x;
q=head; /*从头指针开始*/
while(q->next!=p) q=q->next; /*找*p的前趋结点*/
s->next=p;
q->next=s; /*将*s插入*p之 前*/
} /*InsertBefore */
void DeleteA(SList *p) /*删除*p的后继结点*r,设*r存在*/
{
SList *r;
if(p->next!=NULL)
{
r=p->next;
p->next=r->next;
free(r);
}
}/*end deleteA*/
void DeleteA1(SList* head,datatype a) /*删除*p的后继结点*r,设*r存在*/
{
SList *r;
p=head;
while(p->next->data!=a) //找到节点data域为a的节点的前驱
{
p=p->next;
if (p->next==NULL)
{
printf("no found!");
return -1;
}
}
r=p->next;
p->next=r->next;
free(r);
}/*end deleteA*/
void ShowSList(SList* head)
{
SList *p;
p=head;
if (p->data==0) p=p->next; /*对有头结点的链表进行特殊处理*/
while(p->next!=0)
{
printf("%d-->",p->data);
p=p->next;
}
printf("%d\n\n",p->data);
}
- resulted
能给你有所帮助就是我最大的荣誉 ! 不积硅步,无以至千里,加油!