在C语言中,如何在链表中插入、删除和显示元素?

在C语言中,如何在链表中插入、删除和显示元素?,c,insert,linked-list,C,Insert,Linked List,我正在做一个C语言的程序,我还不太熟悉。它可以选择如何处理链表。但它也有错误。到目前为止,这就是我所拥有的 #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*head; void append(int num) { struct node *temp,*right; temp= (struct node *)malloc

我正在做一个C语言的程序,我还不太熟悉。它可以选择如何处理链表。但它也有错误。到目前为止,这就是我所拥有的

#include<stdio.h>
#include<stdlib.h>

struct node
{
    int data;
    struct node *next;
}*head;



void append(int num)
{
    struct node *temp,*right;
    temp= (struct node *)malloc(sizeof(struct node));
    temp->data=num;
    right=(struct node *)head;
    while(right->next != NULL)
    right=right->next;
    right->next =temp;
    right=temp;
    right->next=NULL;
}



void add( int num )
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
    head=temp;
    head->next=NULL;
    }
    else
    {
    temp->next=head;
    head=temp;
    }
}
void addafter(int num, int loc)
{
    int i;
    struct node *temp,*left,*right;
    right=head;
    for(i=1;i<loc;i++)
    {
    left=right;
    right=right->next;
    }
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    left->next=temp;
    left=temp;
    left->next=right;
    return;
}

int count()
{
    struct node *n;
    int c=0;
    n=head;
    while(n!=NULL)
    {
    n=n->next;
    c++;
    }
    return c;
} 


void insert(int num)
{
    int c=0;
    struct node *temp;
    temp=head;
    if(temp==NULL)
    {
    add(num);
    }
    else
    {
    while(temp!=NULL)
    {
        if(temp->data<num)
        c++;
        temp=temp->next;
    }
    if(c==0)
        add(num);
    else if(c<count())
        addafter(num,++c);
    else
        append(num);
    }
}



int delete(int num)
{
    struct node *temp, *prev;
    temp=head;
    while(temp!=NULL)
    {
    if(temp->data==num)
    {
        if(temp==head)
        {
        head=temp->next;
        free(temp);
        return 1;
        }
        else
        {
        prev->next=temp->next;
        free(temp);
        return 1;
        }
    }
    else
    {
        prev=temp;
        temp= temp->next;
    }
    }
    return 0;
}


void  display(struct node *r)
{
    r=head;
    if(r==NULL)
    {
    return;
    }
    while(r!=NULL)
    {
    printf("%d ",r->data);
    r=r->next;
    }
    printf("\n");
}


