C语言 结构体使用,关于链表和栈的区别
#include<stdio.h>#include<malloc.h>typedefstructlist{intdata;structlist*next;//strust...
#include<stdio.h>
#include<malloc.h>
typedef struct list {
int data;
struct list * next; //strust list next
} Node;
typedef struct {
Node * list;//Node list
//int numbe;
} stack;
//----------------------------------------------------------------
void add(Node ** head, int d) {
Node *p = malloc(sizeof(Node));
p->data = d;
p->next = NULL;
if (*head == NULL) {
*head = p;
} else {
Node* q = *head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
void delete(Node **head, int d) {
if (*head == NULL)
return;
if ((*head)->data == d) {
if ((*head)->next == NULL) {
free((*head));
*head = NULL;//??????????????????
return;
} else {
Node * m = (*head)->next;
free((*head));
*head = m;
return;
}
}
Node* p = (*head)->next;
Node* q = (*head);
while (p != NULL) {
if (p->data == d) {
q->next = p->next;
free(p);
return;
}
q = q->next;
p = p->next;
}
}
void update(Node *head, int d1, int d2) {
Node *p = head;
if (p == NULL)
return;
while (p != NULL) {
if (p->data == d1) {
p->data = d2;
return;
}
p = p->next;
}
printf("Not Found!");
}
int serach(Node* head, int d) {
Node *p = head;
while (p != NULL) {
if (p->data == d) {
return 1;
}
p = p->next;
}
return 0;
}
int length(Node *head) {
Node *p = head;
int total = 0;
while (p != NULL) {
total++;
p = p->next;
}
return total;
}
void print(Node * head) {
Node *p = head;
while (p != NULL) {
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
return;
}
//------------------------------------------------------------------
int getlast(Node * head) {
if (head == NULL)
return -1;
if (head->next == NULL) {
return head->data;
} else {
Node *p = head;
while (p->next != NULL) {
p = p->next;
}
return p->data;
}
}
void push(stack* ps, int d) {
add(&ps->list, d);
//ps->numbe = ps->numbe + 1;
}
void pop(stack *s) {
delete(&(s->list), getlast(s->list));
//s->numbe = s->numbe - 1;
}
int top(stack *s) {
return getlast(s->list);
}
int length_stack(stack *s) {
return length(s->list);
}
int empty(stack *s) {
if (length_stack(s)) {
return 1;
} else {
return 0;
}
}
void print_stack(stack *s) {
print(s->list);
}
int main() {
stack *st = NULL;//此处应该是stack st;
st->list = NULL;
//st->numbe = 0;
push(st, 1);
push(st, 2);
while (empty(st)) {
printf("%d\t", top(st));
pop(st);
}
//-------------------------------
Node *head = NULL;//此处可以用指针!!!
add(&head, 1);
print(head);
return 0;
}
为什么stack不能声明一个指针去用,非要声明一个变量??为什么Node可以 声明一个指针?? 展开
#include<malloc.h>
typedef struct list {
int data;
struct list * next; //strust list next
} Node;
typedef struct {
Node * list;//Node list
//int numbe;
} stack;
//----------------------------------------------------------------
void add(Node ** head, int d) {
Node *p = malloc(sizeof(Node));
p->data = d;
p->next = NULL;
if (*head == NULL) {
*head = p;
} else {
Node* q = *head;
while (q->next != NULL) {
q = q->next;
}
q->next = p;
}
}
void delete(Node **head, int d) {
if (*head == NULL)
return;
if ((*head)->data == d) {
if ((*head)->next == NULL) {
free((*head));
*head = NULL;//??????????????????
return;
} else {
Node * m = (*head)->next;
free((*head));
*head = m;
return;
}
}
Node* p = (*head)->next;
Node* q = (*head);
while (p != NULL) {
if (p->data == d) {
q->next = p->next;
free(p);
return;
}
q = q->next;
p = p->next;
}
}
void update(Node *head, int d1, int d2) {
Node *p = head;
if (p == NULL)
return;
while (p != NULL) {
if (p->data == d1) {
p->data = d2;
return;
}
p = p->next;
}
printf("Not Found!");
}
int serach(Node* head, int d) {
Node *p = head;
while (p != NULL) {
if (p->data == d) {
return 1;
}
p = p->next;
}
return 0;
}
int length(Node *head) {
Node *p = head;
int total = 0;
while (p != NULL) {
total++;
p = p->next;
}
return total;
}
void print(Node * head) {
Node *p = head;
while (p != NULL) {
printf("%d\t", p->data);
p = p->next;
}
printf("\n");
return;
}
//------------------------------------------------------------------
int getlast(Node * head) {
if (head == NULL)
return -1;
if (head->next == NULL) {
return head->data;
} else {
Node *p = head;
while (p->next != NULL) {
p = p->next;
}
return p->data;
}
}
void push(stack* ps, int d) {
add(&ps->list, d);
//ps->numbe = ps->numbe + 1;
}
void pop(stack *s) {
delete(&(s->list), getlast(s->list));
//s->numbe = s->numbe - 1;
}
int top(stack *s) {
return getlast(s->list);
}
int length_stack(stack *s) {
return length(s->list);
}
int empty(stack *s) {
if (length_stack(s)) {
return 1;
} else {
return 0;
}
}
void print_stack(stack *s) {
print(s->list);
}
int main() {
stack *st = NULL;//此处应该是stack st;
st->list = NULL;
//st->numbe = 0;
push(st, 1);
push(st, 2);
while (empty(st)) {
printf("%d\t", top(st));
pop(st);
}
//-------------------------------
Node *head = NULL;//此处可以用指针!!!
add(&head, 1);
print(head);
return 0;
}
为什么stack不能声明一个指针去用,非要声明一个变量??为什么Node可以 声明一个指针?? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询