繁体   English   中英

c语言中的简单链表程序

[英]simple linked list program in c

//我在c中实现了简单的链表程序在这里我在第一个,最后一个,特定位置实现了插入..执行时发生了这些错误:预期的'=',',',';','asm'或' attribute '之前'*' 标记
..任何人都可以请澄清这些错误是什么

#include<stdio.h>
#include<malloc.h>
#define ISEMPTY printf("\n empty list");

struct node{ 
    int value;
    struct node *next;
}
startnode * create_node(int);
void insert_node_first();
void insert_node_last();
void insert_node_position();

typedef struct node start_node;

start_node *newnode,*ptr,*prev,*temp;
start_node *first=NULL,*last=NULL;


int main()
{
    int ch;
    char ans='Y';
    while(ans == 'Y' || ans == 'y')
    {
        printf("\n Single linked list \n");
        printf("\n1.INSERT NODE AT FIRST");
        printf("\n2.INSERT NODE AT LAST");
        printf("\n3.INSERT NODE AT POSITION");
        printf("\n ENTER YOUR CHOICE");
        scanf("%d",&ch);
            switch(ans)
            {
                case 1:printf("INSERT NODE AT FIRST");insert_node_first();break;
                case 2:printf("INSERT NODE AT LAST");insert_node_last();break;
                case 3:printf("INSERT NODE AT POSITION");insert_node_position;break;
                default:printf("U DIDN'T SELECT ANYTHING SO EXCEED");
            }
            printf("DO YOU WANT TO CONTINUE");
            scanf("%c",&ans);
    }
    return 0;
}
start_node* create_node(int val)
{
    newnode=(start_node *)malloc(sizeof(start_node));
    if(newnode==NULL)
    {
        printf("\n Memory not allocated");
        return 0;
    }
    else{
        newnode->value=val;
        newnode->next=NULL;
        return newnode;
    }
}
void insert_node_first()  
{
    int val;
    printf("Enter the value for the node");
    scanf("%d",&val);
    newnode=create_node(val);

    if(first == last && first == NULL)
    {
        first=last=newnode;
        first->next=NULL;
        last->next=NULL;
    }

    else{
        temp=first;
        first=newnode;
        first->next=temp;
    }
    printf("\n inserted");
}
void insert_node_last()  
{
    int val;
    printf("Enter the value insert at last");
    scanf("%d",&val);
    newnode=create_node(val);
    if(first == last && first == NULL)
    {
        first=last=newnode;
        first->next=NULL;
        last->next=NULL;
    }
    else{
        last->next=newnode;
        last=newnode;
        last->next=NULL;
    }
}
void insert_node_position()
{
    int val,position,count,i;
    printf("Enter the value to insert");
    scanf("%d",&val);
    newnode=create_node(val);
    printf("Enter the position you want to ");
    scanf("%d",&position);
    ptr=first;
        while(ptr !=NULL)
        {
            ptr=ptr->next;
            count++;
        }
        if(position == 1)
        {
            if(first == last && first == NULL)
            {
                first=last=newnode;
                first->next=NULL;
                last->next=NULL;
            }
            else{
                temp=first;
                first=newnode;
                first->next=temp;
            }
        }
        else if(position > 1 && position<=count)
        {
            ptr=first;
            for(i=1;i<position;i++)
            {
                prev=ptr;
                ptr=ptr->next;
            }
            prev->next=newnode;
            newnode->next=ptr;
            printf("inserted at position");
        }else{
            printf("position is out of range");
        }
}

使用start_node更改startnode并移动

start_node * create_node(int);

typedef struct node start_node;

否则编译器此时不知道start_node是什么;

而且你忘记了; 关于结构声明

struct node{ 
    int value;
    struct node *next;
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM