如何在列表中插入特定数量的元素?
How can I insert a specific number of elements in a list?
我试图通过使用 size_t n 在列表中插入特定数量的元素(来自结构的四个元素,每个元素代表一个数据包)但是当我 运行 主程序时它什么都不显示。我如何在插入函数中定义大小,以便当更多数据包(四组)尝试插入列表时,它们将因为列表的大小而无法插入?
struct Packet
{
int rollnumber;
int src;
int dest;
double gentime;
struct Packet *next;
}* head;
void insert(int rollnumber, int src, int dest, double gentime, size_t n)
{
if (n==0) n=(size_t)-1;
struct Packet * packet = (struct Packet *) malloc(sizeof(struct Packet));
for ( struct Packet * temp = head; n-- && temp != NULL; temp = temp->next ){
packet->rollnumber = rollnumber;
packet->src=src;
packet->dest=dest;
packet->gentime = gentime;
packet->next = NULL;
if(head==NULL){
head = packet;
}
else{
packet->next = head;
head = packet;
}
}
}
void display()
{
struct Packet * temp = head;
while(temp != NULL)
{
printf("Roll Number: %d\n",temp->rollnumber);
printf("src: %d\n", temp->src);
printf("dest: %d\n", temp->dest);
printf("gentime: %0.1f\n\n", temp->gentime);
temp = temp->next;
}
}
编辑: 下面的新代码是一个完整的工作示例。它应该给你足够的开始去休息。
您创建了上面的数据包,但从未真正将其放入您的链中。这将创建它并尝试找到插入它的正确位置:
#include <stdlib.h>
#include <stdio.h>
struct Packet
{
int rollnumber;
int src;
int dest;
double gentime;
struct Packet *next;
}* head;
void insert(int rollnumber, int src, int dest, double gentime, size_t n)
{
/* Assuming you're trying to insert these values at point n */
if (n >= 15) {
printf("no %d!\n", n);
return;
}
/* create the packet to insert */
struct Packet * packet = (struct Packet *) malloc(sizeof(struct Packet));
packet->rollnumber = rollnumber;
packet->src=src;
packet->dest=dest;
packet->gentime = gentime;
packet->next = NULL;
/* now find the point where it should be inserted */
if (n == 0 || head == NULL) {
/* it should be first! */
packet->next = head;
head = packet;
} else {
struct Packet *temp = head;
for ( ; n > 1 && temp != NULL && temp->next != NULL ; temp = temp->next, n-- ){
}
if (temp != NULL) {
/* put ourselves in the middle (or the end if packet->next is NULL) */
packet->next = temp->next;
temp->next = packet;
}
}
}
void display()
{
printf("-----------------\n");
struct Packet * temp = head;
while(temp != NULL)
{
printf("Roll Number: %d\n",temp->rollnumber);
printf("src: %d\n", temp->src);
printf("dest: %d\n", temp->dest);
printf("gentime: %0.1f\n\n", temp->gentime);
temp = temp->next;
}
}
int main() {
insert(1,1,1,1,10); /* legal, will just append */
display();
insert(2,2,2,2,0); /* insert before 1 */
display();
insert(3,3,3,3,2); /* insert after 2 */
display();
insert(4,4,4,4,1); /* after 2, and before 3 */
display();
insert(5,5,5,5,15); /* illegal */
}
我试图通过使用 size_t n 在列表中插入特定数量的元素(来自结构的四个元素,每个元素代表一个数据包)但是当我 运行 主程序时它什么都不显示。我如何在插入函数中定义大小,以便当更多数据包(四组)尝试插入列表时,它们将因为列表的大小而无法插入?
struct Packet
{
int rollnumber;
int src;
int dest;
double gentime;
struct Packet *next;
}* head;
void insert(int rollnumber, int src, int dest, double gentime, size_t n)
{
if (n==0) n=(size_t)-1;
struct Packet * packet = (struct Packet *) malloc(sizeof(struct Packet));
for ( struct Packet * temp = head; n-- && temp != NULL; temp = temp->next ){
packet->rollnumber = rollnumber;
packet->src=src;
packet->dest=dest;
packet->gentime = gentime;
packet->next = NULL;
if(head==NULL){
head = packet;
}
else{
packet->next = head;
head = packet;
}
}
}
void display()
{
struct Packet * temp = head;
while(temp != NULL)
{
printf("Roll Number: %d\n",temp->rollnumber);
printf("src: %d\n", temp->src);
printf("dest: %d\n", temp->dest);
printf("gentime: %0.1f\n\n", temp->gentime);
temp = temp->next;
}
}
编辑: 下面的新代码是一个完整的工作示例。它应该给你足够的开始去休息。
您创建了上面的数据包,但从未真正将其放入您的链中。这将创建它并尝试找到插入它的正确位置:
#include <stdlib.h>
#include <stdio.h>
struct Packet
{
int rollnumber;
int src;
int dest;
double gentime;
struct Packet *next;
}* head;
void insert(int rollnumber, int src, int dest, double gentime, size_t n)
{
/* Assuming you're trying to insert these values at point n */
if (n >= 15) {
printf("no %d!\n", n);
return;
}
/* create the packet to insert */
struct Packet * packet = (struct Packet *) malloc(sizeof(struct Packet));
packet->rollnumber = rollnumber;
packet->src=src;
packet->dest=dest;
packet->gentime = gentime;
packet->next = NULL;
/* now find the point where it should be inserted */
if (n == 0 || head == NULL) {
/* it should be first! */
packet->next = head;
head = packet;
} else {
struct Packet *temp = head;
for ( ; n > 1 && temp != NULL && temp->next != NULL ; temp = temp->next, n-- ){
}
if (temp != NULL) {
/* put ourselves in the middle (or the end if packet->next is NULL) */
packet->next = temp->next;
temp->next = packet;
}
}
}
void display()
{
printf("-----------------\n");
struct Packet * temp = head;
while(temp != NULL)
{
printf("Roll Number: %d\n",temp->rollnumber);
printf("src: %d\n", temp->src);
printf("dest: %d\n", temp->dest);
printf("gentime: %0.1f\n\n", temp->gentime);
temp = temp->next;
}
}
int main() {
insert(1,1,1,1,10); /* legal, will just append */
display();
insert(2,2,2,2,0); /* insert before 1 */
display();
insert(3,3,3,3,2); /* insert after 2 */
display();
insert(4,4,4,4,1); /* after 2, and before 3 */
display();
insert(5,5,5,5,15); /* illegal */
}