【发布时间】:2015-07-12 06:55:41
【问题描述】:
我正在尝试解决我在使用 insertIntoS 函数时遇到的问题。我正在尝试从堆栈的头部向链表或堆栈中插入一个值。但是,我这样做并且它崩溃了。当我从最高点开始时它会起作用,我觉得这是因为头点指向 NULL。这是我的问题吗?我该如何解决这个问题才能从头部插入到第 n 个位置?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct node{
int data;
struct node *next;
} Node;
typedef struct elem {
int data;
struct elem *next;
} Element;
Node *top = NULL;
Node *head = NULL;
Element *rear = NULL;
Element *front = NULL;
void pop();
void display();
void push (int num);
void dequeue();
void displayQ();
void insertIntoS(int data, int index);
void insertIntoQ(int data, int index);
void enqueue(int num);
int main(){
int choice, num, index;
while(1) {
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - display stack");
printf("\n 4 - Enqueue");
printf("\n 5 - Dequeue ");
printf("\n 6 - Show Queue ");
printf("\n 7 - Insert Into Queue ");
printf("\n 8 - Insert Into Stack ");
printf("\n 9 - Exit ");
printf("\n Please enter your choice");
scanf("%d",&choice);
switch (choice) {
case 1:
printf("Enter Data : ");
scanf("%d", &num);
push(num);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Enter Data: ");
scanf("%d", &num);
enqueue(num);
break;
case 5:
dequeue();
break;
case 6:
displayQ();
break;
case 7:
printf("Enter Data: ");
scanf("%d", &num);
printf("Enter the index: ");
scanf("%d", &index);
insertIntoQ(num, index);
break;
case 8:
printf("Enter Data: ");
scanf("%d", &num);
printf("Enter the index: ");
scanf("%d", &index);
insertIntoS(num, index);
break;
case 9:
exit(0);
default:
break;
}
}
}
void push (int num){
Node *temp = (Node*) malloc(sizeof(Node));
if(temp == NULL){
printf("Stack OVERFLOW");
return;
}
if(head == NULL) {
temp->data = num;
temp->next = NULL;
head = temp;
printf("Value of the head after push : %d \n", head->data);
}
temp->data = num;
temp->next = top;
top = temp;
}
void pop(){
Node *temp;
if(top == NULL){
printf("You are trying to pop from a stack that is empty");
return;
}
temp = top;
top = top->next;
free(temp);
}
void display(){
Node *p;
if(top == NULL){
printf("Empty Stack");
return;
}
printf("\n Stack : \n");
p=top;
while(p != NULL){
printf("%d\n", p->data);
p=p->next;
}
}
void displayQ(){
Element *p = front;
if(p == NULL){
printf("Empty stack\n");
return;
}else{
printf("Queue: \n");
while(p != NULL){
printf("%d\n", p->data);
p=p->next;
}
}
}
void dequeue(){
Element *temp = front;
if(front == NULL){
printf("Queue is empty");
return;
}
if(front == rear){
front = rear = temp;
}else{
front = front->next;
}
free(temp);
}
void enqueue(int num){
Element *temp = (Element*) malloc(sizeof(Element));
if(temp == NULL){
printf("Stack OVERFLOW");
return;
}
temp->data = num;
temp->next = NULL;
if(front == NULL && rear == NULL){
front = rear = temp;
}
rear->next = temp;
rear = temp;
printf("value of rear in enque: %d\n", rear->data);
}
void insertIntoQ(int data, int index){
int i;
Element *temp = (Element*) malloc(sizeof(Element));
temp->data = data;
temp->next = NULL;
if(index == 1){
temp->next = rear;
rear = temp;
return;
}
Element *temp1 = rear;
for(i = 0; i<index;i++){
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp1;
}
void insertIntoS(int data, int index){
int i;
Node *temp1 = (Node*) malloc(sizeof(Node));
temp1->data = data;
temp1->next = NULL;
if(index == 1){
temp1->next = head;
head = temp1;
return;
}
printf("Value of head in insert %d\n", head->data);
Node *temp2 = head;
for(i = 0; i<index;i++){
temp2 = temp2->next;
printf("i count : %d\n", i);
}
temp1->next = temp2->next;
temp2->next = temp1;
}
【问题讨论】:
-
你能举一个导致程序崩溃的输入例子吗?
-
我输入数据如下 1 1 1 2 1 3 1 4 1 5 8 9 3
-
如果不清楚 ^^ Push 1 push 2 push 3 push 4 push 5 stack 看起来像 5 4 3 2 1 然后我在索引 3 处插入值 9
-
@Tyler Pierog 不清楚 head 和 top 是什么意思?它们有什么区别?
-
@Tyler Pierog 如果插入值的索引大于堆栈中的元素个数怎么办?
标签: c linked-list stack