用头插、尾插、按顺序插入创建一个不带头节点的链表,栈的基本操作
2014-06-09 18:41
701 查看
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
typedef struct Node
{
int data;
struct Node *next;
}Node, *pNode;
/*尾插法*/
void create_list_rear(pNode *h)
{
srand(time(NULL));
pNode p, q;
p = q = *h = (pNode)calloc(1,sizeof(Node));
p->next = NULL;
int count = 0;
while (count!=N){
++count;
if (count == 1){
p->data = rand()%100;
printf("%d ", p->data);
}
else{
p = (pNode)calloc(1, sizeof(Node));
p->data = rand() % 100;
printf("%d ",p->data);
p->next = NULL;
q->next = p;
q = p;
}
}
printf("\n");
}
/*头插法*/
void create_list_front(pNode *h)
{
pNode p;
p = *h = (pNode)calloc(1,sizeof(Node));
p->next = NULL;
int count = 0;
while (count != N){
++count;
if (count == 1){
p->data = rand() % 100;
printf("%d ", p->data);
}
else {
p = (pNode)calloc(1,sizeof(Node));
p->data = rand() % 100;
printf("%d ", p->data);
p->next = *h;
*h = p;
}
}
printf("\n");
}
/*顺序插入法*/
void create_list_sequence(pNode *h)
{
pNode p, q, r=NULL;
p = q = *h = (pNode)calloc(1,sizeof(Node));
p->next = NULL;
int count = 0;
while (count != N){
++count;
if (count == 1){
p->data = rand()%100;
printf("%d ", p->data);
}
else{
r = (pNode)calloc(1,sizeof(Node));
r->data = rand() % 100;
printf("%d ", r->data);
p = q = *h;
while (p ->next != NULL && p->data < r->data ){
q = p;
p = p->next;
}
if (p->data >= r->data){
if (p == q){
r->next = *h;
*h = r;
}
else {
r->next = p;
q->next = r;
}
}
else{
p->next = r;
r->next = NULL;
}
}
}
printf("\n");
}
void printList(pNode h)
{
while (h != NULL){
printf("%d ",h->data);
h = h->next;
}
printf("\n");
}
int main()
{
pNode List = NULL;
/*尾插法*/
printf("尾插法:原数列的顺序为:\n");
create_list_rear(&List);
printf("链表的顺序为:\n");
printList(List);
/*头插法*/
printf("头插法:原数列的顺序为:\n");
create_list_front(&List);
printf("链表的顺序为:\n");
printList(List);
/*顺序插法*/
printf("顺序插法:原数列的顺序为:\n");
create_list_sequence(&List);
printf("链表的顺序为:\n");
printList(List);
return 0;
}
栈的存储结构有两种:一种是线性栈,一种是链式栈。下面分别是这两种存储结构的实现。
线性栈:
[cpp] view
plaincopy
#define STACK_INIT_SIZE 10
#define STACK_REALLOCATION 2
typedef int ElemType;
typedef struct SqStack
{
ElemType *base;
int top;
int stacklength;
}SqStack;
void InitSqStack(SqStack *S)
{
S->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if (!S->base)
exit(1);
S->top = 0;
S->stacklength = STACK_INIT_SIZE;
}
void Push_SqStack(SqStack *S,ElemType e)
{
if (S->top == S->stacklength){
S->base = (ElemType*)realloc(S->base,(S->stacklength
+STACK_REALLOCATION)*sizeof(ElemType));
if (!S->base)
exit(1);
S->stacklength += STACK_REALLOCATION;
}
S->base[S->top] = e;
++S->top;
}
int Pop_SqStack(SqStack *S, ElemType *e)
{
if (S->top == 0)
return 0;
--S->top;
*e = S->base[S->top];
return 1;
}
int GetTopElem_SqStack(SqStack *S, ElemType *e)
{
if (S->top == 0)
return 0;
*e = S->base[S->top-1];
return 1;
}
int isSqStackEmpty(SqStack S)
{
if (S.top == 0)
return 1;
else
return 0;
}
void SqStack_test()
{
SqStack S;
InitSqStack(&S);
ElemType e;
printf("Please input some numbers(Ctrl + Z to end).\n");
while (scanf("%d", &e) != EOF)
Push_SqStack(&S,e);
printf("The out stack sequence is:\n");
while (!isSqStackEmpty(S)){
Pop_SqStack(&S,&e);
printf("%d ",e);
}
printf("\n");
}
int main()
{
SqStack_test();
return 0;
}
链式栈:
[cpp] view
plaincopy
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACK_REALLOCATION 2
typedef int ElemType;
typedef struct LinkStack
{
ElemType data;
struct LinkStack *next;
}StackNode,*StackPNode;
StackPNode InitLinkStack()
{
StackPNode head = (StackPNode)malloc(sizeof(StackNode));
if (!head)
exit(1);
head->next = NULL;
head->data = 0;
return head;
}
void Push_LinkStack(StackPNode head, ElemType e)
{
StackPNode node = (StackPNode)malloc(sizeof(StackNode));
if (!node)
exit(1);
node->data = e;
node->next = NULL;
node->next = head->next;
head->next = node;
}
int Pop_LinkStack(StackPNode head, ElemType *e)
{
StackPNode p;
if (!head->next)
return 0;
//StackPNode p;
//StackPNode p;
//struct LinkStack *p;
p= head->next;
*e=p->data;
head->next = p->next;
free(p);
return 1;
}
int isLinkStackEmpty(StackPNode head)
{
if (head->next)
return 0;
else
return 1;
}
int getTopElem_LinkStack(StackPNode head,ElemType *e)
{
if (head->next)
return 0;
*e = head->next->data;
return 1;
}
void LinkStack_test()
{
StackPNode head = InitLinkStack();
ElemType e;
printf("Please input some numbers(Ctrl + Z to end).\n");
while (scanf("%d", &e) != EOF)
Push_LinkStack(head,e);
printf("The out stack sequence is:\n");
while (!isLinkStackEmpty(head)){
Pop_LinkStack(head, &e);
printf("%d ", e);
}
printf("\n");
}
int main()
{
LinkQueueTest();
return 0;
}
#include <stdlib.h>
#include <time.h>
#define N 10
typedef struct Node
{
int data;
struct Node *next;
}Node, *pNode;
/*尾插法*/
void create_list_rear(pNode *h)
{
srand(time(NULL));
pNode p, q;
p = q = *h = (pNode)calloc(1,sizeof(Node));
p->next = NULL;
int count = 0;
while (count!=N){
++count;
if (count == 1){
p->data = rand()%100;
printf("%d ", p->data);
}
else{
p = (pNode)calloc(1, sizeof(Node));
p->data = rand() % 100;
printf("%d ",p->data);
p->next = NULL;
q->next = p;
q = p;
}
}
printf("\n");
}
/*头插法*/
void create_list_front(pNode *h)
{
pNode p;
p = *h = (pNode)calloc(1,sizeof(Node));
p->next = NULL;
int count = 0;
while (count != N){
++count;
if (count == 1){
p->data = rand() % 100;
printf("%d ", p->data);
}
else {
p = (pNode)calloc(1,sizeof(Node));
p->data = rand() % 100;
printf("%d ", p->data);
p->next = *h;
*h = p;
}
}
printf("\n");
}
/*顺序插入法*/
void create_list_sequence(pNode *h)
{
pNode p, q, r=NULL;
p = q = *h = (pNode)calloc(1,sizeof(Node));
p->next = NULL;
int count = 0;
while (count != N){
++count;
if (count == 1){
p->data = rand()%100;
printf("%d ", p->data);
}
else{
r = (pNode)calloc(1,sizeof(Node));
r->data = rand() % 100;
printf("%d ", r->data);
p = q = *h;
while (p ->next != NULL && p->data < r->data ){
q = p;
p = p->next;
}
if (p->data >= r->data){
if (p == q){
r->next = *h;
*h = r;
}
else {
r->next = p;
q->next = r;
}
}
else{
p->next = r;
r->next = NULL;
}
}
}
printf("\n");
}
void printList(pNode h)
{
while (h != NULL){
printf("%d ",h->data);
h = h->next;
}
printf("\n");
}
int main()
{
pNode List = NULL;
/*尾插法*/
printf("尾插法:原数列的顺序为:\n");
create_list_rear(&List);
printf("链表的顺序为:\n");
printList(List);
/*头插法*/
printf("头插法:原数列的顺序为:\n");
create_list_front(&List);
printf("链表的顺序为:\n");
printList(List);
/*顺序插法*/
printf("顺序插法:原数列的顺序为:\n");
create_list_sequence(&List);
printf("链表的顺序为:\n");
printList(List);
return 0;
}
栈的存储结构有两种:一种是线性栈,一种是链式栈。下面分别是这两种存储结构的实现。
线性栈:
[cpp] view
plaincopy
#define STACK_INIT_SIZE 10
#define STACK_REALLOCATION 2
typedef int ElemType;
typedef struct SqStack
{
ElemType *base;
int top;
int stacklength;
}SqStack;
void InitSqStack(SqStack *S)
{
S->base = (ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if (!S->base)
exit(1);
S->top = 0;
S->stacklength = STACK_INIT_SIZE;
}
void Push_SqStack(SqStack *S,ElemType e)
{
if (S->top == S->stacklength){
S->base = (ElemType*)realloc(S->base,(S->stacklength
+STACK_REALLOCATION)*sizeof(ElemType));
if (!S->base)
exit(1);
S->stacklength += STACK_REALLOCATION;
}
S->base[S->top] = e;
++S->top;
}
int Pop_SqStack(SqStack *S, ElemType *e)
{
if (S->top == 0)
return 0;
--S->top;
*e = S->base[S->top];
return 1;
}
int GetTopElem_SqStack(SqStack *S, ElemType *e)
{
if (S->top == 0)
return 0;
*e = S->base[S->top-1];
return 1;
}
int isSqStackEmpty(SqStack S)
{
if (S.top == 0)
return 1;
else
return 0;
}
void SqStack_test()
{
SqStack S;
InitSqStack(&S);
ElemType e;
printf("Please input some numbers(Ctrl + Z to end).\n");
while (scanf("%d", &e) != EOF)
Push_SqStack(&S,e);
printf("The out stack sequence is:\n");
while (!isSqStackEmpty(S)){
Pop_SqStack(&S,&e);
printf("%d ",e);
}
printf("\n");
}
int main()
{
SqStack_test();
return 0;
}
链式栈:
[cpp] view
plaincopy
#include <stdio.h>
#include <stdlib.h>
#define STACK_INIT_SIZE 10
#define STACK_REALLOCATION 2
typedef int ElemType;
typedef struct LinkStack
{
ElemType data;
struct LinkStack *next;
}StackNode,*StackPNode;
StackPNode InitLinkStack()
{
StackPNode head = (StackPNode)malloc(sizeof(StackNode));
if (!head)
exit(1);
head->next = NULL;
head->data = 0;
return head;
}
void Push_LinkStack(StackPNode head, ElemType e)
{
StackPNode node = (StackPNode)malloc(sizeof(StackNode));
if (!node)
exit(1);
node->data = e;
node->next = NULL;
node->next = head->next;
head->next = node;
}
int Pop_LinkStack(StackPNode head, ElemType *e)
{
StackPNode p;
if (!head->next)
return 0;
//StackPNode p;
//StackPNode p;
//struct LinkStack *p;
p= head->next;
*e=p->data;
head->next = p->next;
free(p);
return 1;
}
int isLinkStackEmpty(StackPNode head)
{
if (head->next)
return 0;
else
return 1;
}
int getTopElem_LinkStack(StackPNode head,ElemType *e)
{
if (head->next)
return 0;
*e = head->next->data;
return 1;
}
void LinkStack_test()
{
StackPNode head = InitLinkStack();
ElemType e;
printf("Please input some numbers(Ctrl + Z to end).\n");
while (scanf("%d", &e) != EOF)
Push_LinkStack(head,e);
printf("The out stack sequence is:\n");
while (!isLinkStackEmpty(head)){
Pop_LinkStack(head, &e);
printf("%d ", e);
}
printf("\n");
}
int main()
{
LinkQueueTest();
return 0;
}
相关文章推荐
- 用头插、尾插、按顺序插入创建一个不带头节点的链表
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 编程菜鸟的日记-初学尝试编程-顺序表的类定义及其基本操作算法(创建表、元素插入、元素删除、顺序查找、测表空、求表长、输出等)
- C语言实现单链表(带头结点)的基本操作(创建,头插法,尾插法,删除结点,打印链表)
- 算法研究之——链表的一些操作(创建、打印,在头部、中间、尾部插入节点)
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- java实现创建链表以及插入节点,查找结点,删除节点等操作
- 设计一个整型链表类list,能够实现链表节点的插入、删除、以及链表数据的输出操作。
- 带头节点的单链表的基本操作
- 线性表的链式存储格式基本操作:创建链表、插入、删除、查找、求表长、打印链表
- 带头节点的单链表的插入操作优化
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- 不带头节点的双向循环链表的基本操作
- 带头节点循环单链表的操作(创建,保序插入,指定插入,删除指定表元,删除相同元素,统计某元素表元个数)
- 不带头结点的单链表的插入,删除,原地转置,判断空,清空,统计节点数目等操作
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- 每天一个小程序(2)——带头结点的链表的创建以及插入和删除
- C-链表的一些基本操作【创建-删除-打印-插入】
- 单链表基本操作:创建、插入、删除、获取
- c语言:顺序表的实现(一) 创建,插入,删除,查找,输出等基本操作实现