0

I'm working on a program in C which is not yet so familiar to me. It has choices on what to do with the linked list. But it has errors. So far, this is what I have.

#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;
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ninadeleon
  • 21
  • 1
  • 1
  • 6
  • what errors are you experiencing, compile or run time? – ColWhi Feb 19 '13 at 08:48
  • 3
    That's not C++, that's C. – Some programmer dude Feb 19 '13 at 08:49
  • 3
    When asking for help with compilation errors, post the full error message and in your source code, use comments to indicate lines to which the error message refers. – Angew is no longer proud of SO Feb 19 '13 at 08:51
  • so did you type the whole program in then compile or did you do it in bits. What compile error are you getting? – ColWhi Feb 19 '13 at 08:51
  • When I compile it, it has errors on the delete method. – ninadeleon Feb 19 '13 at 08:53
  • Also, as @JoachimPileborg pointed out, your program is pure C. And while technically it is valid C++ (as most C is), it doesn't follow C++ principles or use C++ features. If you really want to use "proper" C++, you should start over (and read a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) first). – Angew is no longer proud of SO Feb 19 '13 at 08:53
  • This rather looks like C than like C++. C code is perfectly fine C++ syntax, but C++ already has things like lists build in (http://www.cplusplus.com/reference/list/list/) and follows other principles when devoloping. If you want to continue your C-like implementation you might want to look at C tutorial. – Marc Fischer Feb 19 '13 at 08:53
  • 3
    `delete` is a keyword in C++, you cannot use it to name functions. Either compile this as C, or start over and use C++ – Angew is no longer proud of SO Feb 19 '13 at 08:54
  • I see. I guess I really have to start it all over? – ninadeleon Feb 19 '13 at 08:55
  • @ninadeleon Algorithmic principles are the same, but the language constructs used to express them will be vastly different. `new` instead of `malloc`, `cin`/`cout` instead of `scanf`/`printf`, member functions instead of free functions etc. – Angew is no longer proud of SO Feb 19 '13 at 09:11
  • Or just rename the `delete` function to `remove`... – Bartek Banachewicz Feb 19 '13 at 09:29
  • Before you start all over, you have to decide, and say, what you want. If you want to learn C, go ahead, but compile as C code (than `delete` won't be keyword, so it should compile). If you want to learn C++, you should probably choose different example. – Jan Hudec Feb 19 '13 at 09:36

1 Answers1

0

I made another working program. Take a look at this. You might have some additions, maybe?

#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();
}
ninadeleon
  • 21
  • 1
  • 1
  • 6