Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Help PLEASE with LINK-LIST

Posted on 2003-03-27
4
Medium Priority
?
235 Views
Last Modified: 2011-04-14
I received this code from my instructor and know very little about C/C++.  What i have to do is change the code so that it stored INTEGERS in ASCENDING order and numbers are ENTER FROM KEYBOARD.  Has to store INTS 0-1000, inclusive. Once the list is built, visit each node in the doubly linked list FORWARD then BACKWARDS.  I have to print(to screen) FORWARDS/BACKWARDS.  Can anyone please help me with this promblem.  I really need the code. I would greatly appreciate any effort with this task.

******************************************HERE IS THE CODE*********************************************
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <ctype.h>
#include <dos.h>
#include <stdio.h>
#include <conio.h>
#include <iomanip.h>


     typedef struct node
     {
          char data;
          struct node *link;
     }     LNODE;

void show_list(LNODE *ptr);
void add_node (LNODE **ptr, char item);

main()
{
     LNODE *n1 = NULL;
     char item;

     do
     {
          printf("\nENTER ITEM: ");
          item = getche();
          if(item != '\r')
               add_node(&n1,item);
     } while (item !='\r');
     printf('\nTHE NEW LINKED-LIST IS: ");
      show_list(n1);
}

void show_list(LNODE *ptr)
{
     while(ptr != NULL)
     {
          printf("%c",ptr -> data);
          ptr = ptr -> link;
     }
     printf("\n");

}

void add_node(LNODE **ptr,char item)
{
     LNODE *p1, *p2;

     p1 = *ptr;
     if(p1 == NULL)
     {
          p1 = malloc(sizeof(LNODE));
          if(p1 1= NULL)
          {
               p1 -> data = item;
               p1 -> link = NULL;
               *ptr = p1;
          }
     }
     else
     {
          while(p1 -> link != NULL)
               p1 = p1 ->link;
          p2 = malloc(sizeof(LNODE)):
          if(p2 != NULL)
          {
               p2 -> data = item;
               p2 -> link = NULL;
               p1 -> link = p2;
          }
  }
}
**************************************THANKS*********************************************************
0
Comment
Question by:Ride_Green2002
3 Comments
 

Expert Comment

by:chryso
ID: 8221451
Ride_Green2002:

You're going to want to do a type of insertion sort on the list.  When you get a new input, starting from the beginning of the list (keep a pointer to the first element), traverse each link pointer until the data in the current node (we'll call it "curnode") is smaller than your input, insert at that point.  To insert an item in a linked list:

-make a new node with your new input

-in your new node, make link point to the same node as "curnode" (newnode->link = curnode->link)

-set curnode's link to your newnode (curnode->link=curnode)

Hope this helps!

-chryso
0
 
LVL 5

Accepted Solution

by:
Kocil earned 500 total points
ID: 8222950
Your's now
ptr -> [node1]->[node2]->[node3]

You want double linked list

ptr -> [node1]<->[node2]<->[node3]<->{circle to [node1]}

This is the code.
/* I hate to show you the code,
but it is faster for me than to write a long explanation */

typedef struct node
{
  //         char data; change to int
  int data;
//  struct node *link;
  struct node *prev, next;  // two for double link list
} LNODE;

// void show_list(LNODE *ptr);
void show_forward(LNODE *ptr);   // show forward
void show_backward(LNODE *ptr);  // show backward

// void add_node (LNODE **ptr, char item);
void add_node (LNODE **ptr, int item);  // item is int now

main()
{
    LNODE *n1 = NULL;
//  char item;
    int item;
    do
    {
         printf("\nENTER ITEM: ");
         // item = getche();
         scanf("%d", &item)
         // if(item != '\r')  
         if ((item >= 0) && (item <= 1000))
              add_node(&n1,item);
//    } while (item !='\r');
  } while (item != -1)   /// assume -1 to stop  
  printf('\nTHE NEW LINKED-LIST IS: ");
//  show_list(n1);
  show_forward(n1);
  show_backward(n1);
}

// void show_list(LNODE *ptr)
// void show_forward(LNODE *ptr)
{
    int ptr1;
    if (*ptr) {
       ptr1 = ptr;
       while(ptr != NULL)
       {
         // printf("%c",ptr -> data);
         printf("%4d ",ptr -> data);
         ptr = ptr -> next;
       }
       printf("\n");
    }
    else printf("EMPTY\n");
}

void show_backward(LNODE *ptr)
{
    // do it by yourself
    ....
}

// void add_node(LNODE **ptr,char item)
void add_node(LNODE **ptr, int item)
{
    LNODE *p1, *pitem;

    pitem = malloc(sizeof(LNODE));
    if(p1 == NULL) return;  // should return FAIL here
    pitem -> data = item;

    if(*ptr == NULL) // test if the list is empty
    {
        pitem -> prev = pitem->next = pitem; // circle to self
        *ptr = pitem;
        return;
    }
    p1 = *ptr;
    if (p1 -> data < item) // test if item is the smallest
    {
        // insert first
        pitem->next = p1;
        pitem->prev = p1->prev;
        p1->prev->next = pitem;
        p1->prev = pitem;
        *ptr = pitem;
        return;
    }
    // search insert position ascending
    while((p1->next != *ptr) && (p1->next->data < item))
       p1 = p1->next;

    // insert after p1 now
    pitem->next = p1->next;
    pitem->prev = p1;
    p1->next->prev = pitem;
    p1->next = pitem;  
}
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9551611
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by: Kocil

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question