int  main()
{
    int i,num;
    struct node *n;
    head=NULL;
    while(1)
    {
    printf("\nList Operations\n");
    printf("===============\n");
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Size\n");
    printf("4.Delete\n");
    printf("5.Exit\n");
    printf("Enter your choice : ");
    if(scanf("%d",&i)<=0){
        printf("Enter only an Integer\n");
        exit(0);
    } else {
        switch(i)
        {
        case 1:      printf("Enter the number to insert : ");
                 scanf("%d",&num);
                 insert(num);
                 break;
        case 2:     if(head==NULL)
                {
                printf("List is Empty\n");
                }
                else
                {
                printf("Element(s) in the list are : ");
                }
                display(n);
                break;
        case 3:     printf("Size of the list is %d\n",count());
                break;
        case 4:     if(head==NULL)
                printf("List is Empty\n");
                else{
                printf("Enter the number to delete : ");
                scanf("%d",&num);
                if(delete(num))
                    printf("%d deleted successfully\n",num);
                else
                    printf("%d not found in the list\n",num);
                }
                break;
        case 5:     return 0;
        default:    printf("Invalid option\n");
        }
    }
    }
    return 0;
}
#包括
#包括
结构节点
{
int数据;
结构节点*下一步;
}*头部;
无效附加(int num)
{
结构节点*temp,*右;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=num;
右=(结构节点*)头;
while(右->下一步!=NULL)
右=右->下一步;
右->下一步=温度;
右=温度;
右->下一步=空;
}
无效添加(整数)
{
结构节点*temp;
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=num;
if(head==NULL)
{
压头=温度;
head->next=NULL;
}
其他的
{
温度->下一步=头部;
压头=温度;
}
}
void addafter(int num,int loc)
{
int i;
结构节点*temp、*left、*right;
右=头部;
对于(i=1;不精确;
}
temp=(结构节点*)malloc(sizeof(结构节点));
温度->数据=num;
左->下一步=温度;
左=温度;
左->下一步=右;
返回;
}
整数计数()
{
结构节点*n;
int c=0;
n=头部;
while(n!=NULL)
{
n=n->next;
C++;
}
返回c;
} 
无效插入(整数)
{
int c=0;
结构节点*temp;
温度=水头;
if(temp==NULL)
{
添加(num);
}
其他的
{
while(temp!=NULL)
{
如果(临时->数据下一步;
}
如果(c==0)
添加(num);
else if(cdata==num)
{
如果(温度==水头)
{
头=温度->下一步;
免费(临时);
返回1;
}
其他的
{
上一个->下一个=临时->下一个;
免费(临时);
返回1;
}
}
其他的
{
prev=温度;
温度=温度->下一步;
}
}
返回0;
}
无效显示(结构节点*r)
{
r=头部;
if(r==NULL)
{
返回;
}
while(r!=NULL)
{
printf(“%d”,r->data);
r=r->next;
}
printf(“\n”);
}
int main()
{
int i,num;
结构节点*n;
head=NULL;
而(1)
{
printf(“\n列出操作\n”);
printf(“============================\n”);
printf(“1.插入\n”);
printf(“2.显示\n”);
printf(“3.Size\n”);
printf(“4.删除\n”);
printf(“5.退出\n”);
printf(“输入您的选择:”);

如果(scanf(“%d”,&i)我做了另一个工作程序。看看这个。你可能有一些补充,也许

#include <iostream>
#include <conio.h>
using namespace std;

class Node {
      int data;
      Node* next;
      public:
             Node() {};
             void SetData(int aData) { data = aData; };
             void SetNext(Node* aNext) { next = aNext; };
             int Data() { return data; };
             Node* Next() { return next; };
};

class List {
      Node *head;
      public:
             List() { head = NULL; };
             void Display();
             void Append(int data);
             void Remove(int data);
};

void List::Display() {
     Node *tmp = head;
     if ( tmp == NULL ) {
          cout << "EMPTY" << endl;
          return;
     }
     if ( tmp->Next() == NULL ) {
          cout << tmp->Data();
          cout << " --> ";
          cout << "NULL" << endl;
          }
     else {
          do {
              cout << tmp->Data();
              cout << " --> ";
              tmp = tmp->Next();
          } while ( tmp != NULL );
     cout << "NULL" << endl;
     }
}

void List::Append(int data) {
     Node* newNode = new Node();
     newNode->SetData(data);
     newNode->SetNext(NULL);
     Node *tmp = head;
     if ( tmp != NULL ) {
          while ( tmp->Next() != NULL ) {
                tmp = tmp->Next();
          }
     tmp->SetNext(newNode);
     }
     else {
          head = newNode;
     }
}

void List::Remove(int data) {
     Node *tmp = head;
     if ( tmp == NULL )
          return;
     if ( tmp->Next() == NULL ) {
          delete tmp;
          head = NULL;
     }
     else {
          Node *prev;
          do {
              if ( tmp->Data() == data ) break;
              prev = tmp;
              tmp = tmp->Next();
          } while ( tmp != NULL );
     prev->SetNext(tmp->Next());
     delete tmp;
     }
}

int main() {
    List list;
    int n[100], i, size;
    char choice;
    for(i=1; i<=100; i++){
             cout<<"Insert[I]\nDisplay[D]\nRemove[R]\n";
             cin>>choice;
             if (choice=='I'||choice=='i'){
                                       cout<<"Enter the number of nodes: ";
                                       cin>>size;
                                       cout<<"Enter a number to be inserted:\n";
                                       for(i=1; i<=size; i++){
                                                         cin>>n[i];
                                                         list.Append(n[i]);
                                       }
             }
             if (choice=='D'||choice=='d'){
                                       cout<<"Elements in the list are: ";
                                       list.Display();
             }
             if (choice=='R'||choice=='r'){  
                                       cout<<"Enter a number to be removed: ";
                                       cin>>n[i];
                                       list.Remove(n[i]);
             }
    }
    getch();
}
#包括
#包括
使用名称空间std;
类节点{
int数据;
节点*下一步;
公众:
Node(){};
void SetData(int aData){data=aData;};
void SetNext(Node*aNext){next=aNext;};
int Data(){return Data;};
Node*Next(){return Next;};
};
班级名单{
节点*头;
公众:
List(){head=NULL;};
void Display();
无效附加(int数据);
无效删除(int数据);
};
无效列表::显示(){
节点*tmp=头部;
if(tmp==NULL){
cout Next();
}
tmp->SetNext(新节点);
}
否则{
头=新节点;
}
}
无效列表::删除(整型数据){
节点*tmp=头部;
if(tmp==NULL)
返回;
如果(tmp->Next()==NULL){
删除tmp;
head=NULL;
}
否则{
节点*prev;
做{
如果(tmp->Data()==数据)中断;
prev=tmp;
tmp=tmp->Next();
}while(tmp!=NULL);
prev->SetNext(tmp->Next());
删除tmp;
}
}
int main(){
名单;
int n[100],i,大小;
字符选择;

对于(i=1;您经历了什么错误,编译或运行时间?这不是C++,即C。当请求编译错误时,张贴完全错误消息并在源代码中使用注释来指示错误消息引用的行。那么,您是否键入整个程序,然后编译或以比特进行编译。编译错误是什么?你得到了吗?<代码>删除>代码>是C++中的关键字,你不能用它来命名函数。或者编译为C,或者重新启动,使用C++,这不是C,你已经用C++编写了代码。